2016-09-18 37 views
1

我用这个权限:的Android后重新启动广播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作为广播接收器接收引导事件的意图。本的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> 
相关问题