Android开发之Notification手机状态栏通知用法实例分析-创新互联
本文实例讲述了Android开发之Notification手机状态栏通知用法。分享给大家供大家参考,具体如下:
成都创新互联公司是一家专注于网站制作、成都网站设计与策划设计,崆峒网站建设哪家好?成都创新互联公司做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:崆峒等地区。崆峒做网站价格咨询:18980820575简介:
通知是显示在手机状态栏的通知(PS:就是手机上方,显示时间啥的那一栏)
用法:
Notification添加了Builder()类,其包含如下方法:
1. setDefaults() 通知led灯、音乐、震动等
2. setAutoChange() 设置点击通知后,通知自动从状态栏删除
3. setContentTitle() 通知标题
4. setContentText() 通知内容
5. setSmallcon() 为通知设置图标
6. setLargelcon() 为通知设置大图标
7. setTick() 设置通知状态栏的提示文本
8. setContentIntent()点击通知后要启动的相应组件
运行效果:

实现方法:
1.首先建立一个活动用来执行:
public class MainActivity extends Activity {
static final int NOTIFICATION_ID = 0x123;
NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取系统的Notification对象
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
//为发送通知的按钮点击事件定义事件处理方法
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public void send(View source){
//创建一个其他Activity的Intent
Intent intent = new Intent(MainActivity.this,TextActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
Notification notification = new Notification.Builder(this)
//设置打开通知 通知自动消失
.setAutoCancel(true)
//设置显示状态栏的通知提示信息
.setTicker("注目提醒!")
//设置通知图标
.setSmallIcon(R.drawable.seek02)
//设置通知内容标题
.setContentTitle("该应用发生 爆炸大 大 大 新闻!!")
//设置通知内容
.setContentText("冒险没有 你手机自嗨罢了~")
//设置使用默认的声音 LED灯
.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS)
//设置通知自定义声音
// .setSound()
.setWhen(System.currentTimeMillis())
//设置他只要启动的程序Intent
.setContentIntent(pendingIntent)
.build();
notificationManager.notify(NOTIFICATION_ID,notification);
}
public void del(View view){
//取消通知
notificationManager.cancel(NOTIFICATION_ID);
}
}
本文题目:Android开发之Notification手机状态栏通知用法实例分析-创新互联
文章起源:http://www.jxjierui.cn/article/dhgsoe.html


咨询
建站咨询
