Notification
==>
Notification是显示在手机状态栏的消息,位于手机屏幕的最上方;
一般显示手机当前网络、电池状态、时间等;
Notification所代表的是一种全局效果的通知,程序一般通过NotificationManager服务来发送Notification。
应用程序可通过NotificationManager向系统发送全局通知;
使用Notification发送Notification,操作步骤:
1.调用getSystemService(NoTIFICATION_SERVICE)方法获取系统的NotificationManager服务;
2.通过构造器创建一个Notification对象;
3.为Notification设置各种属性;
4.通过NotificationManager发送Notification
实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | 布局文件==》 <?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" > <Button android:id= "@+id/btnone" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:text= "one" /> <Button android:id= "@+id/btntwo" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:text= "two" /> </LinearLayout> <?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" > <Button android:id= "@+id/btnone" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:text= "otehr" /> <Button android:id= "@+id/btntwo" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:text= "other" /> </LinearLayout> 代码实现==》 package com.example.mynotification; import android.os.Bundle; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { final int NotificationId = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnone = (Button) this .findViewById(R.id.btnone); Button btntwo = (Button) this .findViewById(R.id.btntwo); btnone.setOnClickListener( new OnClickListener() { @SuppressWarnings( "deprecation" ) @Override public void onClick(View v) { Intent intent = new Intent(MainActivity. this , OtherActivity. class ); PendingIntent pintent = PendingIntent.getActivity(MainActivity. this , 0, intent, 0); // 创建Notification Notification notify = new Notification(); // 为Notification设置图标,该图标显示在状态栏 notify.icon = R.drawable.ele; // 为Notification设置文本内容, 该文本显示在状态栏 notify.tickerText = "启动其他程序通知" ; // 设置Notification发送时间 notify.when = System.currentTimeMillis(); // 为Notification设置默认声音、默认震动、默认闪关灯 notify.defaults = Notification.DEFAULT_SOUND; // 设置事件信息 notify.setLatestEventInfo(MainActivity. this , "Notification通知" , "Notification查看" , pintent); // 获取系统NotificationManager服务 NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.notify(NotificationId,notify); } }); btntwo.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.cancel(NotificationId); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true ; } } package com.example.mynotification; import android.app.Activity; import android.os.Bundle; public class OtherActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.other); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 权限设置、Activity添加==》 <?xml version= "1.0" encoding= "utf-8" ?> <manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "com.example.mynotification" android:versionCode= "1" android:versionName= "1.0" > <uses-sdk android:minSdkVersion= "8" android:targetSdkVersion= "18" /> <!-- 添加操作权限 --> <!-- 添加闪光灯操作权限 --> <uses-permission android:name= "android.permission.FLASHLIGHT" /> <!-- 添加操作振动器的操作权限 --> <uses-permission android:name= "android.permission.VIBRATE" /> <application android:allowBackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/AppTheme" > <activity android:name= "com.example.mynotification.MainActivity" android:label= "@string/app_name" > <intent-filter> <action android:name= "android.intent.action.MAIN" /> <category android:name= "android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name= "com.example.mynotification.OtherActivity" android:label= "other acitivity" /> </application> </manifest> |
注意:
以上程序中粗体内容为Notification设置各种属性,包括图标、标题、发送时间。
也可以通过以下方式实现:Notification notify= new Notification(R.drawable.image,"启动Activity的通知",System.currentTimeMillis());
以上程序,还通过defaults属性Notification设置了声音提示、震动提示、闪光灯等,该属性支持的属性如下所示:
1.DEFAULT_SOUND:设置使用默认声音;
2.DEFAULT_VIBRATE:设置使用默认震动;
3.DEFAULT_LIGHTS:设置使用默认闪光灯;
4.ALL:设置使用默认声音、震动、闪光灯;
如果不想使用默认设置,也可使用代码进行设置:
//设置自定义声音
notify.sound=Uril.parse("file://sdcard//music.mp3");
//设置自定义震动
notify.vibrate = new Long[]{0,50,100,150};
//设置闪光灯颜色为红色
notify.ledARGB=Color.Red;
//设置闪光灯多少毫秒后熄灭
notify.ledoffms=800ms;
//设置闪光灯多少毫秒后开启
notify.ledOnms=800ms;
注意:添加新的Activity需要在AndroidMainfest.xml添加对应配置,需要操作系统设置,也需要在AndroidMainfest.xml添加对应的权限配置才可正常使用。
运行效果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本