我是android平台的新手。目前正在开发流媒体广播应用程序。
我想制作应用程序,开始音频播放并继续播放,即使应用程序进入后台。
我已经使用startService()创建了一个服务,因此即使应用程序进入后台,播放也会继续。我还使用了 bindService()来获取 serviceConnection 引用来执行带有服务的 RPC 调用。
应用程序启动服务并正确启动播放,并且 bindService 能够获取引用并能够进行 RPC 调用,一切正常,直到应用程序进入后台。
我在主要活动的onResume()和onPause()中调用bindService()和unBindService()调用。
当应用程序进入后台时,播放继续没有任何问题。unBindservice()调用分离服务连接,并且在 Service 类中调用了onUnbind(Intent)方法。到目前为止没有问题。
当应用程序进入前台时(当服务正在运行并且播放继续时),我没有得到服务连接引用(onServiceConnected()没有被调用),即使在活动onResume()方法中调用了bindService() 。我还验证了服务类onBind(Intent)方法也没有被调用。
在过去的几天里,我一直在努力解决这个问题。
将startService()和bindService()与用户定义的 Android 服务一起使用的推荐方法是什么。
任何帮助解决此问题,高度赞赏。
活动 onResume/onPause 代码。
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate => " + getIntent());
super.onCreate(savedInstanceState);
// set the default network preference, if it is not already set
setDefaultNetwork();
// initialize the navigation tab bars
initializeTabBar();
mPlayerController = AudioPlayerController.getInstance(this);
mActionBar.setDisplayShowHomeEnabled(true);
mActionBar.setDisplayShowTitleEnabled(true);
mRemoteControlReceiver = new RemoteControlReceiver();
IntentFilter filter = new IntentFilter(
"android.intent.action.MEDIA_BUTTON");
registerReceiver(mRemoteControlReceiver, filter);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG,"onResume");
mPlayerController.bindService();
restoreAppState();
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG,"onPause");
mPlayerController.unBindService();
storeAppState();
}
控制器类具有服务相关的启动/停止/绑定/取消绑定方法
public void bindService() {
Intent intent = new Intent();
intent.setClassName("com.vikkrithik.radio.indradio",
"com.vikkrithik.radio.indradio.AudioPlayerService");
Log.d(TAG, "bindService Context " + mPlayerContext
+ " serviceConnection => " + audioServiceConnection);
mPlayerContext.bindService(intent, audioServiceConnection, 0);
}
public void unBindService() {
mPlayerContext.unbindService(audioServiceConnection);
Log.d(TAG, "unBindService done context => " + mPlayerContext
+ " serviceConnection => " + audioServiceConnection);
}
private ServiceConnection audioServiceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "onServiceConnected " + name);
mAudioService = AudioPlayerServiceInterface.Stub
.asInterface(service);
}
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "onServiceDisconnected " + name);
mAudioService = null;
}
};
/**
* Start the AudioService
*
*/
public void startAudioService(Station station) {
Intent intent = new Intent();
intent.setClassName("com.vikkrithik.radio.indradio",
"com.vikkrithik.radio.indradio.AudioPlayerService");
Bundle extras = new Bundle();
extras.putString("station_url", station.getUrl());
extras.putString("station_name", station.getStationName());
intent.putExtras(extras);
mCurrentStation = station;
mPlayerContext.startService(intent);
bindService();
Log.d(TAG, "startService called");
}
public void stopAudioService() {
//
// Check if AudioService is already created
//
if (null == mAudioService)
return;
Intent intent = new Intent();
intent.setClassName("com.vikkrithik.radio.indradio",
"com.vikkrithik.radio.indradio.AudioPlayerService");
mPlayerContext.stopService(intent);
Log.d(TAG, "stopAudioService done");
}
服务类方法
@Override
public IBinder onBind(Intent arg0) {
Log.d(TAG, "onBind invoked");
return audioServiceStub;
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG,"onUnbind invoked");
return super.onUnbind(intent);
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mNetworkStatusReceiver = new NetworkStatusReceiver();
IntentFilter filter = new IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mNetworkStatusReceiver, filter);
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(
mNetworkStatusReceiver);
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestory");
stopPlayback();
PreferenceManager.getDefaultSharedPreferences(this)
.unregisterOnSharedPreferenceChangeListener(
mNetworkStatusReceiver);
unregisterReceiver(mNetworkStatusReceiver);
cancelNotification();
audioServiceStub = null;
super.onDestroy();
}
日志输出
应用启动时
TabbarMainActivity(15432): onResume AudioPlayerController(15432): bindService 上下文 com.vikkrithik.radio.indradio.TabbarMainActivity@4150fdb8 serviceConnection => com.vikkrithik.radio.indradio.AudioPlayerController$1@418a2d30
按下播放按钮时
AudioPlayerController(15432): bindService 上下文 com.vikkrithik.radio.indradio.TabbarMainActivity@4150fdb8 serviceConnection => com.vikkrithik.radio.indradio.AudioPlayerController$1@418a2d30
AudioPlayerController(15432): 调用 startService
AudioPlayerService(15432):onCreate
AudioPlayerService(15432):onBind 调用onBind() 调用
AudioPlayerService(15432): onStartCommand with IndetntIntent { cmp=xxxx (has extras) }
AudioPlayerController(15432):onServiceConnected ComponentInfo{com.vikkrithik.radio.indradio/com.vikkrithik.radio.indradio.AudioPlayerService} onServiceConnected 调用
按下后退按钮时,应用程序进入后台
TabbarMainActivity(15432):完成调用
TabbarMainActivity(15432):onPause
AudioPlayerController(15432): unBindService done context => com.vikkrithik.radio.indradio.TabbarMainActivity@4150fdb8 serviceConnection => com.vikkrithik.radio.indradio.AudioPlayerController$1@418a2d30 onPause() 调用 unBindService()
AudioPlayerService(15432): onUnbind 调用 unBind 在服务中调用
TabbarMainActivity(15432):onDestroy
应用程序进入前台
TabbarMainActivity(15432): onCreate => Intent { cmp=xxxx }
TabbarMainActivity(15432): 从 onResume 调用 onResume bindService
AudioPlayerController(15432): bindService 上下文 com.vikkrithik.radio.indradio.TabbarMainActivity@4150fdb8 serviceConnection => com.vikkrithik.radio.indradio.AudioPlayerController$1@418a2d30
播放停止后
音频播放器服务(15432):停止
AudioPlayerService(15432):重置
AudioPlayerController(15432): stopAudioService 完成
AudioPlayerFragment(15432): 移除可视化器
AudioPlayerService(15432): onDestory