从broadcastreceiver绑定远程服务

时间:2013-03-28 08:37:26

标签: android broadcastreceiver

我有一个名为StartService BroadcastReceiver ,它会在手机启动时启动,然后启动服务,但是当我从 BroadcastReceiver 启动服务时我得到了

android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to bind to services 

例外

我的代码如下:

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;

public class StartService extends BroadcastReceiver {
        private RemoteServiceConnection conn = null;
        private IMyRemoteService remoteService;
        @Override
        public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Intent startServiceIntent = new Intent(context, RemoteServiceClient.class);
                                    context.startService(startServiceIntent);
                                    conn = new RemoteServiceConnection();


                                        Intent i = new Intent();
                                        i.setClassName("com.collabera.labs.sai", "com.collabera.labs.sai.RemoteService");
                                        context.bindService(i, conn, Context.BIND_AUTO_CREATE);


                                }

                                class RemoteServiceConnection implements ServiceConnection {
                                    public void onServiceConnected(ComponentName className, 
                                        IBinder boundService ) {
                                      remoteService = IMyRemoteService.Stub.asInterface((IBinder)boundService);
                                      Log.d( getClass().getSimpleName(), "onServiceConnected()" );
                                    }

                                    public void onServiceDisconnected(ComponentName className) {
                                      remoteService = null;
                                       Log.d( getClass().getSimpleName(), "onServiceDisconnected" );
                                    }
                                };

                            }

1 个答案:

答案 0 :(得分:0)

接下来的一些步骤。

第1步:

public class StartService extends BroadcastReceiver
        {  
            @Override
            public void onReceive(Context context, Intent intent) {
                Intent startServiceIntent = new Intent(context, myServices.class);
                context.startService(startServiceIntent);           
            }    
        } 

第2步:

// make all service related task

第3步:

<service
            android:name=".myServices"
            android:enabled="true" />
        <receiver android:name="com.yourpackage.StartService " >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

我总是以这种方式使用BroadcastReceiver和服务。

可能对您有所帮助。如果有帮助,请接受答复。