状态栏获取信息
16lz
2021-01-26
效果图
主界面代码
package com.xiaoke.notification;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.RemoteViews;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.support.v4.app.NotificationCompat;public class MainActivity extends Activity {private final int NOTIFICATION_ID = 105;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button send = (Button) findViewById(R.id.send);send.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {sendNotification();}});Button clear = (Button) findViewById(R.id.clear);clear.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {clearNotification();}});}private void sendNotification() {// 获取管理工具NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);// 获取管理工具中的通知工具NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);// 设置通知栏样式mBuilder.setSmallIcon(R.drawable.dev);// 系统下拉弹出自带样式// mBuilder.setContentTitle("通知的标题");// mBuilder.setContentText("通知的内容");Notification notification = mBuilder.build();// 找到自定义布局文件RemoteViews rv = new RemoteViews(getPackageName(), R.layout.a_text);// 设置布局文件的内容// rv.setImageViewResource(R.id.image, R.drawable.edn);// rv.setTextViewText(R.id.title, "来消息啦");// rv.setTextViewText(R.id.text, "还不来看我");// 添加进标题栏notification.contentView = rv;// 缺省设置为当发送通知到通知栏时候:提示声音 + 手机震动notification.defaults = Notification.DEFAULT_SOUND| Notification.DEFAULT_VIBRATE;// 要注意的是,作为选项,此处可以设置MainActivity的启动模式为singleTop,避免重复新建onCreate()。// 通知的时间notification.when = System.currentTimeMillis();// 找到要跳转的布局文件Intent intent = new Intent(this, MainActivity.class);PendingIntent pi = PendingIntent.getActivity(this, 0x05, intent,PendingIntent.FLAG_UPDATE_CURRENT);// 添加进标题栏中,点击开始跳转notification.contentIntent = pi; // 点击notification自动消失 notification.flags = Notification.FLAG_AUTO_CANCEL;// 发送到手机的通notificationManager.notify(NOTIFICATION_ID, notification);}// 清除消息private void clearNotification() {NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);notificationManager.cancel(NOTIFICATION_ID);}}
主界面UI代码
通知显示代码
更多相关文章
- 安卓通知栏自定义布局提示(NotificationCompat.Builder)
- Java - Android(安卓)自定义控件之圆形进度条
- android 自带的日期控件 DatePicker
- 安卓自定义简单loading
- 自定义View组合模式
- java.lang.NoClassDefFoundError: com.jayway.android.robotium.
- fill_parent与wrap_content的区别
- Android使用摄像头拍照
- Android之Notification、NotificationChannel、NotificationComp