Android(安卓)之通知Notification应用
16lz
2022-05-27
通知适用于应用不在前台运行的状态下,给用户的一种提醒方式。
先上布局文件
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; }}
更多相关文章
- Android通知栏图标显示网络图片
- Android向Http服务器发送Http请求异常-UnknowHostException
- android使用Intent操作拨打号码发送短信
- Android(安卓)Socket 编程
- Android判断app是否打开消息通知并跳转设置
- Android腾讯微博客户端开发四:微博发送篇(QQ表情,@搜索)
- android 发送短信sms
- Android将发送的短信插入数据库
- Android监听应用程序安装和卸载