Java 在BroadcastReceiver中使用活动中的ArrayList
我在MainActivity中有一个arrayList,我正在使用alarmManager在应用程序关闭时显示通知。我不知道如何在BroadcastReceiver“inside onReceive()方法”中使用该arraylist,并从该arraylist获取数据以在通知中显示它。我还希望能够从通知中已经显示的arraylist中删除元素。 反正这是我的代码,希望你能帮我 -主要活动:Java 在BroadcastReceiver中使用活动中的ArrayList,java,android,android-studio,notifications,broadcastreceiver,Java,Android,Android Studio,Notifications,Broadcastreceiver,我在MainActivity中有一个arrayList,我正在使用alarmManager在应用程序关闭时显示通知。我不知道如何在BroadcastReceiver“inside onReceive()方法”中使用该arraylist,并从该arraylist获取数据以在通知中显示它。我还希望能够从通知中已经显示的arraylist中删除元素。 反正这是我的代码,希望你能帮我 -主要活动: NotificationManager mNotificationManager; int NOTIFI
NotificationManager mNotificationManager;
int NOTIFICATION_ID = 0;
private static final String ACTION_NOTIFY = "com.example.android.standup.ACTION_NOTIFY";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent notifyIntent = new Intent(ACTION_NOTIFY);
final PendingIntent notifyPendingIntent = PendingIntent.getBroadcast
(this, NOTIFICATION_ID, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
final AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
ToggleButton toggleButton = findViewById(R.id.alarmToggle);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b){
long triggerTime = SystemClock.elapsedRealtime()+10000;
long repeatInterval = 180000;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerTime, repeatInterval, notifyPendingIntent);
}else {
mNotificationManager.cancelAll();
alarmManager.cancel(notifyPendingIntent);
}
}
});
}
AlarmReceiver类别:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent contentIntent = new Intent(context, MainActivity.class);
PendingIntent contentPendingIntent = PendingIntent.getActivity
(context, createID(), contentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Hello")
.setContentText("Notification Text")
.setContentIntent(contentPendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
notificationManager.notify(createID(), builder.build());
}
public int createID(){
Date now = new Date();
int id = Integer.parseInt(new SimpleDateFormat("ddHHmmss", Locale.US).format(now));
return id;
}
}
谢谢大家! 你熟悉通知操作吗?我刚开始学习通知,所以我不熟悉