0
我已經實現了一個下載管理器,其中顯示導航欄上的完成通知,當用戶觸摸通知時,它會自動啓動我的主要活動。不知道如何返回到以前的應用程序或主屏幕
現在的問題是當我按下主要活動的按鈕時,它返回到我不想發生的以前的活動。
我已經試過代碼:
在主類:
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
這:
@Override
public void onBackPressed() {
super.onBackPressed();
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
這是我的下載接收器代碼:
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent i) {
if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(i.getAction()))
startActivity(new Intent(getApplicationContext(),MainActivity.class));
else if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(i.getAction())) {
Toast.makeText(ctxt,"Done!",Toast.LENGTH_SHORT).show();
Log.i("download status: ", "Download Completed!");
MyNotificationManager.ShowNotification((Activity) ctxt, MainActivity.GetTheActivity(), R.drawable.ic_launcher, "package " + downloadedFileName),
"package" + downloadedFileName + " is downloaded!"), MyNotificationManager.SoundsType.System);
Log.i("download status: ", "Notification shown!");
DoBackgroundDatabaseOperations proceedDownload = new DoBackgroundDatabaseOperations();
proceedDownload.execute();// Sending the package
}
}
};
這是它具有以下方法來顯示我的MyNotificationManager類通知:
public static enum SoundsType
{
System,
My
}
public SoundsType SoundType;
static Uri soundUri;
public static void ShowNotification(Activity curActivity,Activity targetActivity,int icon,String title,String decs,SoundsType sound)
{
if (sound == SoundsType.System)
soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
else if (sound == SoundsType.My)
soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent notificationIntent = new Intent(curActivity, targetActivity.getClass());
PendingIntent contentIntent = PendingIntent.getActivity(curActivity, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(curActivity)
.setSmallIcon(icon)
.setContentTitle(title)
.setContentText(decs)
.setSound(soundUri)
.setAutoCancel(true)
.setContentIntent(contentIntent);
NotificationManager manager = (NotificationManager) curActivity.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
現在我應該怎麼才能妥善處理後退按鈕做到這一點,去之前的應用程序,它可能一直在運行,或如果用戶從那裏啓動我的應用程序的主屏幕?
我知道這是一個多碼問題的問題,但我想這是許多人的一個問題...
那麼你在'onbackpressed'上開始一個新的活動? – johntheripp3r
由於您在點擊通知時直接啓動主要活動,因此不會看到它如何打開以前的活動,因爲它不應該放在堆棧上。 –
@ johntheripp3r好,是的。我應該如何關閉我的活動? –