我真的是android和java的新手.我正在制作一个应用程序,它具有媒体服务,我希望在来电时停止或暂停媒体.
这是我的媒体服务代码
公共类ServiceMusic扩展Service {
MediaPlayer music;@OverrIDepublic IBinder onBind(Intent arg0) { return null;}@OverrIDepublic voID onDestroy() { super.onDestroy(); music.stop(); music.release();}@OverrIDepublic voID onStart(Intent intent, int startID) { super.onStart(intent, startID); if (music !=null) music.stop(); music = MediaPlayer.create(ServiceMusic.this, R.raw.music); music.start(); music.setLooPing(true);}
}
如果您能为我提供帮助,并且能给我完整的指导,那将是非常不错的.
谢谢
解决方法:
您必须为来话呼叫创建接收方,如下所示:
public class call_reciver extends broadcastReceiver { @OverrIDe public voID onReceive(Context context, Intent intent) { // Todo auto-generated method stub String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); String number = ""; Bundle bundle = intent.getExtras(); if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { // Phone is ringing number = bundle.getString("incoming_number"); } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { // Call received } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) { // Call Dropped or rejected } }
在清单中放入:
<receiver androID:name=".call_reciver" > <intent-filter> <action androID:name="androID.intent.action.PHONE_STATE" /> </intent-filter> </receiver>
许可:
<uses-permission androID:name="androID.permission.READ_PHONE_STATE" > </uses-permission>
使您的媒体播放器变量变为静态&全局,在接收方呼叫另一个服务以检查您的媒体播放器是否正在运行.如果它正在运行,则停止它.您可以使用也可以使用相同的服务.
您必须从接收方启动服务:
Intent myIntent = new Intent(context, Service_temp.class); context.startService(myIntent);
在使用中使用此:
@OverrIDe public int onStartCommand(Intent intent, int flags, int startID) { // Todo auto-generated method stub return super.onStartCommand(intent, flags, startID); }
检查编辑的ans:
在下面的示例中,我已完成所有 *** 作.有一个按钮可以播放正在播放的歌曲.那时您收到来电,它将停止您的服务(停止播放歌曲),当您拒绝或挂断电话时,它将再次开始播放歌曲
主要活动类别:
package com.example.androIDmediaplay;import androID.os.Bundle;import androID.app.Activity;import androID.content.Intent;import androID.vIEw.Menu;import androID.vIEw.VIEw;public class MainActivity extends Activity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public voID clickBtn(VIEw v) { if (v.getID() == R.ID.button1) { UtilClass.playing = true; Intent i = new Intent(MainActivity.this, BackgroundSoundService.class); startService(i); } }}
BackgroundSoundService类:
package com.example.androIDmediaplay;import java.io.file;import androID.app.Service;import androID.content.Intent;import androID.media.MediaPlayer;import androID.net.Uri;import androID.os.Environment;import androID.os.IBinder;import androID.util.Log;public class BackgroundSoundService extends Service { private static final String TAG = null; MediaPlayer player; public IBinder onBind(Intent arg0) { return null; } @OverrIDe public voID onCreate() { super.onCreate(); player = MediaPlayer.create( this, Uri.fromfile(new file(Environment.getExternalStorageDirectory() + "/Songs/test.mp3"))); player.setLooPing(true); // Set looPing player.setVolume(100, 100); } public int onStartCommand(Intent intent, int flags, int startID) { Log.e("onStartCommand", "onStartCommand"); if (player.isPlaying()) { player.pause(); UtilClass.pause = true; Log.e("onStartCommand pause", "onStartCommand pause"); } else { UtilClass.pause = false; player.start(); } return 1; } public voID onStart(Intent intent, int startID) { // TO DO } public IBinder onUnBind(Intent arg0) { // TO DO auto-generated method return null; } public voID onStop() { } public voID onPause() { } @OverrIDe public voID onDestroy() { UtilClass.playing = false; player.stop(); player.release(); } @OverrIDe public voID onLowMemory() { }}
呼叫接收者类别:
package com.example.androIDmediaplay;import androID.app.ActivityManager;import androID.app.ActivityManager.RunningServiceInfo;import androID.content.broadcastReceiver;import androID.content.Context;import androID.content.Intent;import androID.content.SharedPreferences;import androID.content.SharedPreferences.Editor;import androID.os.Bundle;import androID.preference.PreferenceManager;import androID.telephony.TelephonyManager;import androID.util.Log;public class call_reciver extends broadcastReceiver { @OverrIDe public voID onReceive(Context context, Intent intent) { // Todo auto-generated method stub String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); String number = ""; Bundle bundle = intent.getExtras(); if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { // Phone is ringing number = bundle.getString("incoming_number"); Log.e("incoming_number", "incoming_number"); _stopServices(context); } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { // Call received } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) { // Call Dropped or rejected _stopServices(context); } } private voID _stopServices(Context con) { // Todo auto-generated method stub if (UtilClass.playing == true) { Log.e("start services", "start services"); Intent i = new Intent(con, BackgroundSoundService.class); con.startService(i); } else { Log.e("start not services", "start not services"); } }}
静态成员的额外类:
package com.example.androIDmediaplay;public class UtilClass { public static Boolean playing = false; public static boolean pause = false;}
在最后一个清单文件中:
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.example.androIDmediaplay" androID:versionCode="1" androID:versionname="1.0" > <uses-sdk androID:minSdkVersion="8" androID:targetSdkVersion="17" /> <uses-permission androID:name="androID.permission.READ_PHONE_STATE"/> <application androID:allowBackup="true" androID:icon="@drawable/ic_launcher" androID:label="@string/app_name" androID:theme="@style/Apptheme" > <activity androID:name="com.example.androIDmediaplay.MainActivity" androID:label="@string/app_name" > <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <service androID:name=".BackgroundSoundService" androID:enabled="true" > </service> <receiver androID:name=".call_reciver" > <intent-filter> <action androID:name="androID.intent.action.PHONE_STATE" /> </intent-filter> </receiver> </application></manifest>
所有代码都在我的工作端.请检查并告诉我.
总结以上是内存溢出为你收集整理的Android在来电时关闭媒体服务全部内容,希望文章能够帮你解决Android在来电时关闭媒体服务所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)