我是Android初学者,在看第一行代码学习自学时,其中通知用到NotificationCompat()这个V4库中的方法。但是在实际使用时发现书上的代码已经过时并且Android8.0已经不支持这种写法,经过几天的研究终于写出一个可以同时兼容Android8.0及以下的通知写法再此把我的研究过程写给大家。
·查找的资料
代码过时第一想到的就是看源码的替代代码是什么,所以我贴上源码的截图参考
仔细看一下官方的说明
通过百度在一篇文章中详细介绍了channel的用法及其特性,我贴出地址
http://www.jianshu.com/p/92afa56aee05
我按照文章中的方法创建了channel并在NotificationCompat中使用,但是在模拟器中出现了BUG 界面出错并且抽风。
贴出错误代码
String id = "channel_1";
String description = "123";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(id, "123", importance);
// mChannel.setDescription(description);
// mChannel.enableLights(true);
// mChannel.setLightColor(Color.RED);
// mChannel.enableVibration(true);
// mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotificationManager.createNotificationChannel(mChannel);
Notification notification = new Notification.Builder(this, id)
.setContentTitle("Title")
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentTitle("您有一条新通知")
.setContentText("这是一条逗你玩的消息")
.setAutoCancel(true)
// .setContentIntent(pintent)
.build();
mNotificationManager.notify(1, notification);
这样写会出现如下错误还伴随卡死
大概可能是不安全吧 关于Android O的写法在网上很少 最后在一个国外网站上看到一种写法可行 并且我把它改编 还可以适配Android8.0以下的系统
贴出国外网站
http://forum.yourwebapp.mobi/android-o-how-to-use-notification-channels/不得不说标题非常亲民啊:ANDROID O: HOW TO USE NOTIFICATION CHANNELS
·改编后的具有简单兼容性的代码
NotificationUtils:
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
/**
* Created by LaoZhao on 2017/11/19.
*/
public class NotificationUtils extends ContextWrapper {
private NotificationManager manager;
public static final String id = "channel_1";
public static final String name = "channel_name_1";
public NotificationUtils(Context context){
super(context);
}
public void createNotificationChannel(){
NotificationChannel channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
getManager().createNotificationChannel(channel);
}
private NotificationManager getManager(){
if (manager == null){
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
return manager;
}
public Notification.Builder getChannelNotification(String title, String content){
return new Notification.Builder(getApplicationContext(), id)
.setContentTitle(title)
.setContentText(content)
.setSmallIcon(android.R.drawable.stat_notify_more)
.setAutoCancel(true);
}
public NotificationCompat.Builder getNotification_25(String title, String content){
return new NotificationCompat.Builder(getApplicationContext())
.setContentTitle(title)
.setContentText(content)
.setSmallIcon(android.R.drawable.stat_notify_more)
.setAutoCancel(true);
}
public void sendNotification(String title, String content){
if (Build.VERSION.SDK_INT>=26){
createNotificationChannel();
Notification notification = getChannelNotification
(title, content).build();
getManager().notify(1,notification);
}else{
Notification notification = getNotification_25(title, content).build();
getManager().notify(1,notification);
}
}
}
MainActivity:
import android.app.NotificationManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private NotificationManager mManager;
private Button sendn;
public static final String id = "channel_1";
public static final String name = "名字";
private NotificationUtils notificationUtils;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendn = findViewById(R.id.send_notice);
sendn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.send_notice:
NotificationUtils notificationUtils = new NotificationUtils(this);
notificationUtils.sendNotification("测试标题", "测试内容");
}
}
}
布局文件activity_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="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/send_notice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送通知"/>
</LinearLayout>
经过测试可以在8.0模拟器和7.0模拟器运行。