使用库项目中的服务和活页夹

时间:2013-07-11 14:16:53

标签: java android android-service android-library android-binder

我目前正在开发广播流式应用。 为了完成这项工作,我决定创建一个图书馆项目来做一些代码重构

所以在我的图书馆项目中我有:

  • StreamingService.java
  • StreamingPlayerMP.java
  • StreamingPlayerAAC.java
  • StreamingCallback.java
  • StreamingNotification.java
  • StreamingState.java
  • StreamingStation.java

我是否需要在库清单应用程序清单中定义服务?

   <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;
}

如果您有完整的解决方案,欢迎您! 建议也欢迎

0 个答案:

没有答案