首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法动态注册接收方

无法动态注册接收方
EN

Stack Overflow用户
提问于 2011-10-12 23:44:19
回答 5查看 11.8K关注 0票数 8

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

启动接收器,我在其中注册了另一个接收器:

代码语言:javascript
复制
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);
        }
    }
}

我要注册的接收者:

代码语言:javascript
复制
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        
        }
    }
}

LogCat:

代码语言:javascript
复制
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
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-10-12 23:51:29

答案在错误消息中:IntentReceiver components are not allowed to register to receive intents。您不能在现有BroadcastReceiver中注册新的BroadcastReceiver。

票数 13
EN

Stack Overflow用户

发布于 2012-09-20 23:17:47

我已经在ACTION_BOOT_COMPLETED中启动了该服务

代码语言:javascript
复制
@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);
            }
        }

将在后台创建我的应用程序:

代码语言:javascript
复制
public class MyApplication extends Application

在这里我已经覆盖了onCreate:

代码语言:javascript
复制
@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);
}

至少对我来说问题解决了。

票数 5
EN

Stack Overflow用户

发布于 2011-10-12 23:56:39

你不能把这个登记在货单上吗?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7742776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档