Android实现仿银行APP回退至后台,并在通知栏里显示

给应用实现仿银行APP回退至后台,在通知栏或状态栏里显示通知提示

使用广播接收形式,实现在通知栏里显示常驻通知:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class LifeCircleReceiver extends BroadcastReceiver {
private NotificationManager notificationManager;
private Activity activity;
private static int notifyId;
private static final int NOTIFY_ID = 10001;

public LifeCircleReceiver(Activity activity) {
this.activity = activity;
}

@Override
public void onReceive(Context context, Intent intent) {
NLog.i(Constants.TAG, "LifeCircleReceiver.onReceive");
notificationManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);

boolean background = intent.getBooleanExtra("background", false);
if (!background) {
if (notifyId > 0) {
notificationManager.cancel(notifyId); //清除ID号为常量notifyId的通知
notificationManager.cancelAll(); //清除全部通知
}
return;
}

String channel = AnalyticsConfig.getChannel(context);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channel);
//PendingIntent 跳转动作
PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, activity.getIntent(), 0);
mBuilder.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(context.getResources().getString(R.string.app_name))
.setContentText(context.getResources().getString(R.string.app_run_background))
.setContentIntent(pendingIntent);
Notification notification = mBuilder.build();
//notification.icon = R.drawable.logo;
//在通知栏上点击此通知后不会自动清除此通知
notification.flags = Notification.FLAG_ONGOING_EVENT;
//设置显示通知时的默认的发声、震动、Light效果
//notification.defaults = Notification.DEFAULT_VIBRATE;
notifyId = NOTIFY_ID;
notificationManager.notify(notifyId, notification);
}

}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :