2

我目前正在开发一个无线电流应用程序。为了完成这项工作,我决定创建一个库项目来进行一些代码重构

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

  • 流服务.java
  • StreamingPlayerMP.java
  • StreamingPlayerAAC.java
  • StreamingCallback.java
  • StreamingNotification.java
  • StreamingState.java
  • StreamingStation.java

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

   <service
        android:name=".StreamingService"
        android:enabled="true" >
    </service>

我是否也需要在库清单应用程序清单中都设置权限?

目前 Service/players/callback 已经可以使用了。我想使用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;
}

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

4

0 回答 0