android 如何打开活动中对应的位置点击时弹出的通知?

9vw9lbht  于 2022-11-03  发布在  Android
关注(0)|答案(1)|浏览(184)

我有一个包含笔记的RecyclerView,当我添加新笔记时,我想为该特定笔记添加提醒,但当出现通知并单击它时,我想导航到该活动并打开该笔记。通知工作正常,但我想要的是,当我单击通知打开该笔记时,我在其上设置提醒。
报警接收器类:

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Intent i = new Intent(context, ChecklistChildActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "alarmChannel")
            .setContentTitle("title")
            .setContentText("text")
            .setAutoCancel(true)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setSmallIcon(R.drawable.alarm)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentIntent(pendingIntent);

    NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context);
    managerCompat.notify(123, builder.build());

}

}
setAlarm方法:

private void setAlarm() {

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR, hour);
    calendar.set(Calendar.MINUTE, minute);

    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    Toast.makeText(this, "Alarm is set", Toast.LENGTH_SHORT).show();
}

我在RecyclerAdapter中有一个时钟接口,它是在MainActivity中实现的:
@Override public void onNoteClicked(整数位置,查看项视图){

Intent intent = new Intent(this, NoteDetail.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("id", noteAdapter.itemList.get(position).getId());
        intent.putExtra("title", noteAdapter.itemList.get(position).getTitle());
        intent.putExtra("content", noteAdapter.itemList.get(position).getContent());
        intent.putExtra("date", noteAdapter.itemList.get(position).getDate());
        intent.putExtra("backgroundColor", noteAdapter.itemList.get(position).getBackgroundColor());

        startActivity(intent);
    }

当我点击通知的时候,这个方法应该被调用。当我添加一个注解的时候,我不知道它会去哪个位置,以便传递意图,虽然如果我知道位置,当我添加或删除另一个注解的时候,位置可能会改变。

vyswwuz2

vyswwuz21#

太好了,我有个主意,我假设您的Activity从Intent获取ID和其他数据。因此,我们必须将数据放入警报Intent,然后从AlarmReceiver读取它们,并将它们再次放入Activity Intent。

private void setAlarm() {

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR, hour);
    calendar.set(Calendar.MINUTE, minute);

    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmReceiver.class);
    intent.putExtra("id", "myId");
    intent.putExtra("title", "my title");

    pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    Toast.makeText(this, "Alarm is set", Toast.LENGTH_SHORT).show();
}

并从Receiver类中获取:

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Intent i = new Intent(context, ChecklistChildActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.putExtra("id", intent.getStringExtra("id"));
    i.putExtra("title", intent.getStringExtra("title"));
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "alarmChannel")
        .setContentTitle("title")
        .setContentText("text")
        .setAutoCancel(true)
        .setDefaults(NotificationCompat.DEFAULT_ALL)
        .setSmallIcon(R.drawable.alarm)
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setContentIntent(pendingIntent);

    NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context);
    managerCompat.notify(123, builder.build());

}

这是一种简单的方法,但我建议在Activity中使用数据库,并在onCreate方法中使用id获取注解详细信息,因此您只需在Intent中添加id,并在www.example.com类中添加get queryNoteDetail.java。

相关问题