创建NotificationManager,点击进入ACTIVITY会创建新的activity

创建NotificationManager,点击进入ACTIVITY会创建新的activity

    public void CreateNotification()
    {

        Intent mainIntent = new Intent(mContext, MainActivity.class);

        mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent mainPendingIntent = PendingIntent.getActivity(mContext, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        //获取NotificationManager实例
        NotificationManager notifyManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        //实例化NotificationCompat.Builde并设置相关属性
        NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
                //设置小图标
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                //设置通知标题
                .setContentTitle(mContext.getResources().getString(R.string.app_name))
                //设置通知内容
                .setContentText(mContext.getResources().getString(R.string.running))
                .setOngoing(true)
                .setContentIntent(mainPendingIntent);
        //设置通知时间,默认为系统发出通知的时间,通常不用设置
        //.setWhen(System.currentTimeMillis());
        //通过builder.build()方法生成Notification对象,并发送通知,id=1

        NotificationChannel channel = new NotificationChannel(tag, tag, NotificationManager .IMPORTANCE_LOW);
        builder.setChannelId(tag);
        notifyManager.createNotificationChannel(channel);

        notifyManager.notify(0, builder.build());

    }

    public void CancleNotification()
    {
        NotificationManager notifyManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        notifyManager.cancel(0);
    }

要避免这个问题activity要设置

android:launchMode="singleTask"
<activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:excludeFromRecents="true"
            android:launchMode="singleTask"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

Activity一共有以下四种launchMode:

1.standard

2.singleTop

3.singleTask

4.singleInstance

launchMode 可以查看 https://blog.csdn.net/liuhe688/article/details/6754323/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值