我正在为我的学校编写一个Android应用程序,我对通知有一点问题: 我的应用程序检查主页是否在后台更新(页面日期比最后一个日期的日期更新)。如果页面已更新,应用程序会构建一个通知(直到这里工作正常)有2个选项:Dismiss或show(也有效)。 “显示”工作正常,但我的问题是“解雇”,因为它必须更新设置中的日期,然后它应该退出,但它总是显示最后打开的活动。
我正在使用Intent extras管理它的Activity:
public class ***********Handler extends Activity {
String EXTRA_ACTION = "de.xorg.*****.ACTION";
String EXTRA_VPDATE = "de.xorg.*****.VPDATE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ntd);
Intent intent = getIntent();
String todo = intent.getStringExtra(EXTRA_ACTION); //Action from Notification ("D" or "S")
String date = intent.getStringExtra(EXTRA_VPDATE); //New date
if(todo.toLowerCase().contains("d")) {
//update date value
Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putString("readDate", date);
editor.commit();
// Remove notification
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(0);
// Quit app (1st method) [I'm only using one in real code, but both aren't working]
android.os.Process.killProcess(android.os.Process.myPid());
// Quit app (2nd method)
finish();
} else {
//update date value
Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putString("readDate", date);
editor.commit();
// Remove notification
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(0);
//Set theme (not important now)
Boolean BeanUI = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("bean", false);
String URL;
if(BeanUI) {
URL = "*****" + PreferenceManager.getDefaultSharedPreferences(this).getString("klasse", "") + "&fc=ffffff&bc=000000";
} else {
URL = "*****" + PreferenceManager.getDefaultSharedPreferences(this).getString("klasse", "");
}
String EXTRA_URL = "de.xorg.*****.MESSAGE";
String EXTRA_NAME = "de.xorg.*****.MESSAGENAME";
Intent intent2 = new Intent(this, InternetViewer.class);
intent2.putExtra(EXTRA_URL, URL);
intent2.putExtra(EXTRA_NAME, ",Vertretungsplan");
startActivity(intent2);
}
}
}
这是创建通知的代码:
// I've censored all private values with ***'s
// CheckService.MC = Context of main activity
// CheckService.NM = NotificationManager
public static void PostNotification(String text, String datum) {
Intent intentS = new Intent(CheckService.MC, ***************Handler.class);
Intent intentD = new Intent(CheckService.MC, ***************Handler.class);
String EXTRA_ACTION = "de.xorg.*****.ACTION";
String EXTRA_DATE = "de.xorg.*****.VPDATE";
intentS.putExtra(EXTRA_ACTION, "S");
intentS.putExtra(EXTRA_DATE, datum);
intentD.putExtra(EXTRA_ACTION, "D");
intentD.putExtra(EXTRA_DATE, datum);
PendingIntent pIntentD = PendingIntent.getActivity(CheckService.MC, 0, intentD, 0);
PendingIntent pIntentS = PendingIntent.getActivity(CheckService.MC, 0, intentS, 0);
// Build notification
Notification n = new Notification.Builder(CheckService.MC)
.setContentTitle("Title")
.setContentText(text)
.setSmallIcon(R.drawable.vertretung)
.setContentIntent(pIntentS)
.setAutoCancel(true)
.addAction(R.drawable.anzeigen, "Show", pIntentS)
.addAction(R.drawable.gelesen, "Dismiss", pIntentD).build();
CheckService.NM.notify(0, n);
}
我尝试使用»finish()
;«和»android.os.Process.killProcess(android.os.Process.myPid());
«两者都不起作用。
为什么不起作用?是否有更新(更好)的方式来更新设置?