详解Android中Notification的使用方法
作者:cjjky 时间:2023-02-07 13:17:39
在消息通知的时候,我们经常用到两个控件Notification和Toast。特别是重要的和需要长时间显示的信息,用Notification最合适不过了。他可以在顶部显示一个图标以标示有了新的通知,当我们拉下通知栏的时候,可以看到详细的通知内容。
最典型的应用就是未看短信和未接来电的显示,还有QQ微信,我们一看就知道有一个未接来电或者未看短信,收到QQ离线信息。同样,我们也可以自定义一个Notification来定义我们自己的程序想要传达的信息。
Notification我把他分为两种,一种是默认的显示方式,另一种是自定义的,今天为大家讲述默认的显示方式:
1、程序框架结构图如下
2、布局文件 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:gravity="center"
android:textColor="#EEE"
android:textStyle="bold"
android:textSize="25sp"
android:text="NotificationDemo实例" />
<Button
android:id="@+id/btnSend"
android:text="send notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
3、MainActivity.java源码如下:
package com.andyidea.notification;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btnSend;
//定义BroadcastReceiver的action
private static final String NotificationDemo_Action = "com.andyidea.notification.NotificationDemo_Action";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSend = (Button)findViewById(R.id.btnSend);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(NotificationDemo_Action);
sendBroadcast(intent);
}
});
}
}
4、布局文件 secondlayou.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:gravity="center"
android:textColor="#EEE"
android:textStyle="bold"
android:textSize="25sp"
android:text="显示通知界面" />
<Button
android:id="@+id/btnCancel"
android:text="cancel notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
5、SecondActivity.java源码如下:
package com.andyidea.notification;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SecondActivity extends Activity {
private Button btnCancel;
//声明Notification
private Notification notification;
//声明NotificationManager
private NotificationManager mNotification;
//标识Notification的ID
private static final int ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondlayout);
btnCancel = (Button)findViewById(R.id.btnCancel);
//怎样获得NotificationManager的实例?
String service = NOTIFICATION_SERVICE;
mNotification = (NotificationManager)getSystemService(service);
//获得Notification的实例
notification = new Notification();
//设置该图标 会在状态栏显示
int icon = notification.icon = android.R.drawable.stat_sys_phone_call;
//设置提示信息
String tickerText = "Test Notification";
//设置显示时间
long when = System.currentTimeMillis();
notification.icon = icon;
notification.tickerText = tickerText;
notification.when = when;
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "消息", "SMS Android", pi);
mNotification.notify(ID, notification);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mNotification.cancel(ID); //--->取消通知
}
});
}
}
6、NotificationReceiver.java源码如下:
package com.andyidea.notification;
import com.andyidea.notification.SecondActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//实例化Intent
Intent i = new Intent();
//在新任务中启动Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//设置Intent启动的组件名称
i.setClass(context, SecondActivity.class);
//启动Activity,显示通知
context.startActivity(i);
}
}
7、程序运行效果如下:
标签:Android,Notification
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#中参数个数可变的方法实例分析
2022-12-16 20:02:32
轻松实现Android语音识别功能
2022-09-11 17:42:46
springboot+rabbitmq实现智能家居实例详解
2022-09-05 12:23:39
![](https://img.aspxhome.com/file/2023/0/76480_0s.jpg)
Netty分布式从recycler对象回收站获取对象过程剖析
2021-08-12 06:45:48
Java jvm中Code Cache案例详解
2022-02-04 17:00:53
Android自定义漂亮的圆形进度条
2023-08-04 04:23:06
![](https://img.aspxhome.com/file/2023/8/138128_0s.gif)
Java的设计模式编程中迪米特法则的应用示例
2022-10-29 01:34:35
C#实现跨线程操作控件方法
2023-06-30 13:47:39
Android实现录音方法(仿微信语音、麦克风录音、发送语音、解决5.0以上BUG)
2023-02-27 09:48:51
![](https://img.aspxhome.com/file/2023/5/120155_0s.gif)
Spring Aop 源码增强获取分享
2023-06-22 21:59:22
![](https://img.aspxhome.com/file/2023/5/108415_0s.webp)
java实现冒泡排序算法
2023-10-17 20:44:01
![](https://img.aspxhome.com/file/2023/1/69681_0s.gif)
Android开发跳转应用市场进行版本更新功能实现
2021-12-25 09:24:39
Java实战之医院管理系统的实现
2022-04-13 17:39:27
![](https://img.aspxhome.com/file/2023/2/82382_0s.jpg)
Android基准配置文件Baseline Profile方案提升启动速度
2022-08-12 18:54:42
![](https://img.aspxhome.com/file/2023/9/91019_0s.jpg)
Mybatis-plus自动填充不生效或自动填充数据为null原因及解决方案
2021-08-16 02:18:42
java笔记学习之操作符
2022-10-19 05:31:59
java 如何给对象中的包装类设置默认值
2022-02-09 21:04:08
C# XML字符串包含特殊字符的处理转换方法小结
2022-08-17 07:21:24
![](https://img.aspxhome.com/file/2023/3/96363_0s.jpg)
Spring Security自定义认证逻辑实例详解
2023-02-28 19:19:18
![](https://img.aspxhome.com/file/2023/8/72418_0s.png)
IntelliJ IDEA的代码搁置功能实现
2022-03-28 07:32:52
![](https://img.aspxhome.com/file/2023/1/74921_0s.png)