我是android和java的新手。我正在制作一个应用程序,它有一个媒体服务,我希望媒体在来电时停止或暂停。 这是我的媒体服务代码
公共类ServiceMusic扩展了服务{
MediaPlayer music;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
music.stop();
music.release();
}
@Override
public 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);
}
}
如果你能帮助我,我将非常感激,如果你能给我一个很棒的完整指导。 感谢
答案 0 :(得分:4)
您必须为来电创建接收器,如下所示:
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>
让您的媒体播放器变为静态&amp; globale,在reciver中调用另一个服务来检查您的媒体播放器是否正在运行。如果它正在运行然后停止它。你也可以使用相同的服务。
你必须从接收者那里开始服务:
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>
所有代码都在我的最后工作。请检查并告诉我。