恢复应用程序时使用服务恢复音乐

时间:2017-12-07 13:49:52

标签: android service

我正在开发一个应用程序,它通过使用服务在后台播放音乐。 当我们回击应用程序时音乐会停止暂停,但是当我回到应用程序时音乐没有恢复。

public class backService extends Service implements ComponentCallbacks2 {
    private MediaPlayer mp;
    SharedPreferences sharedpreferences;
    public Boolean musicSwitch;

 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
 }

 @Override
 public void onDestroy() {
 super.onDestroy();
 if (mp != null){
        mp.stop();
        mp.release();
  }
}

@Override
public void onCreate() {
    super.onCreate();
    sharedpreferences = getSharedPreferences(mypreference,
            Context.MODE_PRIVATE);
    musicSwitch = sharedpreferences.getBoolean("music", true);
    if(musicSwitch){
        mp = MediaPlayer.create(this, R.raw.all);
        mp.setLooping(true);
        mp.start();
    }
}



@Override
public void onTrimMemory(final int level) {
    if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
        if(mp != null){
            mp.pause();
        }
    }
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}
    }

我希望应用程序在返回应用程序时恢复音乐,我尝试使用onResume方法,但服务中没有onResume方法。 TIA

1 个答案:

答案 0 :(得分:0)

1)您需要创建前台服务以防止它被操作系统杀死 How can we prevent a Service from being killed by OS?

2)您可以绑定服务(bindService(serviceIntent))并使用Binder接口 https://developer.android.com/guide/components/bound-services.html#Binder

public class LocalService extends Service {
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
// Random number generator
private final Random mGenerator = new Random();

/**
 * Class used for the client Binder.  Because we know this service always
 * runs in the same process as its clients, we don't need to deal with IPC.
 */
public class LocalBinder extends Binder {
    LocalService getService() {
        // Return this instance of LocalService so clients can call public methods
        return LocalService.this;
    }
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

/** method for clients */
public int getRandomNumber() {
  return mGenerator.nextInt(100);
}
}

的活动:

public class BindingActivity extends Activity {
LocalService mService;
boolean mBound = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

@Override
protected void onStart() {
    super.onStart();
    // Bind to LocalService
    Intent intent = new Intent(this, LocalService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onStop() {
    super.onStop();
    unbindService(mConnection);
    mBound = false;
}

/** Called when a button is clicked (the button in the layout file attaches to
  * this method with the android:onClick attribute) */
public void onButtonClick(View v) {
    if (mBound) {
        // Call a method from the LocalService.
        // However, if this call were something that might hang, then this request should
        // occur in a separate thread to avoid slowing down the activity performance.
        int num = mService.getRandomNumber();
        Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
    }
}

/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
            IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        LocalBinder binder = (LocalBinder) service;
        mService = binder.getService();
        mBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;
    }
}
}

3)然后,在您的活动onResume中,您可以通过Binder调用方法来控制音乐