我目前正在开发广播流式应用。 为了完成这项工作,我决定创建一个图书馆项目来做一些代码重构
所以在我的图书馆项目中我有:
我是否需要在库清单和应用程序清单中定义服务?
<service
android:name=".StreamingService"
android:enabled="true" >
</service>
我是否需要同时在库清单和应用清单中设置权限?
目前,服务/播放器/回调已准备就绪。我想通过 ServiceConnection 和绑定管理主要活动的服务:
public void bindToService() {
Intent intent = new Intent(getApplicationContext(), StreamingService.class);
if (((TestingApplication) getApplication()).MediaPlayerServiceRunning()) {
Log.i(TAG, "bindToService--");
// Bind to LocalService
bindService(intent, _connectionStreaming, Context.BIND_AUTO_CREATE);
}
else {
Log.i(TAG, "bindToService--startService--");
startService(intent);
boolean isBound = bindService(intent, _connectionStreaming, Context.BIND_AUTO_CREATE);
Log.d(TAG, "IS SERVICE BOUND--" + isBound);
}
}
private ServiceConnection _connectionStreaming = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder serviceBinder) {
Log.d(TAG, "onServiceConnected--");
// service
MediaPlayerBinder binder = (MediaPlayerBinder) serviceBinder;
TestingApplication._mediaPlayerService = binder.getServiceInstance();
// callback
TestingApplication._mediaPlayerService.setInitCallBack(MainActivity.this);
TestingApplication._mediaPlayerService.initializePlayer(getString(R.string.url_podcast_mp3_test));
_bound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
Log.d(TAG, "onServiceDisconnected--");
_bound = false;
}
};
活页夹方法(位于服务中):
private final Binder _binder = new MediaPlayerBinder();
public class MediaPlayerBinder extends Binder {
public StreamingService getServiceInstance() {
Log.i(TAG, "getServiceInstance--");
return StreamingService.this;
}
}
@Override
public IBinder onBind(Intent arg0) {
Log.i(TAG, "onBind--");
return _binder;
}
该服务是一个前台服务:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand--");
_runningCallBack.onBufferingStarted();
return START_STICKY;
}
如果您有完整的解决方案,欢迎您! 建议也欢迎