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
原创粉丝点击