android通知打开app,Android打开通知栏并回到主页的⼏种⽅盛传
式
⽤Activity创建通知栏
在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⽣成规则
Intent notifyIntent = new Intent();
notifyIntent.tClass(this, NotifyActivity.class);
PendingIntent pendingIntent = Activity(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);
Notification.Builder builder = new Notification.Builder(this, "0")
.tAutoCancel(true)
.tContentTitle(getString(R.string.app_name))
.tContentText("xxx")
.tOnlyAlertOnce(true)
.tSmallIcon(R.mipmap.ic_launcher)
.
tContentIntent(pendingIntent);
notification = builder.build();
} el {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.tSmallIcon(R.mipmap.ic_launcher)
.tContentText("xxx")
.tAutoCancel(true)
.tWhen(System.currentTimeMillis())
.tOnlyAlertOnce(true)
.tContentTitle(getString(R.string.app_name))
.tContentIntent(pendingIntent);
notification = builder.build();
}
}
复制代码
运⾏代码,点击启动通知栏按钮,此时会创建⼀个通知栏,并且2秒后,主页⾃动关闭。然后在点击通知栏,进⼊到通知栏页⾯,点击返回按钮时,发下APP没有回到主页⾯,⽽是回到了Launcher主页⾯。如下⾯截图所⽰
所以⽤Activity⽅式打开通知栏,就会出现上⾯所描述的问题。为了解决这问题,提供了⼀下⼏种⽅式。
⽤Activities创建通知栏
处理逻辑基本上跟上⾯⼀致,只需替换pendingIntent⽣成规则那部分代码,需替换的代码如下⾯所⽰
Intent notifyIntent = new Intent();
Intent mainIntent = new Intent();
notifyIntent.tClass(this, NotifyActivity.class);
caramelomainIntent.tClass(this, MainActivity.class);
//需要注意这⾥的顺序
Intent[] intents = new Intent[]{mainIntent, notifyIntent};
PendingIntent pendingIntent = PendingIntent.
google street view
getActivities(this, 0, intents, PendingIntent.FLAG_UPDATE_CURRENT);
复制代码
汽车拍摄
运⾏代码,如下⾯截图所⽰
此⽅法适⽤于MainActivity和NotifyActivity在同⼀个moudle的情况。如果不在同⼀个moudle⼜该如何处理呢?接着往下看。⽤TaskStackBuilder创建通知栏
替换pendingIntent⽣成规则那部分代码,需替换的代码如下⾯所⽰
Intent notifyIntent = new Intent();
notifyIntent.tClass(this, NotifyActivity.class);
五月飞霜TaskStackBuilder stackBuilder = ate(this);
stackBuilder.addParentStack(NotifyActivity.class);
hikingstackBuilder.addNextIntent(notifyIntent);
PendingIntent pendingIntent = PendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
复制代码
除了替换pendingIntent⽣成规则之外,还需要修改l内的代码,为NotifyActivity指
定parentActivityName属性,如下⾯代码所⽰
android:parentActivityName=".MainActivity"/>
复制代码
连词该属性是在Android 4.1(API level 16)引⼊的,此处的名称必须与为相应元素的android:name属性指定的类名称⼀致,以确定当⽤户按下返回按钮时应该启动哪⼀个Activity。
运⾏代码,效果如图2所⽰
此⽅法可以适⽤于Activity在不同moudle的情况。
但是,当主页MainActivity(这⾥⽤A代表,⽅便后⾯描述)的launchMode设置为singleTask时,当主页A存在时,并且还打开了其他页
⾯'OtherActivity'(B),⽬前Activity的栈的顺序是A、B。当打开⽤Activities和TaskStackBuilder两种⽅式创建的通知栏,页⾯跳转到NotifyActivity(C),并且⼀直按返回键,退栈的顺序是C、A、Launcher,B却没在栈内了,见图3。具体原因是,当打开通知栏是,栈的顺序是A、B、A,由于A的launchMode是singleTask,此时会删除B,当整个通知栏操作全部完成时,Activity的
栈的顺序是A、C,所以会出现上⾯描述的现象。如果要满⾜退栈顺序是C、B、A、Launcher该怎么实现?
⽤Activity创建通知栏,本地维护Activity栈
⾸先需要创建⼀个Activity管理类ActivityManager,来维护Activity栈,如下⾯代码所⽰
public class ActivityManager{
private static final byte[] sLock = new byte[0];
private final Stack mActivityStack = new Stack<>();
private static ActivityManager sInstance; public static ActivityManager getInstance(){ if (sInstance == null) {
synchronized (sLock) {
stand upif (sInstance == null) {
sInstance = new ActivityManager();
}
snake是什么意思
}
}
return sInstance;
}
private ActivityManager(){
}
/**
* activity⼊栈
*/
public void addActivity(Activity activity){ mActivityStack.add(activity);
}
/**
* activity出栈
*/
public void removeActivity(Activity activity){ ve(activity);
}
/**
欧洲victoryday
* 当栈的个数为1的时候,判断cls是否在栈内
*/
public boolean currentActivity(Class> cls){ if (mActivityStack.size() != 1) {
return true;
}
for (Activity activity : mActivityStack) {
if (Class().equals(cls)) {