设置闹钟时向接收者发送通知 ID

Send notification id to receiver while set alarm

我在设置闹钟时尝试根据用户的选择设置闹钟,我想将该闹钟的唯一通知 ID 发送到 reciever.java。我想通过这个 id 在 reciever.java 上获取数据。

我有一个表单,其中用户正在添加他们的药丸,我将每个药丸保存在不同的 xml 文件中,例如 pill1.xml pill2.xml。例如,当闹钟时间到来时,保存的药丸会发出警报和通知 pill2.xml 我将在通知栏中显示药丸 2 数据。

底部的代码是我创建警报的方式。

Long alertTime = new GregorianCalendar().getTimeInMillis()+10*1000;
Intent alertIntent = new Intent(ilac_hatirlatma.this, hatirlatma_detay.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent.getBroadcast(ilac_hatirlatma.this, NOTIF_ID, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));

reciever.java

@Override
public void onReceive(Context context, Intent intent) {
    createNotification(context, "İlaç Hatırlatma", "8 saatte bir içmeniz gereken 'Arvelez' adlı ilacınız bulunmaktadır.", "İlacınzı Almayı Unutmayınız!");

}

public void  createNotification(Context context, String msg, String msgText, String msgAlert){

    PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, ilac_hatirlatma.class), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.app_icon).setContentTitle(msg).setTicker(msgAlert).setContentText(msgText);
    mBuilder.setContentIntent(notificIntent);
    mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());

}

这里我准备了闹钟管理器,每天同一时间

alarmManager = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), MyBroadcastReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);

// Set the alarm to start at some time.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        int curHr = calendar.get(Calendar.HOUR_OF_DAY);

        // Checking whether current hour is over 15
        if (curHr >= 15)
        {
            // Since current hour is over 15, setting the date to the next day
            calendar.add(Calendar.DATE, 1);
        }

        calendar.set(Calendar.HOUR_OF_DAY, 15);
        calendar.set(Calendar.MINUTE, 30);


        // every day
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent);

这里我有生成通知的 BroadcastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent)
{
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    //Partial_wake_lock only need CPU active
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "tag");

    //Acquire the lock
    wl.acquire();

    int mId = 0;
    //Show the notification
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_action_edit)
                    .setContentTitle("YOUR APP NAME")
                    .setContentText("TAKE THE PILLS!")
                    .setAutoCancel(true)
                    .setDefaults(-1);

    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(context, MainActivity.class);

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(MainActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);

    NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(mId, mBuilder.build());

    //Release the lock
    wl.release();
}
}

清单的权限

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

以及清单中的 Receiver 声明

<receiver android:name=".MyBroadcastReceiver" />

要设置您的警报管理器,请阅读documentation

如果您需要在 AlarmManager 和 BroadcastReceiver 之间传递数据,请使用 .putExtra() 就像 post 解释的那样 希望这对您有所帮助!