2016-09-18 9 views
1

를 실행하지 않는 한 나는이 권한 사용 :안드로이드 재부팅 방송 reciver입니다이

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

및 수신기는 다음과 같습니다

<receiver android:name=".auth.NotificationBroadcast" android:enabled="true" > 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
</receiver> 

및 코드에서 수신기는 다음과 같습니다 재부팅 후

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

     System.out.println("BroadcastReceiverBroadcast--------------------ReceiverBroadcastReceiverBroadcastReceiver----------------BroadcastReceiver"); 

     if (intent != null) { 
      String action = intent.getAction(); 

     switch (action) { 
      case Intent.ACTION_BOOT_COMPLETED: 
       System.out.println("Called on REBOOT"); 
       // start a new service and repeat using alarm manager 

       break; 
      default: 
       break; 
     } 
    } 
} 

여전히 롤리팝에 불려 가지 않고 있지만, 마시맬로에서는 달리고 있습니다.

답변

1

이 라인을 수신기의 인 텐트 필터에 넣으십시오.

<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" /> 

응용 프로그램이 SD 카드에 설치되어있는 경우, 당신은 android.intent.action.BOOT_COMPLETED 이벤트를 얻을이를 등록해야합니다.

업데이트 : 앱이 알람 서비스를 사용하고 있으므로 외부 저장소에 설치해서는 안됩니다. 참조 : http://developer.android.com/guide/topics/data/install-location.html

0

플랫폼 부팅이 완료 될 때마다 android.intent.action.BOOT_COMPLETED 동작의 인 텐트가 브로드 캐스팅됩니다. 이 의도를 받으려면 신청서를 등록해야합니다. 등록을 위해 AndroidManifest.xml에이 내용을 추가하십시오.

<receiver android:name=".ServiceManager"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
</receiver> 

따라서 부트 이벤트의 의도를 받기 위해 ServiceManager를 브로드 캐스트 리시버로 사용하게됩니다. 우리가 서비스를 시작하고 있기 때문에

public class ServiceManager extends BroadcastReceiver { 

    Context mContext; 
    private final String BOOT_ACTION = "android.intent.action.BOOT_COMPLETED"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
       // All registered broadcasts are received by this 
     mContext = context; 
     String action = intent.getAction(); 
     if (action.equalsIgnoreCase(BOOT_ACTION)) { 
         //check for boot complete event & start your service 
      startService(); 
     } 

    } 


    private void startService() { 
       //here, you will start your service 
     Intent mServiceIntent = new Intent(); 
     mServiceIntent.setAction("com.bootservice.test.DataService"); 
     mContext.startService(mServiceIntent); 
    } 
} 

, 그것은 너무 AndroidManifest를 언급해야합니다 :

<service android:name=".LocationService"> 
    <intent-filter> 
     <action android:name="com.bootservice.test.DataService"/> 
    </intent-filter> 
</service> 
다음과 같이 ServiceManager의 클래스는 다음과 같다