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

先上布局文件
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中得到view在父容器中的位置下标
  3. Android判断app是否打开消息通知并跳转设置
  4. 基于百度推送android notification的使用之合并通知栏
  5. Android 8.0和8.1通知栏
  6. android 自定义通知消息设置背景色不生效,导致部分机型显示白色字
  7. CCRenderTexture 从后台进入前台变黑的处理(android)
  8. Android获取前台进程包名
  9. Android——自定义通知栏使用

随机推荐

  1. 1.17android小记
  2. The builder launch configuration could
  3. Android使用READ_CONTACTS读取手机联系人
  4. 安卓控件注入
  5. Android程序——人机猜拳
  6. Android(安卓)切屏
  7. EditText禁止复制粘贴
  8. ActivityManagerService中根据UID获取App
  9. Android(安卓)PopupWindows使用
  10. android 图片渐变处理