android错误---Notification用法,注意API版本
来源:互联网 发布:新浪用户数据库价格 编辑:程序博客网 时间:2024/05/17 02:17
今天在公司的bugly上看的一个问题就是notification的错误提示android4.0的notification没有build方法,所以就了解了下。这里和大家分享下。
按步骤来
1、获取Notification管理器
// NotificationManager 是一个系统Service,必须通过 getSystemService()方法来获取。 NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2、建一个Notification,设置状态栏显示样式
这里我们就要分版本了
首先是API>16的
notification=new Notification.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("通知") .setContentText("你的余额已不足!") .setContentIntent(pendingIntent) //setContentIntent定义点击通知跳转 .build();
然后是11
Notification.Builder builder = new Notification.Builder(MainActivity.this) .setAutoCancel(true) .setContentTitle("title") .setContentText("describe") .setContentIntent(pendingIntent) .setSmallIcon(R.mipmap.ic_launcher) .setWhen(System.currentTimeMillis()) .setOngoing(true); notification=builder.getNotification();
然后是API小于11的
Notification notification = new Notification( R.mipmap.ic_launcher, "This is ticker text", System.currentTimeMillis());
但是分三部实在是太麻烦了所以给大家另一个方案是简化api<16的,这个方法是api>4 api<16都可以用,(至于API<4,拜托就不用考虑的了吧大哥)
NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setTicker("This is ticker text") .setWhen(System.currentTimeMillis()); notification =builder.getNotification(); //调用builder.getNotification()来生成Notification
贴出实例代码
package com.example.admin.apinotification;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Build;import android.os.Bundle;import android.support.v4.app.NotificationCompat;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity { private Button btn_click; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_click = (Button) findViewById(R.id.btn_click); btn_click.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { nation(); } }); } public void nation(){// NotificationManager 是一个系统Service,必须通过 getSystemService()方法来获取。 NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //通知的点击跳转的 Intent intent =new Intent(MainActivity.this,MainActivity.class); //创建PendingIntent对象 PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, 0); Notification notification = null; if(Build.VERSION.SDK_INT >=16) { notification=new Notification.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("通知") .setContentText("你的余额已不足!") .setContentIntent(pendingIntent) //setContentIntent定义点击通知跳转 .build(); }else if (Build.VERSION.SDK_INT < 16 && Build.VERSION.SDK_INT >11 ){ Notification.Builder builder = new Notification.Builder(MainActivity.this) .setAutoCancel(true) .setContentTitle("title") .setContentText("describe") .setContentIntent(pendingIntent) .setSmallIcon(R.mipmap.ic_launcher) .setWhen(System.currentTimeMillis()) .setOngoing(true); notification=builder.getNotification(); }else if (Build.VERSION.SDK_INT < 16 && Build.VERSION.SDK_INT>4){ NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setTicker("This is ticker text") .setWhen(System.currentTimeMillis()); notification =builder.getNotification(); //调用builder.getNotification()来生成Notification }// 执行状态栏的公布方法 nm.notify(1,notification); }}
0 0
- android错误---Notification用法,注意API版本
- Notification用法,注意API版本,API大于11的setLatestEventInfo()方法已经不适用
- android编译时Notification版本引起的错误
- Android API guide Notification
- Android Notification 用法
- android notification 的用法
- android notification的用法
- Android Notification用法
- Android基础-Notification用法
- Android通知Notification用法
- Android Notification 用法
- android Notification简单用法
- android Notification的实例与Notification API
- android api分析34 Notification
- android之Notification版本兼容性问题
- Android关于notification的在不同API下的用法说明
- Android API Level在11前后及16之后时Notification的不同用法
- Android中Notification的用法
- Java实现--登录和注册案例(把用户信息存进集合)
- 【学习笔记15】java面向对象-匿名内部类
- 设计模式之6 -- 建造者模式
- 设计模式之7 -- 外观模式
- JVM深入浅出(四) -- Java内存分配机制
- android错误---Notification用法,注意API版本
- 转眼就毕业了
- Fiddler抓取移动端HTTPS数据包
- 设计模式之8 -- 适配器模式
- hexo 写博客+部分常用markdown语法
- 设计模式之9--装饰者模式
- 命令模式--把需求记录在案,让事务灵活可变(行为模式02)
- 设计模式之10--桥接模式
- 设计模式之11--组合模式