我有一個接收器,用於在電話屏幕打開或關閉時播放音頻。使應用程序關閉後接收器仍能繼續運行
我的音頻文件發送到接收器這樣
try {
Intent i = new Intent("my.action");
i.putExtra("posLock", newPosition2).putExtra("songlistLock", mySongs).putExtra("lockSound", "lock");
sendBroadcast(i);
}catch (Exception e) {
Log.e(TAG, "Intent error");
}
和
try {
Intent i = new Intent("my.action.unlock");
i.putExtra("posUnlock", newPosition3).putExtra("songlistUnlock", mySongs).putExtra("unlockSound", "unlock");
sendBroadcast(i);
}catch (Exception e) {
Log.e(TAG, "Intent error2");
}
然後我播放音頻在我的接收機類
LockScreenReceiver.java
public class LockScreenReceiver extends BroadcastReceiver {
MediaPlayer mp;
ArrayList<File> mySongs;
ArrayList<File> mySongs2;
Uri u;
Uri u2;
AudioManager am;
private static final String TAG = SecondScreen.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
if(action.equals("my.action")) {
Bundle b = intent.getExtras();
mySongs = (ArrayList) b.getParcelableArrayList("songlistLock");
int position = b.getInt("posLock", 0);
u = Uri.parse(mySongs.get(position).toString());
}
if(action.equals("my.action.unlock")) {
Bundle b = intent.getExtras();
mySongs2 = (ArrayList) b.getParcelableArrayList("songlistUnlock");
int position = b.getInt("posUnlock", 0);
u2 = Uri.parse(mySongs2.get(position).toString());
}
if (action.equals(Intent.ACTION_SCREEN_ON) && am.getRingerMode() == AudioManager.RINGER_MODE_NORMAL)
{
if(u2!=null) {
stopPlaying();
mp = MediaPlayer.create(context, u2);
mp.start();
Toast.makeText(context, "Audio on playing", Toast.LENGTH_SHORT).show();
}
}
else if (action.equals(Intent.ACTION_SCREEN_OFF) && am.getRingerMode() == AudioManager.RINGER_MODE_NORMAL)
{
if(u!=null) {
stopPlaying();
mp = MediaPlayer.create(context, u);
mp.start();
Toast.makeText(context, "Audio off playing", Toast.LENGTH_SHORT).show();
}
}
}
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}
我註冊使用的服務我的接收機
LockScreenService.java
public class LockScreenService extends Service {
BroadcastReceiver receiver;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
@SuppressWarnings("deprecation")
public void onCreate() {
//Start listening for the Screen On, Screen Off, and Boot completed actions
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_BOOT_COMPLETED);
//Set up a receiver to listen for the Intents in this Service
receiver = new LockScreenReceiver();
registerReceiver(receiver, filter);
registerReceiver(receiver, new IntentFilter("my.action"));
registerReceiver(receiver, new IntentFilter("my.action.unlock"));
// Toast.makeText(getApplicationContext(), "Starting service now", Toast.LENGTH_SHORT).show();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}
}
而且我也註冊我的接收器在我的Manifest.xml
的Manifest.xml
<receiver
android:name=".LockScreenReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="my.action" />
<action android:name="my.action.unlock" />
</intent-filter>
</receiver>
從我閱讀時,當接收者通過清單註冊時,即使應用程序終止,它也應該繼續運行。但是當我測試它時,我的手機會在最近的應用程序管理器關閉我的應用程序後停止播放音頻。
你應該實現'onStartCommand'並返回標誌START_STICKY http://developer.android.com/intl/pt-br/reference/android/app/Service.html#START_STICKY –
我曾嘗試過,但它不起作用。 –
,因爲您的MediaPlayer對象需要在服務中,而不是在接收器中 –