效果图


主界面代码

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代码
        
通知显示代码

                                                                                                                                                                


更多相关文章

  1. 安卓通知栏自定义布局提示(NotificationCompat.Builder)
  2. Java - Android(安卓)自定义控件之圆形进度条
  3. android 自带的日期控件 DatePicker
  4. 安卓自定义简单loading
  5. 自定义View组合模式
  6. java.lang.NoClassDefFoundError: com.jayway.android.robotium.
  7. fill_parent与wrap_content的区别
  8. Android使用摄像头拍照
  9. Android之Notification、NotificationChannel、NotificationComp

随机推荐

  1. ArcGIS Runtime SDKs v10.2.4最新(Android
  2. android 自定义控件之一
  3. Android 8.0的WIFI 架构差异分析
  4. Android中如何修改系统时间(应用程序获得
  5. Android人脸检测方案汇总
  6. Android隐藏标题栏
  7. android adb命令安装和删除apk应用
  8. Android 接口定义语言AIdl
  9. Android Webview upload 图片上传
  10. [AndroidUI]使用DrawerLayout实现侧滑菜