2015-11-01 293 views
2

我试图让我的广播接收器在启动后立即运行。据我所知,广播接收机无论重新启动都会运行,而我刚刚了解到这一点,但我的问题是,我已将它设置为每天晚上在午夜运行,而且我不想等到午夜才运行一次。这打破了目的。但是我需要它在重新启动后立即运行。但它没有运行。有人可以看到我是否做错了什么?我正在Galaxy S4和S6上尝试此操作,并且未收到日志消息,指出它已重新启动。重新启动后广播接收器不能重新启动

这是我的Manifest文件,你可以看到它是必要的权限。

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<receiver 
     android:name=".StartActivityAtBootReceiver" 
     android:enabled="true" > 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
</receiver> 

在我StartActivityAtBootReceiver我有跟的onReceive调用的主要活动将开始广播接收机。

public void onReceive(Context context, Intent intent) { 
    Log.e("LOGS", "Start Activity after Rebooted "); 
    Intent rebootIntent = new Intent(context, MainActivity.class); 
    rebootIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(rebootIntent); 
} 

在我的MainActivity我这就要求广播接收器在扩展广播接收器另一大类下面。

//run from boot or from button on screen 
protected void runFromOutside() throws ParseException { 
    checkIfStartingNow(); 
    startTheClock(); 
    finish(); //close the app/view 
} 

//check if starting now pops up message to state that it is staring now 
protected void checkIfStartingNow() throws ParseException { 
//does some checks and displays a message popup 
} 

    protected void startTheClock() { 

    // Set the alarm to run at midnight every night 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 
    calendar.set(Calendar.HOUR_OF_DAY, 00); 
    //calendar.set(Calendar.MINUTE, 01); 
    // Get the AlarmManager Service 
    mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

    // Create an Intent to broadcast to the Shhh 
    mNotificationReceiverIntent = new Intent(MainActivity.this, Shhh.class); 

    // Create an PendingIntent that holds the NotificationReceiverIntent 
    // mNotificationReceiverPendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, mNotificationReceiverIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    mNotificationReceiverPendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, mNotificationReceiverIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    //Set repeating alarm that checks every minute. 
    mAlarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, mNotificationReceiverPendingIntent); 

    // set true for alarm 
    settings.edit().putBoolean(NotificationOn, true).apply(); 
    Log.e("LOGS", "Entered Start the midnight alarm"); 
} 

回答

1

行,所以我想通了,它不工作的原因是因为<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />不是在Android清单文件中的正确位置,所以最后我不得不把它移到了application标签之外(放置它的正确位置),然后重新启动工作。因此,请确保您的Android Manifest文件中有正确的XML布局,因为我学会了艰难的方式,因为它可能会在您的应用程序中造成严重破坏。

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />位于application标记内,因此它无法工作。很重要。希望这有助于未来的其他人。所以请确保您的XML标签位于正确的位置。