Android 安卓:警报有时不会触发

Android 安卓:警报有时不会触发,android,broadcastreceiver,android-service,alarmmanager,Android,Broadcastreceiver,Android Service,Alarmmanager,我有一个应用程序,它使用AlarmManager服务安排一些活动。它在大多数情况下都能工作,但有时它不发送广播,因此广播接收器不会接收任何东西(即使应用程序已启动并正在运行且处于前台活动状态)。我从时间选择器小部件读取报警时间。 以下是我的代码片段: 设置报警: private void setAlarm(int hour, int minute) { //define intent and parameters Intent intent = new Intent(getApp

我有一个应用程序,它使用AlarmManager服务安排一些活动。它在大多数情况下都能工作,但有时它不发送广播,因此广播接收器不会接收任何东西(即使应用程序已启动并正在运行且处于前台活动状态)。我从时间选择器小部件读取报警时间。 以下是我的代码片段:

设置报警:

private void setAlarm(int hour, int minute) {
    //define intent and parameters
    Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
    intent.putExtra(TAG_NAME, name);
    intent.putExtra(TAG_ADDRESS, address);
    intent.putExtra(TAG_AVERAGE, average);
    intent.putExtra(TAG_DATABASE_ID, dbID);
    //define alarm pedning intent
    PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(),(int)dbID,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
    //calculate alarm  time
    Calendar currentTimeCalendar = Calendar.getInstance();


    currentTimeCalendar.set(Calendar.HOUR_OF_DAY, hour);
    currentTimeCalendar.set(Calendar.MINUTE, minute);
    currentTimeCalendar.set(Calendar.SECOND, 0);

    int h = currentTimeCalendar.get(Calendar.HOUR_OF_DAY);
    int m = currentTimeCalendar.get(Calendar.MINUTE);
    int s= currentTimeCalendar.get(Calendar.SECOND);
    Toast.makeText(getApplicationContext(), "Hour "+h+":"+m+":"+s, Toast.LENGTH_LONG).show();


    SimpleDateFormat dateFormat = new SimpleDateFormat("DD-MMM-yyyy hh:mm:ss");
    String date = dateFormat.format(currentTimeCalendar.getTime());
    Log.v("VSB_TAG","Alarm Time: "+date);
    // set alarm
    AlarmManager alarmMgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmMgr.set(AlarmManager.RTC_WAKEUP, currentTimeCalendar.getTimeInMillis(), alarmIntent);

}
public class AlarmReceiver extends BroadcastReceiver {
    public AlarmReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Log.v("VHB_TAG", "Alarm Received");
        if (bundle == null)
            return;
        String name = bundle.getString(StudentDetailsActivity.TAG_NAME);
        String address = bundle.getString(StudentDetailsActivity.TAG_ADDRESS);
        double average = bundle.getDouble(StudentDetailsActivity.TAG_AVERAGE);
        long dbID = bundle.getLong(StudentDetailsActivity.TAG_DATABASE_ID);
        Toast.makeText(context, "Alarm for " + name, Toast.LENGTH_LONG).show();

        displayStudentNotification(context, name, address, dbID,bundle);

    }

    private void displayStudentNotification(Context context, String name, String address, long dbID, Bundle bundle) {
        ....
        ....
    }

}
广播接收器:

private void setAlarm(int hour, int minute) {
    //define intent and parameters
    Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
    intent.putExtra(TAG_NAME, name);
    intent.putExtra(TAG_ADDRESS, address);
    intent.putExtra(TAG_AVERAGE, average);
    intent.putExtra(TAG_DATABASE_ID, dbID);
    //define alarm pedning intent
    PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(),(int)dbID,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
    //calculate alarm  time
    Calendar currentTimeCalendar = Calendar.getInstance();


    currentTimeCalendar.set(Calendar.HOUR_OF_DAY, hour);
    currentTimeCalendar.set(Calendar.MINUTE, minute);
    currentTimeCalendar.set(Calendar.SECOND, 0);

    int h = currentTimeCalendar.get(Calendar.HOUR_OF_DAY);
    int m = currentTimeCalendar.get(Calendar.MINUTE);
    int s= currentTimeCalendar.get(Calendar.SECOND);
    Toast.makeText(getApplicationContext(), "Hour "+h+":"+m+":"+s, Toast.LENGTH_LONG).show();


    SimpleDateFormat dateFormat = new SimpleDateFormat("DD-MMM-yyyy hh:mm:ss");
    String date = dateFormat.format(currentTimeCalendar.getTime());
    Log.v("VSB_TAG","Alarm Time: "+date);
    // set alarm
    AlarmManager alarmMgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmMgr.set(AlarmManager.RTC_WAKEUP, currentTimeCalendar.getTimeInMillis(), alarmIntent);

}
public class AlarmReceiver extends BroadcastReceiver {
    public AlarmReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Log.v("VHB_TAG", "Alarm Received");
        if (bundle == null)
            return;
        String name = bundle.getString(StudentDetailsActivity.TAG_NAME);
        String address = bundle.getString(StudentDetailsActivity.TAG_ADDRESS);
        double average = bundle.getDouble(StudentDetailsActivity.TAG_AVERAGE);
        long dbID = bundle.getLong(StudentDetailsActivity.TAG_DATABASE_ID);
        Toast.makeText(context, "Alarm for " + name, Toast.LENGTH_LONG).show();

        displayStudentNotification(context, name, address, dbID,bundle);

    }

    private void displayStudentNotification(Context context, String name, String address, long dbID, Bundle bundle) {
        ....
        ....
    }

}
这是舱单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.evegroup.studentslist">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".StudentDetailsActivity" />

        <receiver
            android:name=".AlarmReceiver"
            android:enabled="true"
            android:exported="true"></receiver>
    </application>

</manifest>

由于您的问题是间歇性的,因此问题可能不在您显示的代码中

如果设置了报警,则会触发报警,因此您需要检查设置是否正确或是否符合预期


您也可以在以后使用另一个设置覆盖您想要的报警。

所以您说我发布的代码的总体逻辑似乎很好?我没有检查它,但您说代码有时会工作,所以您的问题更可能是代码的使用,而不是代码本身。