在MainActivity
中增長點擊事件,用來啓動NotifyService
和延遲2秒銷燬MainActivity
,以下面代碼所示java
Intent intent = new Intent(MainActivity.this, NotifyService.class);
startService(intent);
tvTips.postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
}, 2000L);
複製代碼
NotifyService
類繼承IntentService
,並在onHandleIntent()
方法類處理展現通知欄的邏輯,以下面代碼所示android
private void showNotification() {
Notification notification;
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//pendingIntent生成規則
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);
}
複製代碼
運行代碼,點擊啓動通知欄
按鈕,此時會建立一個通知欄,而且2秒後,主頁自動關閉。而後在點擊通知欄
,進入到通知欄頁面
,點擊返回按鈕時,發下APP沒有回到主頁面,而是回到了Launcher主頁面。以下面截圖所示git
因此用PendingIntent.getActivity
方式打開通知欄,就會出現上面所描述的問題。爲了解決這問題,提供了一下幾種方式。github
處理邏輯基本上跟上面一致,只需替換pendingIntent生成規則
那部分代碼,需替換的代碼以下面所示app
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);
複製代碼
運行代碼,以下面截圖所示ide
此方法適用於MainActivity
和NotifyActivity
在同一個moudle的狀況。若是不在同一個moudle又該如何處理呢?接着往下看。post
替換pendingIntent生成規則
那部分代碼,需替換的代碼以下面所示ui
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生成規則
以外,還須要修改AndroidManifest.xml
內的代碼,爲NotifyActivity
指定parentActivityName
屬性,以下面代碼所示this
<activity android:name=".NotifyActivity" android:parentActivityName=".MainActivity"/>
複製代碼
該屬性是在Android 4.1(API level 16)引入的,此處的名稱必須與爲相應<activity>
元素的android:name
屬性指定的類名稱一致,以肯定當用戶按下返回按鈕時應該啓動哪個Activity
。spa
運行代碼,效果如圖2所示
此方法能夠適用於Activity
在不一樣moudle的狀況。
可是,當主頁MainActivity
(這裏用A表明,方便後面描述)的launchMode
設置爲singleTask
時,當主頁A
存在時,而且還打開了其餘頁面'OtherActivity'(B),目前Activity的棧的順序是A、B
。當打開用PendingIntent.getActivities
和TaskStackBuilder
兩種方式建立的通知欄,頁面跳轉到NotifyActivity
(C),而且一直按返回鍵,退棧的順序是C、A、Launcher
,B
卻沒在棧內了,見圖3
。具體緣由是,當打開通知欄是,棧的順序是A、B、A
,因爲A
的launchMode
是singleTask
,此時會刪除B
,當整個通知欄操做所有完成時,Activity的棧的順序是A、C
,因此會出現上面描述的現象。若是要知足退棧順序是C、B、A、Launcher
該怎麼實現?
ActivityManager
,來維護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);
}
/** * 當棧的個數爲1的時候,判斷cls是否在棧內 */
public boolean currentActivity(Class<?> cls) {
if (mActivityStack.size() != 1) {
return true;
}
for (Activity activity : mActivityStack) {
if (activity.getClass().equals(cls)) {
return true;
}
}
return false;
}
}
複製代碼
Activity
的基類BaseActivity
,全部Activity
頁面須要繼承這個基類,而且分別在onCreate
和onDestroy
方法中分別實現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);
}
}
複製代碼
運行代碼,以下面截圖所示