是否可以在不将活动绑定服务的情况下使用AIDL

时间:2014-10-02 15:28:43

标签: android android-service aidl

我已经实现了and aidl文件,如下所示:

interface ITaskService{
void addGetNamesJob();
void addUploadJob();
}

然后我在服务中做了以下事情:

@Override
public IBinder onBind(Intent intent) {
    return new ITaskService.Stub() {
        @Override
        public void addBeerNamesJob() throws RemoteException {
            jobManager.addJobInBackground(new GetNamesTask());
        }

    @Override
    public void addUploadJob() throws RemoteException {
        jobManager.addJobInBackground(new UploadRatingTask(wifi, twitter, TWITTER_CONSUMER, TWITTER_CONSUMER_SECRET, /*ctx,*/ accessToken, accessTokenSecret ));
    }
};
}

然后在我的主要活动中,服务绑定如下:

private void initService() {
        Log.i(TAG, "initService()" );
        mServiceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                service = ITaskService.Stub.asInterface((IBinder)iBinder);
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                bound = false;
                service = null;
            }
        };
        if(service == null){
            Intent it = new Intent();
            it.setAction("com.company.taskservice");
            bindService(it, mServiceConnection, Context.BIND_AUTO_CREATE);
            bound = true;
        }
    }

    private void releaseService() {
        if(bound && mServiceConnection!=null && service!=null){
            unbindService(mServiceConnection);
        }
        bound = false;
        service = null;
        Log.d(TAG, "releaseService(): unbound.");
    }

这一切都很完美,我可以通过界面调用服务中的方法。但是,我现在要做的是在设备启动时启动服务,我这样编写:

public class DeviceBootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            Intent ratingUpload = new Intent(context, RatingUploaderService.class);
            //Toast.makeText(context, "------------->>>>DEVICE BOOT RECEIVER CALLED ---- CONNECTED", Toast.LENGTH_LONG).show();
            context.startService(ratingUpload);
        }
    }
}

如果应用程序是A.发送到后台或B.已杀死,我不希望服务死亡。因此,如果可以在启动时启动服务,它将独立于应用程序运行。但是,我可以通过我的aidl文件访问没有绑定的方法,如果是这样,有人可以告诉我如何?

提前致谢

0 个答案:

没有答案