Android 在应用关闭后使应用在后台运行

Android 在应用关闭后使应用在后台运行,android,audio,Android,Audio,我正在构建一个应用程序,在手机屏幕打开或关闭时播放音频 下面是我播放音频的方式 public class LockScreenReceiver extends BroadcastReceiver { MediaPlayer mp; ArrayList<File> mySongs; ArrayList<File> mySongs2; Uri u; Uri u2; AudioManager am; private static final String TAG = Seco

我正在构建一个应用程序,在手机屏幕打开或关闭时播放音频

下面是我播放音频的方式

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();

        }
    }


}
我想让我的应用程序能够播放音频,即使从任务管理器关闭应用程序。但“开始”似乎不起作用


我错过什么了吗?我真的需要使用SharedReference吗?因为音乐文件是从用户的设备存储器中提取的。

在onDestroy ya中销毁服务。你能发布代码吗?我不明白你的意思(
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();
}