//MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendNotice = (Button) findViewById(R.id.send_notice);
sendNotice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Notification_ID = "com.lk.notificationtest";//必须唯一
//必须写这一步否则无法显示通知(创建通道)
NotificationChannel channel = new NotificationChannel(Notification_ID,"xx",NotificationManager.IMPORTANCE_LOW);
//状态栏通知的管理类,负责发通知、清楚通知等。
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//创建可以发布通知的通道
manager.createNotificationChannel(channel);
//是具体的状态栏通知对象
Notification notification = new NotificationCompat.Builder(MainActivity.this).setChannelId(Notification_ID)
//标题
.setContentTitle("大傻瓜")
//内容
.setContentText("清风不问赶路之人")
//指定通知被创建时间
.setWhen(System.currentTimeMillis())
//设置通知小图标
.setSmallIcon(R.mipmap.ic_launcher)
//通知大图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.build();
//显示通知
manager.notify(1,notification);
}
});
}
}
简单的安卓通知
最新推荐文章于 2022-07-17 14:32:58 发布

2796

被折叠的 条评论
为什么被折叠?



