状态栏提示Notification
16lz
2021-01-26
当有未接电话或者短信时,在手机顶部状态栏就会出现一个小图标,提示用户没有处理的快讯,android提供了NotificationManager来管理状态栏信息,提供Notification处理这些快讯信息
示例
main.xml
main2.xml
Activity.java
示例
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="@string/hello" /><Button android:text="Button01" android:id="@+id/Button01"android:layout_width="wrap_content" android:layout_height="wrap_content"></Button></LinearLayout>
main2.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="多谢使用" /></LinearLayout>
Activity.java
public class Test_Notification extends Activity { /** Called when the activity is first created. */ private Button btn1; private Notification notification; private NotificationManager notificationManager; private Intent intent; private PendingIntent pendIntent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn1 = (Button) this.findViewById(R.id.Button01); notificationManager = (NotificationManager) this .getSystemService(NOTIFICATION_SERVICE);// 获取系统服务(消息管理) // 点击通知时转移内容 intent = new Intent(this, Activity2.class); // 设置点击通知时显示内容的类 // 和Intent不同,构造pendIntent是Nofication.setLastestEventInfo参数需要 pendIntent = PendingIntent.getActivity(this, 0, intent, 0); notification = new Notification(); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub notification.icon = R.drawable.img1;// 设置在状态栏显示的图标 notification.tickerText = "Button1通知内容.......";//设置在状态栏显示的内容 notification.defaults = Notification.DEFAULT_SOUND;// 默认的声音 // 设置通知显示的参数 notification.setLatestEventInfo(Test_Notification.this, "Button1", "Button1通知", pendIntent); notificationManager.notify(0, notification);// 执行通知. } }); } }
更多相关文章
- Android(安卓)沉浸式状态栏归纳
- Android(安卓)自定义控件
- Android学习笔记7&s3c2440学习系列8
- Android(安卓)7.0 Audio :通话中的音频调用接口
- Android实用小技巧(持续更新)
- eclipse 中设置android emulator 选项
- Android(安卓)TextView当中设置超链接、颜色、字体
- 无法在Android(安卓)Studio中设置断点
- Android悬浮通知无效的问题