Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从广播接收器绑定远程服务_Android_Broadcastreceiver - Fatal编程技术网

Android 从广播接收器绑定远程服务

Android 从广播接收器绑定远程服务,android,broadcastreceiver,Android,Broadcastreceiver,我有一个名为StartService的BroadcastReceiver,它在手机启动时启动,然后启动一项服务,但当我从该BroadcastReceiver转到该服务时,我遇到异常: android.content.ReceiverCallNotAllowedException:不允许IntentReceiver组件绑定到服务 我的代码如下: import android.content.BroadcastReceiver; import android.content.ComponentNam

我有一个名为
StartService
BroadcastReceiver,它在手机启动时启动,然后启动一项服务,但当我从该BroadcastReceiver转到该服务时,我遇到异常:

android.content.ReceiverCallNotAllowedException:不允许IntentReceiver组件绑定到服务

我的代码如下:

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步:

public class StartService extends BroadcastReceiver
        {  
            @Override
            public void onReceive(Context context, Intent intent) {
                Intent startServiceIntent = new Intent(context, myServices.class);
                context.startService(startServiceIntent);           
            }    
        } 
// make all service related task
<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>
第二步:

public class StartService extends BroadcastReceiver
        {  
            @Override
            public void onReceive(Context context, Intent intent) {
                Intent startServiceIntent = new Intent(context, myServices.class);
                context.startService(startServiceIntent);           
            }    
        } 
// make all service related task
<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>
第三步:

public class StartService extends BroadcastReceiver
        {  
            @Override
            public void onReceive(Context context, Intent intent) {
                Intent startServiceIntent = new Intent(context, myServices.class);
                context.startService(startServiceIntent);           
            }    
        } 
// make all service related task
<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>

我总是用这种方式接收和服务


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

但我想从此远程服务启动另一个bradcastreciever如何操作(不使用任何活动)。表示自动呼叫广播接收器