Java 这是检查服务运行是否真的那么糟糕的方法吗?
在我的mediaplayer应用程序中,我想知道我的服务是否正在运行 我之所以想知道这一点,是因为我只想在我的服务已经运行时绑定它 当打开我的应用程序并单击歌曲时,我调用方法startMusicService,检查服务是否已绑定,如果未绑定,则调用方法bindMusicService 在bindMusicService中,我检查mServiceIsBound是否为真/假,如果为假,我将Bind我的服务,然后调用onServiceConnected 在onServiceConnected中,我检查ServiceReadyRunning是否为false,如果为false,则开始播放我的歌曲 一切都很好,但我相信有更好更有效的方法来实现同样的目标 我这样做是因为如果我绑定我的服务,比如说onCreate,它已经开始播放一首歌而没有选择一首 我只希望在选择要播放的歌曲时启动我的服务Java 这是检查服务运行是否真的那么糟糕的方法吗?,java,android,android-service,android-service-binding,Java,Android,Android Service,Android Service Binding,在我的mediaplayer应用程序中,我想知道我的服务是否正在运行 我之所以想知道这一点,是因为我只想在我的服务已经运行时绑定它 当打开我的应用程序并单击歌曲时,我调用方法startMusicService,检查服务是否已绑定,如果未绑定,则调用方法bindMusicService 在bindMusicService中,我检查mServiceIsBound是否为真/假,如果为假,我将Bind我的服务,然后调用onServiceConnected 在onServiceConnected中,我检查
public void bindMusicService(boolean alreadyRunning){
//boolean alreadyRunning: if True, then we don't call startActionPlay() in onServiceConnected() because Service is already running.
serviceAlreadyRunning = alreadyRunning;
/*mediaPlayerServiceIntent binds our connection to the MediaPlayerService. */
if (!mServiceIsBound) {
try {
mediaPlayerServiceIntent = new Intent(context, MediaPlayerService.class);
context.bindService(mediaPlayerServiceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
} catch (Exception e) {
Log.e("Main", "An exception was caught: Service is not bound!");
}
}
}
public void startMusicService(int songPos, ArrayList<Song> songs){
songIndex = songPos;
songList = songs;
if (mServiceIsBound) {
mediaPlayerService.startActionPlay(context, songList, songIndex);
}else{
bindMusicService(false);
Log.i("Main", "startMusicService(): Service was not bound yet, binding Service...");
}
为什么下面的代码被认为是不好的?许多帖子说,用于检查正在运行的服务的ActivityManager只应用于调试目的,而不应用于发布
在onResume()中,我有以下代码:
if (isMyServiceRunning(MediaPlayerService.class)){
if (!Main.getInstance().mServiceIsBound) {
Main.getInstance().bindMusicService(true);
Log.i(TAG, "Service already running but not bound!");
}
Log.i(TAG, "Service is running!");
}
ISmyService运行方法
public void onServiceConnected(ComponentName name, IBinder service) {
MediaPlayerService.MusicBinder binder = (MediaPlayerService.MusicBinder)service;
mediaPlayerService = binder.getService();
if (!serviceAlreadyRunning) {
mediaPlayerService.startActionPlay(context, songList, songIndex);
}
mServiceIsBound = true;
Intent intent = new Intent(Constants.ACTIONS.BROADCAST_SERVICE_BOUND);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
Log.i("Main","MediaPlayerService is connected!");
}
private boolean isMyServiceRunning(Class<?> serviceClass){
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if (activityManager != null){
for (ActivityManager.RunningServiceInfo serviceInfo: activityManager.getRunningServices(Integer.MAX_VALUE)){
if (serviceClass.getName().equals(serviceInfo.service.getClassName())){
return true;
}
}
}
return false;
}
private boolean IsMyService正在运行(类serviceClass){
ActivityManager ActivityManager=(ActivityManager)getSystemService(Context.ACTIVITY_服务);
如果(activityManager!=null){
对于(ActivityManager.RunningServiceInfo服务信息:ActivityManager.getRunningServices(Integer.MAX_值)){
if(serviceClass.getName().equals(serviceInfo.service.getClassName())){
返回true;
}
}
}
返回false;
}
您是否考虑过以下问题?它完成了构建媒体应用程序的所有步骤。