android错误---Notification用法,注意API版本

今天在公司的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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值