我正在创建一个带有背景音乐的游戏应用程序.我使用Android服务播放背景音乐,因为我想在更改活动时运行BGM.我的问题是,我已经在每个活动中的onPause方法中声明了finish()(我不想让用户返回并想要杀死活动).
所以当我打算进行其他活动时,它会调用onDestroy并停止服务.我想停止服务完全退出应用程序(按主页按钮)并想要通过BGM和onPause()中的finish()进行活动.这可能吗?还是有另一种解决方案?
public class BackgroundMusicService 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, R.raw.topbgm);
player.setLooping(true); // Set looping
player.setVolume(100, 100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
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() {
player.stop();
player.release();
Log.i("service", "service killed");
}
@Override
public void onLowMemory() {
}
}
Run Code Online (Sandbox Code Playgroud)
在清单中
<service android:name=".BackgroundMusicService" android:process=":remote" />
Run Code Online (Sandbox Code Playgroud)
kyo*_*ogs 16
把这一行放在yout活动中
stopService(new Intent(this, BackgroundMusicService.class));
Run Code Online (Sandbox Code Playgroud)
在onDestroy()方法中,您按下主页按钮.
| 归档时间: |
|
| 查看次数: |
20966 次 |
| 最近记录: |