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

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

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

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);
    }

}
上一篇 Android检查手机是否被root
下一篇 Nodejs和Java通过RSA进行签名和验签的两种方式
目录
文章列表
1 OpenSSL生成RSA公私钥
OpenSSL生成RSA公私钥
2
Shell脚本字符串截取的8种方法
Shell脚本字符串截取的8种方法
3
Docker拷贝镜像文件
Docker拷贝镜像文件
4
Laravel中Validator的验证扩展
Laravel中Validator的验证扩展
5
Android集成支付宝SDK支付
Android集成支付宝SDK支付
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。