如果它在另一个进程中绑定如何绑定?

时间:2017-02-28 17:08:48

标签: android service bind

清单:

def authenticate(self, username, password):

尝试在Activity中绑定服务:

 <service android:name="com.example.MainService" android:process=":main_service"/>

错误:

public class MainActivity extends Activity {
    MainService mMainService;

    private boolean mBound;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        bindService(intentForMainService, mConnection, Context.BIND_AUTO_CREATE)
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className,
                                       IBinder service) {
            MainService.MainServiceBinder binder = (MainService.MainServiceBinder) service;//HERE IS EXCEPTION
            mMainService = (MainService) binder.getService();
            mBound = true;
        }

        public void onServiceDisconnected(ComponentName className) {
            mMainService = null;
            mBound = false;
        }
    };

    @Override
    protected void onStop() {
        doUnbindService();
        super.onStop();
    }

    void doUnbindService() {
        if (mBound) {
          unbindService(mConnection);
        }
    }
}

但当我删除 android:process =&#34;:main_service&#34; 时,一切正常

2 个答案:

答案 0 :(得分:6)

步骤1:编写an AIDL file,描述客户端可以绑定到的服务要导出的接口。出于本答案的目的,我将调用此接口Foo,因此AIDL文件将为Foo.aidl。请注意,如果客户端和服务位于需要相同Foo.aidl内容的单独Android Studio模块中。

步骤2:让服务的活页夹扩展Foo.Stub并覆盖Foo.Stub上的方法,而不是扩展IBinder

步骤3:在您的客户端onServiceConnected()中,通过Foo将原始绑定转换为Foo.Stub.asInterface(service)实例,Foo具有AIDL的客户端-defined API。

This pair of sample projects说明了这一点,在我的情况下,客户端和服务位于不同的应用中。

答案 1 :(得分:0)

我认为如果你这样做会很好

所有人都创造Servic

public class MyService extends Service {

MyReceiver receiver = new MyReceiver();
@Override
public void onCreate() {
    super.onCreate();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    receiver.startAlarm(this);
    return START_STICKY;
}

@Override
public void onStart(Intent intent, int startId) {
    receiver.startAlarm(this);
}

}

启动闹钟后听取警报创建BroadcastReceiver

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) { 
        Log.e("Receiver", "end alarm")
    }
}


public void setAlarm(Context context) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, BootReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 10, pi); // Millisec * Second * Minute
}

public void cancelAlarm(Context context) {
    Intent intent = new Intent(context, BootReceiver.class);
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(sender);
}

添加清单

<service android:name=".MyService"
        android:enabled="true"
        android:process=":my_service"/>

    <receiver
        android:name="MyReceiver"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.parse.push.intent.RECEIVE" />
            <action android:name="com.parse.push.intent.DELETE" />
            <action android:name="com.parse.push.intent.OPEN" />
        </intent-filter>
    </receiver>