2011-10-12 3 views
8

부팅시 수신기를 동적으로 등록 할 수 없습니다. 나는 아무런 활동도하지 않는다. 그리고 나는 그것을 서비스에 등록하고 싶지 않습니다.수신기를 동적으로 등록 할 수 없습니다.

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); 
     } 
    } 
} 

수신기 I 등록 할 : 나는 다른 수신기를 등록

부팅 수신기

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   
     } 
    } 
} 

로그 캣 :

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 
+0

Havent는 아직 이러한 패키지 이름을 보았습니다! :) – prijupaul

답변

13

대답은 오류 메시지에 있습니다 : IntentReceiver components are not allowed to register to receive intents. 기존 BroadcastReceiver에 새 BroadcastReceiver를 등록 할 수 없습니다.

+0

오 예! 네 말이 맞아! 감사합니다 – XXX

1

매니페스트에 등록 할 수 없습니까? 나는에서 onCreate를 오버라이드 (override)가이

public class MyApplication extends Application 

과 :

+1

번호 매니페스트에서 작동하지 않습니다. http://stackoverflow.com/questions/1588061/android-how-to-receive-broadcast-intents-action-screen-on-off – XXX

+3

BroadcastReceiver를 등록 할 수있는 서비스를 시작할 수 있습니다. context.getApplicationContext()가 작동 할 수도 있습니다. – njzk2

+1

네, 맞습니다. 나는 서비스로이 문제를 해결했다. 감사! – XXX

5

나는 장면 뒤에 내 응용 프로그램을 만들 것입니다

@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); 
      } 
     } 

ACTION_BOOT_COMPLETED에서 서비스를 시작했습니다

@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); 
} 

적어도 나를 위해 해결 된 문제.

1

위젯의 경우 솔루션이 필요했습니다. 우리가 가지고있는 실행기에서

public class BootServiceReceiver extends BroadcastReceiver {  

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // Launcher is a Singleton. 
     // Note that you pass it context.getApplicationContext() as parameter 
     final Launcher launcher=Launcher.get(context.getApplicationContext()); 
     new Thread(new Runnable() { 

      @Override 
      public void run() { 
       launcher.doActions(); 
      } 
     }).start(); 
    } 
} 

:

0

아주 쉬운 해결책이 분리 된 스레드에서 새로운 서비스를 출시 ...

private static boolean isRegisteredFromCode = false; 
    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 

     for (int i = 0; i < appWidgetIds.length; ++i) { 
      RemoteViews layout = buildLayout(context, appWidgetIds[i]); 
      appWidgetManager.updateAppWidget(appWidgetIds[i], layout); 
     } 
     super.onUpdate(context, appWidgetManager, appWidgetIds); 

     if(!isRegisteredFromCode){ 
      isRegisteredFromCode = true; 
      context.getApplicationContext().registerReceiver(this, new IntentFilter(Intent.ACTION_SCREEN_OFF)); 
      context.getApplicationContext().registerReceiver(this, new IntentFilter(Intent.ACTION_SCREEN_ON)); 
     }  
    } 

위젯은 BroadcastReceiver입니다 : 여기에 내가 해결 한 방법입니다 :

public class Launcher { 
    private static Launcher instance; 
    private Context ctx; 
    private BroadcastReceiver receiver1, receiver2; 

    private Launcher(Context ctx) { 
     this.ctx=ctx; 
    } 

    public static Launcher get(Context ctx) { 
     if (yo==null) 
      yo=new Launcher(ctx); 
     return yo; 
    } 

    public void doActions() { 
     receiver1=new Receiver1(); 
     IntentFilter filter1=new IntentFilter(Service1.MY_ACTION); 
     ctx.registerReceiver(receiver1, filter1); 

     Intent i1=new Intent(ctx, Service1.class); 
     ctx.startService(i1); 


     receiver2=new Receiver2(); 
     IntentFilter filter2=new IntentFilter(Service2.MY_ACTION); 
     ctx.registerReceiver(receiver2, filter2); 

     Intent i2=new Intent(ctx, Service2.class); 
     ctx.startService(i2); 
    } 
} 
관련 문제