栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

android notification (通知的实现)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android notification (通知的实现)

private NotificationManager manager;
private Notification notification;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.base) {
        //对版本进行判断
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            //id在notification中会用到,name可以随意设置,最后一个为优先级
            NotificationChannel channel = new NotificationChannel("myfirst", "ceshi", NotificationManager.importANCE_HIGH);
            manager.createNotificationChannel(channel);
        }

    }

    Intent intent= new Intent(this,目标activity);
            //将创建的intent转化为pendingintent
            PendingIntent pendingIntent= PendingIntent.getActivity(this,0,intent,0);


    notification = new NotificationCompat.Builder(this, "myfirst")
            .setContentTitle("1")
            .setContentText("wolaile")
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentIntent(pendingIntent)
            .build();
}
//通知发出函数
public void send(View view){
manager.notify(1,notification);
}
//通知取消函数
public void cancel(View view){manager.cancel(1);}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/310155.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号