通知适用于应用不在前台运行的状态下,给用户的一种提醒方式。

先上布局文件
activity.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.androidnotification.MainActivity">    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="openNF"        android:text="打开通知" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="sendBR"        android:text="发送广播弹出通知" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="gxNF"        android:text="个性化通知" />LinearLayout>

activity_notifition.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:gravity="center"        android:text="我是由通知打开的"        android:textColor="@android:color/holo_blue_bright"        android:textSize="18sp" />LinearLayout>

MainActivity.class

public class MainActivity extends BaseActivity {    @Override    protected int bindlayout() {        return R.layout.activity_main;    }    public void openNF(View view) {//打开通知        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        Intent intent = new Intent(MainActivity.this, MainActivity.class);        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);        NotificationCompat.Builder compat = new NotificationCompat.Builder(this);        compat.setContentTitle("This is content title!");        compat.setContentText("This is content text!");        compat.setWhen(System.currentTimeMillis());        compat.setContentIntent(pendingIntent);        compat.setSmallIcon(android.R.drawable.sym_action_email);        compat.setLargeIcon(BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_dialog_map));        compat.setAutoCancel(true);//        compat.setSound()设置通知的声音//        compat.setVibrate(new long[]{0,1000,500,1000});设置通知震动下表1表示静止的时长,下表2是震动的时长。(双数下标是静止时长,单数下标是震动时长)//        compat.setLights(Color.RED,1000,1000);设置呼吸灯//        compat.setDefaults(NotificationCompat.COLOR_DEFAULT);根据手机当前环境调节        compat.build();        Notification notification = compat.build();        manager.notify(1, notification);    }    public void sendBR(View view) {//发送广播显示通知        sendBroadcast(new Intent("com.example.androidnotification.receiver.SendBroadcastNotifitionReceiver"));    }    public void gxNF(View view) {        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        NotificationCompat.Builder compat = new NotificationCompat.Builder(this); //必须设置setSmallIcon,否则不显示通知       compat.setSmallIcon(android.R.drawable.stat_notify_sdcard_usb);//        compat.setPriority(NotificationCompat.DEFAULT_ALL);默认的//        compat.setPriority(NotificationCompat.PRIORITY_MIN);最低//        compat.setPriority(NotificationCompat.PRIORITY_LOW);较低//        compat.setPriority(NotificationCompat.PRIORITY_HIGH);较高重要程度的通知//        compat.setPriority(NotificationCompat.PRIORITY_MAX);重要的        Notification notification = compat.build();        manager.notify(3, notification);    }}

广播接受者
SendBroadcastNotifitionReceiver.class

/*发送广播显示通知 */public class SendBroadcastNotifitionReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        intent = new Intent(context, NotifitionActivity.class);        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);        NotificationCompat.Builder compat = new NotificationCompat.Builder(context);        compat.setContentTitle("通知标题");        compat.setContentText("通知的内容");        compat.setSmallIcon(android.R.drawable.stat_notify_more);        compat.setColor(Color.argb(0, 1, 2, 3));        compat.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), 1));        compat.setContentIntent(pendingIntent);        compat.build();        compat.setAutoCancel(true);        Notification notification = compat.build();        manager.notify(2, notification);    }}

通过发送广播来显示的通知,点击界面
NotifitionActivity.class

public class NotifitionActivity extends BaseActivity {    @Override    protected int bindlayout() {//加载布局        return R.layout.activity_notifition;    }}

更多相关文章

  1. Android通知栏图标显示网络图片
  2. Android向Http服务器发送Http请求异常-UnknowHostException
  3. android使用Intent操作拨打号码发送短信
  4. Android(安卓)Socket 编程
  5. Android判断app是否打开消息通知并跳转设置
  6. Android腾讯微博客户端开发四:微博发送篇(QQ表情,@搜索)
  7. android 发送短信sms
  8. Android将发送的短信插入数据库
  9. Android监听应用程序安装和卸载

随机推荐

  1. MYSQL5.7.24安装没有data目录和my-defaul
  2. mysql基础架构教程之查询语句执行的流程
  3. navicat连接mysql出现2059错误的解决方法
  4. MYSQL自定义函数判断是否正整数的实例代
  5. MySQL服务器的SSD性能问题分析和测试详解
  6. MySQL5.7.24版本的数据库安装过程图文详
  7. 最新mysql 5.7.23安装配置图文教程
  8. mysql 5.7.24 安装配置方法图文教程
  9. mysql 5.7.24 安装配置图文教程
  10. 88秒插入1000万条数据到MySQL数据库表的