Android - 从活动外部调用功能(通过AlarmManager和通知)
好吧,我有一个名为'Main.java'的主要活动。这个主要活动启动一个AlarmManager,它触发一个导致'AlarmReceiver.java'的意图。 这个'AlarmReceiver.java'然后创建一个通知,其上有两个按钮。其中一个按钮是删除按钮,所以当用户点击该按钮时,会触发另一个意图,将其导向“DelPair.java”。Android - 从活动外部调用功能(通过AlarmManager和通知)
在DelPair.java中,我修改了数据库中的表格,但是后来我需要Main.java的UI来反映这种更改。我已经创造了Main.java两个函数updateArrayFromDB()和updateUIFromArray()为我做到这一点:
- updateArrayFromDB()将在Main.java创建一个ArrayList同步到一个数据库中的 某个表。
- updateUIFromArray()将更改 Main.java的UI以表示刚刚更改的ArrayList。
问题是我无法从DelPair.java调用这两个函数(它们不存在于该空间中)。我遇到了Serializables试图找到一个答案,但我不知道它们是否适用于此处,或者确切地说,如何通过AlarmManager 和 NotificationManager实现它们。
如何从DelPair.java访问这些方法?
在Main.java:
public void updateArrayFromDB(){
//... The code for this is long and irrelevant
}
public void updateUIFromArray(){
//... The code for this is long and irrelevant
}
private void SendNotification() {
Intent intent = new Intent(this, AlarmReceiver.class);
//...
PendingIntent sender = PendingIntent.getBroadcast(this, 2 , intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, 5000, notif_freq, sender);
}
在AlarmReceiver.java:
Intent delPairI = new Intent(context, DelPair.class);
PendingIntent delPairPI = PendingIntent.getService(context, 0, delPairI, PendingIntent.FLAG_UPDATE_CURRENT);
Notification noti;
noti = new Notification.Builder(context)
//...
.addAction(R.drawable.ic_delete_icon, "Delete the thing", delPairPI)
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
,然后在DelPair.java:
public class DelPair extends IntentService {
//...
@Override
protected void onHandleIntent(final Intent intent) {
//...
Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
getApplicationContext().sendBroadcast(it);
handler.post(new Runnable() {
@Override
public void run() {
//... here is where I update the database, which works perfectly
//now need to update the UI and array in Main.java
updateArrayFromDB(); //these lines
updateUIFromArray(); //obviously don't work
}
});
}
}
为什么不使用广播?在onHandleIntent只需发送一个广播
Intent i = new Intent();
i.setAction(CUSTOM_INTENT);
//put relevant data in intent
getApplicationContext().sendBroadcast(i);
广播接收器:
public class IncomingReceiver extends BroadcastReceiver {
private MainActivity act;
public IncomingReceiver(MainActivity main){
this.act = act;
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(CUSTOM_INTENT)) {
System.out.println("GOT THE INTENT");
// call the method on act
}
}
}
在你活动的onResume - 注册新IncomingReceiver,在onPause注销
private IncomingReceiver receiver;
public void onCreate(Bundle bOs){
//other codes
receiver = new IncomingReceiver(this);
}
@Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction(CUSTOM_INTENT);
registerReceiver(receiver, filter);
super.onResume();
}
@Override
protected void onPause() {
unregisterReceiver(receiver);
super.onPause();
}
我只是阐述了这个答案。我认为那是他需要的。 Upvoted –
既然你需要有一个更新的用户界面根据数据库更改,您可以在您的活动的onResume()
方法中调用updateArrayFromDB()
和updateUIFromArray()
,以便每次更新UI用户输入活动的时间。
您可以尝试在'onResume()'中放置'updateArrayFromDB()'和'updateUIFromArray()'的代码,这样每次用户输入活动时都会调用它。 – Shaishav
这可能只是工作。让我稍微回到你身边。你可以把它作为答案吗? – Beyarkay