2015-11-25 45 views
0

我有一个接收器,用于在电话屏幕打开或关闭时播放音频。使应用程序关闭后接收器仍能继续运行

我的音频文件发送到接收器这样

   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> 

从我阅读时,当接收者通过清单注册时,即使应用程序终止,它也应该继续运行。但是当我测试它时,我的手机会在最近的应用程序管理器关闭我的应用程序后停止播放音频。

+0

你应该实现'onStartCommand'并返回标志START_STICKY http://developer.android.com/intl/pt-br/reference/android/app/Service.html#START_STICKY –

+0

我曾尝试过,但它不起作用。 –

+0

,因为您的MediaPlayer对象需要在服务中,而不是在接收器中 –

回答

0

in.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);设置这个标志,然后使用可以在应用程序关闭后运行接收器,这也适用于4.4+。

0

另一种方式利用可以使用的服务,然后使用ontaskremoved方法然后使用代码开始新的活动,则该活动重启接收器或偿还其优良工程这样的代码

public void onTaskRemoved(Intent rootIntent) 
{ 

      Intent intent = new Intent(this, DummyActivity.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(intent); 
      // stopSelf(); 
      super.onTaskRemoved(rootIntent);  
} 
+0

此功能在4.4+版本上正常工作,我也添加了 –

相关问题