关闭应用程序后未触发BroadcastReceiver

问题描述 投票:0回答:1

我正在尝试实现,因此我的应用每天早上6点运行一次代码。当应用处于打开状态且处于前台时,此方法就很好了,但是如果通过滑动将其关闭,则永远不会在适当的时间调用该代码。

AlarmReceiver.java(出于测试目的,我只是尝试显示一个Toast以验证其运行)

public class AlarmReceiver extends BroadcastReceiver {

    public static final String intentAction = "com.mpagliaro98.action.NOTIFICATIONS";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(intentAction)) {
            Toast.makeText(context, "RECEIVER CALLED", Toast.LENGTH_LONG).show();
        }
    }
}

MainActivity.java(正在设置警报的地方)

public class MainActivity extends AppCompatActivity {

    ...

    private void setRecurringAlarm() {
        // Set this to run at 6am
        Calendar updateTime = Calendar.getInstance();
        updateTime.setTimeZone(TimeZone.getDefault());
        updateTime.set(Calendar.HOUR_OF_DAY, 6);
        updateTime.set(Calendar.MINUTE, 0);
        updateTime.set(Calendar.SECOND, 0);
        updateTime.set(Calendar.MILLISECOND, 0);

        // Build the pending intent and set the alarm
        Intent i = new Intent(AlarmReceiver.intentAction);
        PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(),
                0, i, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
        assert am != null;
        am.setRepeating(AlarmManager.RTC, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
    }
}

AndroidManifest.xml(只是相关部分)

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

<receiver
    android:name=".AlarmReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="com.mpagliaro98.action.NOTIFICATIONS" />
    </intent-filter>
</receiver>

我已经在本网站和其他地方阅读了数十个与此类似的问题,但我对无法解决这个问题感到非常困惑。任何帮助,将不胜感激。

android notifications broadcastreceiver alarmmanager
1个回答
0
投票

尝试将接收者更改为

<receiver android:process=":remote" android:name="AlarmReceiver"></receiver>

Should I use android: process =":remote" in my receiver?

© www.soinside.com 2019 - 2024. All rights reserved.