奥利奥:停止通知在滑动时被移除
创始人
2024-09-03 07:30:20
0

你可以通过以下代码示例来解决在滑动时移除通知的问题:

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;

public class MainActivity extends AppCompatActivity {

    private static final String CHANNEL_ID = "notification_channel";
    private static final int NOTIFICATION_ID = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        createNotificationChannel();

        // 创建自定义布局的通知
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
        
        // 设置通知中的内容和样式
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_notification)
                .setContent(remoteViews)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setAutoCancel(false);

        Notification notification = builder.build();

        // 显示通知
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, notification);
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "Notification Channel";
            String description = "Description";
            int importance = NotificationManager.IMPORTANCE_HIGH;

            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);

            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }
}

上述代码中:

  1. 首先,我们使用createNotificationChannel()方法来创建通知渠道,这是Android 8.0(API level 26)及以上版本所必需的。

  2. 然后,我们创建一个自定义布局的通知RemoteViews,你可以在R.layout.custom_notification_layout中定义自己的布局。

  3. 接下来,我们使用NotificationCompat.Builder来设置通知的内容和样式。我们指定了通知图标、自定义布局以及通知的优先级和自动取消行为。

  4. 最后,我们使用NotificationManager来显示通知,并指定了通知的ID为NOTIFICATION_ID

通过以上代码,你可以在滑动时阻止通知被移除,只有当用户点击通知或者你手动取消通知时,通知才会被移除。

相关内容

热门资讯

透视插件!wepoker可以设... 透视插件!wepoker可以设置盖牌-关于开挂透视辅助教程(有挂教学)透视插件!wepoker可以设...
最新技巧“浙江游戏大厅修改器”... 最新技巧“浙江游戏大厅修改器”原先有开挂辅助挂(确实有挂);1、这是跨平台的浙江游戏大厅修改器黑科技...
透视挂透视!hhpoker可以... 透视挂透视!hhpoker可以开挂-揭露开挂透视辅助攻略(有挂攻略)1、hhpoker可以开挂系统规...
玩家必看分享“小程序微乐游戏辅... 玩家必看分享“小程序微乐游戏辅助器”其实有开挂辅助器(详细教程);1、让任何用户在无需AI插件第三方...
透视工具!wepoker国外版... 透视工具!wepoker国外版透视-教你开挂透视辅助工具(了解有挂)1、全新机制【wepoker国外...
专业讨论“欢乐游戏城破解版内置... 专业讨论“欢乐游戏城破解版内置修改器”本来有开挂辅助脚本(了解有挂);详细欢乐游戏城破解版内置修改器...
一分钟教你“宝宝游戏辅助”好像... 您好:宝宝游戏辅助这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...
透视最新!hhpoker透视挂... 透视最新!hhpoker透视挂实战视频-解谜开挂透视辅助攻略(有挂技术)1、超多福利:超高返利,海量...
9分钟了解“四川家园游戏辅助器... 9分钟了解“四川家园游戏辅助器平台交易”果然有开挂辅助下载(有挂规律);亲真的是有正版授权,小编(透...
透视科技!wepoker智能辅... 透视科技!wepoker智能辅助插件-教你开挂透视辅助插件(有挂方针)1、进入到wepoker智能辅...