Android之Notification、NotificationChannel、NotificationCompat.Builder弃用与更新
16lz
2021-01-26
package com.example.notification;import android.app.NotificationChannel;import android.app.NotificationManager;import android.os.Build;import android.support.v4.app.NotificationCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity { Button button1,button2; NotificationManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1=findViewById(R.id.button1); button2=findViewById(R.id.button2); manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //高版本需要渠道 if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){ //只在Android O之上需要渠道 NotificationChannel notificationChannel = new NotificationChannel("channelid1","channelname",NotificationManager.IMPORTANCE_HIGH); //如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道,通知才能正常弹出 manager.createNotificationChannel(notificationChannel); } NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this,"channelid1"); builder.setSmallIcon(R.drawable.ico) .setContentTitle("通知标题") .setContentText("通知内容") .setAutoCancel(true); manager.notify(0x12,builder.build()); } }); }}
转载于:https://www.cnblogs.com/chunshu/p/10317960.html
更多相关文章
- android Notification动态更新
- 如何在Root的手机上开启ViewServer,使得HierachyViewer能够连接
- android 8.0 报错StartForeground Bad Notification Error 解决
- Android中多USB摄像头解决方案——UVCCamera
- Android(安卓)4.4 KitKat NotificationManagerService使用详解与
- Android(安卓)中状态栏(屏幕顶部)消息的显示 Notification
- Android中switch控件默认是开启/关闭以及edittext进行点击时禁止
- 是否应该开启WebView硬件加速?
- android之Notification通知