Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.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 - Fatal编程技术网

Android 处理广播接收机

Android 处理广播接收机,android,Android,我有一个应用程序,它有MainActivity,但没有gui。。请随波逐流: 现在,此MainActivity正在运行服务,此服务使用sendBroadcast以便与MainActivity通信。。 现在我当然需要在主活动的onResume中注册Receiver 但我还需要在MainActivity的onDestroy中添加unregisterReceiverreceiver 问题是:当我第一次启动该应用程序时,我需要它来启动服务,并且我不希望用户失去焦点,所以我在启动服务后按finish。。但

我有一个应用程序,它有MainActivity,但没有gui。。请随波逐流:

现在,此MainActivity正在运行服务,此服务使用sendBroadcast以便与MainActivity通信。。 现在我当然需要在主活动的onResume中注册Receiver

但我还需要在MainActivity的onDestroy中添加unregisterReceiverreceiver

问题是:当我第一次启动该应用程序时,我需要它来启动服务,并且我不希望用户失去焦点,所以我在启动服务后按finish。。但随后自动调用 未注册的接管人。。这对我不好。。我发现一个错误,它说找不到任何寄存器和接收器

所以我通过删除这一行来修复它。。但我相信它会在未来“重新发现”我,如果我不使用未注册的ReceiverReceiver,什么时候/哪里会有问题。。在onDestroy

mybe我应该删除它而不是删除它以暂停吗

谢谢


ray.

尝试在onStart中调用bindService,在onStop中调用unbindService,在onResume中调用RegisterReceiver,在onPause中调用unregisterReceiver

你可以看看我的一段工作代码。也许它能帮你找到解决办法

在实际项目中,我评论如下:

bindservicenewintentset1.this、MyService.class、mServiceConnection、Context.BIND\u AUTO\u CREATE

解除绑定服务连接

以及所有mServiceConnection声明

我这么做是因为它实际上是一个从系统启动时开始的后台服务。否则,不应对其进行评论

@Override
public void onStart() {
    super.onStart();
    bindService(new Intent(test1.this, MyService.class), mServiceConnection, Context.BIND_AUTO_CREATE);
    Intent serviceIntent = new Intent(this, MyService.class);
    getApplicationContext().startService(serviceIntent);
    Log.d(TAG, "onStart");
}

@Override
public void onStop() {
    super.onStop();
    unbindService(mServiceConnection); 
}

@Override
public void onResume(){
    super.onResume();
    registerReceiver(receiver, new IntentFilter(MyService.BROADCAST_ACTION));
}

@Override
public void onPause() {
    super.onPause();
    unregisterReceiver(receiver);
}

private BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onRecieve");
        constantCursor.requery(); // service finished its job, refresh ListView         
    }
};



private ServiceConnection mServiceConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mService = ((MyService.LocalBinder)service).getService();
        Log.d(TAG, "onServiceConnected");
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        // This is called when the connection with the service has been
        // unexpectedly disconnected -- that is, its process crashed.
        mService = null;
        Log.d(TAG, "onServiceDisconnected");
    }

};

问题是我使用registerReceiver。。仅在恢复和完成时;在onCreate的末尾。。在任何情况下,我们都无法做到这一点。我需要先注册一下。这就是我得到的:主要活动$1@4359dab0原来是在这里注册的。你错过了一个打给取消注册接收者的电话吗?