Android打開通知欄并回到主頁的幾種方式

個人博客

用PendingIntent.getActivity創(chuàng)建通知欄

MainActivity中增加點擊事件,用來啟動NotifyService和延遲2秒銷毀MainActivity,如下面代碼所示

Intent intent = new Intent(MainActivity.this, NotifyService.class);
startService(intent);

tvTips.postDelayed(new Runnable() {
    @Override
    public void run() {
        finish();
    }
}, 2000L);

NotifyService類繼承IntentService,并在onHandleIntent()方法類處理展示通知欄的邏輯,如下面代碼所示

private void showNotification() {
    Notification notification;
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    //pendingIntent生成規(guī)則
    Intent notifyIntent = new Intent();
    notifyIntent.setClass(this, NotifyActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, 
        notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("0", "notify",
            NotificationManager.IMPORTANCE_DEFAULT);
        manager.createNotificationChannel(channel);
        Notification.Builder builder = new Notification.Builder(this, "0")
                .setAutoCancel(true)
                .setContentTitle(getString(R.string.app_name))
                .setContentText("xxx")
                .setOnlyAlertOnce(true)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent);
        notification = builder.build();
    } else {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentText("xxx")
                .setAutoCancel(true)
                .setWhen(System.currentTimeMillis())
                .setOnlyAlertOnce(true)
                .setContentTitle(getString(R.string.app_name))
                .setContentIntent(pendingIntent);
        notification = builder.build();
    }
    manager.notify(0, notification);
}

運行代碼,點擊啟動通知欄按鈕,此時會創(chuàng)建一個通知欄,并且2秒后,主頁自動關(guān)閉。然后在點擊通知欄,進(jìn)入到通知欄頁面,點擊返回按鈕時,發(fā)下APP沒有回到主頁面,而是回到了Launcher主頁面。如下面截圖所示

task_stack_builder_1.gif

所以用PendingIntent.getActivity方式打開通知欄,就會出現(xiàn)上面所描述的問題。為了解決這問題,提供了一下幾種方式。

用PendingIntent.getActivities創(chuàng)建通知欄

處理邏輯基本上跟上面一致,只需替換pendingIntent生成規(guī)則那部分代碼,需替換的代碼如下面所示

Intent notifyIntent = new Intent();
Intent mainIntent = new Intent();
notifyIntent.setClass(this, NotifyActivity.class);
mainIntent.setClass(this, MainActivity.class);
//需要注意這里的順序
Intent[] intents = new Intent[]{mainIntent, notifyIntent};
PendingIntent pendingIntent = PendingIntent.
        getActivities(this, 0, intents, PendingIntent.FLAG_UPDATE_CURRENT);

運行代碼,如下面截圖所示

task_stack_builder_2.gif

此方法適用于MainActivityNotifyActivity在同一個moudle的情況。如果不在同一個moudle又該如何處理呢?接著往下看。

用TaskStackBuilder創(chuàng)建通知欄

替換pendingIntent生成規(guī)則那部分代碼,需替換的代碼如下面所示

Intent notifyIntent = new Intent();
notifyIntent.setClass(this, NotifyActivity.class);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(NotifyActivity.class);
stackBuilder.addNextIntent(notifyIntent);

PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

除了替換pendingIntent生成規(guī)則之外,還需要修改AndroidManifest.xml內(nèi)的代碼,為NotifyActivity指定parentActivityName屬性,如下面代碼所示

<activity android:name=".NotifyActivity"
    android:parentActivityName=".MainActivity"/>

該屬性是在Android 4.1(API level 16)引入的,此處的名稱必須與為相應(yīng)<activity>元素的android:name屬性指定的類名稱一致,以確定當(dāng)用戶按下返回按鈕時應(yīng)該啟動哪一個Activity

運行代碼,效果如圖2所示

此方法可以適用于Activity在不同moudle的情況。

但是,當(dāng)主頁MainActivity(這里用A代表,方便后面描述)的launchMode設(shè)置為singleTask時,當(dāng)主頁A存在時,并且還打開了其他頁面'OtherActivity'(B),目前Activity的棧的順序是A、B。當(dāng)打開用PendingIntent.getActivitiesTaskStackBuilder兩種方式創(chuàng)建的通知欄,頁面跳轉(zhuǎn)到NotifyActivity(C),并且一直按返回鍵,退棧的順序是C、A、LauncherB卻沒在棧內(nèi)了,見圖3。具體原因是,當(dāng)打開通知欄是,棧的順序是A、B、A,由于AlaunchModesingleTask,此時會刪除B,當(dāng)整個通知欄操作全部完成時,Activity的棧的順序是A、C,所以會出現(xiàn)上面描述的現(xiàn)象。如果要滿足退棧順序是C、B、A、Launcher該怎么實現(xiàn)?

task_stack_builder_3.gif

用PendingIntent.getActivity創(chuàng)建通知欄,本地維護(hù)Activity棧

  1. 首先需要創(chuàng)建一個Activity管理類ActivityManager,來維護(hù)Activity棧,如下面代碼所示
public class ActivityManager {
    private static final byte[] sLock = new byte[0];

    private final Stack<Activity> mActivityStack = new Stack<>();

    private static ActivityManager sInstance;

    public static ActivityManager getInstance() {
        if (sInstance == null) {
            synchronized (sLock) {
                if (sInstance == null) {
                    sInstance = new ActivityManager();
                }
            }
        }
        return sInstance;
    }

    private ActivityManager() {
    }

    /**
     *  activity入棧
     */
    public void addActivity(Activity activity) {
        mActivityStack.add(activity);
    }

    /**
     *  activity出棧
     */
    public void removeActivity(Activity activity) {
        mActivityStack.remove(activity);
    }

    /**
     *  當(dāng)棧的個數(shù)為1的時候,判斷cls是否在棧內(nèi)
     */
    public boolean currentActivity(Class<?> cls) {
        if (mActivityStack.size() != 1) {
            return true;
        }
        for (Activity activity : mActivityStack) {
            if (activity.getClass().equals(cls)) {
                return true;
            }
        }
        return false;
    }
}
  1. 其次創(chuàng)建一個Activity的基類BaseActivity,所有Activity頁面需要繼承這個基類,并且分別在onCreateonDestroy方法中分別實現(xiàn)Activity的入棧和出棧操作,并且重寫返回事件,如下面代碼所示
public abstract class BaseActivity extends AppCompatActivity {

   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       ActivityManager.getInstance().addActivity(this);
   }

   @Override
   public void onBackPressed() {
       super.onBackPressed();
       if (!ActivityManager.getInstance().currentActivity(MainActivity.class)) {
           Intent intent = new Intent(BaseActivity.this, MainActivity.class);
           startActivity(intent);
       }
   }

   @Override
   protected void onDestroy() {
       super.onDestroy();
       ActivityManager.getInstance().removeActivity(this);
   }
}

運行代碼,如下面截圖所示

task_stack_builder_4.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容