无法动态注册接收器

XXX*_*XXX 8 android broadcastreceiver

无法在启动时动态注册接收器.我没有活动.而且我不想在服务中注册它.

我注册另一个接收器的Boot Receiver:

package zzz.zzz.zzz;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class AutoStart extends BroadcastReceiver
{   
    @Override
    public void onReceive(Context context, Intent intent)
    {   
        if ((intent.getAction() != null) && (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")))
        {
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            BroadcastReceiver mReceiver = new ScreenReceiver();
            context.registerReceiver(mReceiver, filter);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我要注册的接收者:

package zzz.zzz.zzz;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class ScreenReceiver extends BroadcastReceiver 
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) 
        {
            // some code            
        }
        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) 
        {
            // some code        
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

logcat的:

10-12 15:03:45.849: ERROR/AndroidRuntime(240): Uncaught handler: thread Thread-8 exiting due to uncaught exception
10-12 15:03:45.859: ERROR/AndroidRuntime(240): android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to register to receive intents
10-12 15:03:45.859: ERROR/AndroidRuntime(240):     at android.app.ReceiverRestrictedContext.registerReceiver(ApplicationContext.java:126)
10-12 15:03:45.859: ERROR/AndroidRuntime(240):     at android.app.ReceiverRestrictedContext.registerReceiver(ApplicationContext.java:120)
10-12 15:03:45.859: ERROR/AndroidRuntime(240):     at zzz.zzz.zzz.RegisterReceiver$1.run(RegisterReceiver.java:18)
10-12 15:03:46.159: ERROR/ContactsProvider(98): Cannot determine the default account for contacts compatibility
10-12 15:03:46.159: ERROR/ContactsProvider(98): android.accounts.AuthenticatorException: bind failure
10-12 15:03:46.159: ERROR/ContactsProvider(98):     at android.accounts.AccountManager.convertErrorToException(AccountManager.java:1096)
10-12 15:03:46.159: ERROR/ContactsProvider(98):     at android.accounts.AccountManager.access$500(AccountManager.java:74)
10-12 15:03:46.159: ERROR/ContactsProvider(98):     at android.accounts.AccountManager$BaseFutureTask$Response.onError(AccountManager.java:1003)
10-12 15:03:46.159: ERROR/ContactsProvider(98):     at android.accounts.IAccountManagerResponse$Stub.onTransact(IAccountManagerResponse.java:69)
10-12 15:03:46.159: ERROR/ContactsProvider(98):     at android.os.Binder.execTransact(Binder.java:287)
10-12 15:03:46.159: ERROR/ContactsProvider(98):     at dalvik.system.NativeStart.run(Native Method)
10-12 15:03:46.879: ERROR/MediaPlayerService(31): Couldn't open fd for content://settings/system/notification_sound
10-12 15:03:46.889: ERROR/MediaPlayer(52): Unable to to create media player
Run Code Online (Sandbox Code Playgroud)

Fra*_*nke 13

答案在错误信息中:IntentReceiver components are not allowed to register to receive intents.您无法在现有的BroadcastReceiver中注册新的BroadcastReceiver.


小智 5

我已经在推出此项服务ACTION_BOOT_COMPLETED

@Override
public void onReceive(Context context, Intent intent) {

    Log.d("BootReceiver", "onReceive() intent: " + intent.getAction());

    if (intent.getAction() != null) {

        // Boot completed
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            if(!isMyServiceRunning(context)){
                startTheService(context);
            }
        }
Run Code Online (Sandbox Code Playgroud)

这将在场景后面创建我的应用程序:

public class MyApplication extends Application
Run Code Online (Sandbox Code Playgroud)

在那里我覆盖了onCreate:

@Override
public void onCreate() {
    super.onCreate();
    Log.d("MyApplication", "registering for screen states");
    registerClassBecauseManifestIsNotEnough(getApplicationContext()) ;
}

private void registerClassBecauseManifestIsNotEnough(Context context) {
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver mReceiver = new ScreenStateBroadcastReceiver();       
    context.registerReceiver(mReceiver, intentFilter);
}
Run Code Online (Sandbox Code Playgroud)

至少,问题对我来说已经解决了.