【问题标题】:Broadcast Receiver not restarting after reboot重新启动后广播接收器未重新启动
【发布时间】:2015-11-01 11:42:48
【问题描述】:

我正在尝试让我的广播接收器在启动后立即运行。据我所知,无论重新启动广播接收器都会运行,我刚刚了解到,但我的问题是我已将其设置为每晚午夜运行,我不想等到午夜运行一次这违背了目的。但是我需要它在重新启动一次后立即运行。但它没有运行。有人可以看看我做错了什么吗?我正在 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");
}

【问题讨论】:

    标签: android broadcastreceiver


    【解决方案1】:

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

    &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt; 位于 application 标记内,因此无法正常工作。很重要。希望这对将来的其他人有所帮助。因此,请确保您的 XML 标记位于正确的位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多