1、该书中使用的Android版本较老,8.0以下,没有渠道这一说法,所以使用的高版本Android系统,需要进行适配,即判断本机系统是否在8.0以上,是的话,添加渠道,方法如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button sendNotice = (Button) findViewById(R.id.send_notice);        sendNotice.setOnClickListener(this);    }    @Override    public void onClick(View view) {        switch (view.getId()){            case R.id.send_notice:                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);                if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){  //Android O (8.0)以上版本需要渠道                                NotificationChannel notificationChannel = new NotificationChannel("channelid1","channelname",NotificationManager.IMPORTANCE_HIGH);//通知重要度,DEFAULT及以上,通知时手机默认会有振动                    manager.createNotificationChannel(notificationChannel);                }                NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this,"channelid1");                builder.setContentTitle("This is content title");                builder.setContentText("This is content text");                builder.setWhen(System.currentTimeMillis());                builder.setSmallIcon(R.mipmap.ic_launcher);                builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));                manager.notify(1,builder.build());                break;                default:                    break;        }    }}

2、引导用户开启通知

今天再次用了通知加到自己的APP中,结果发现上面好使的程序失效了,经过一番波折,发现是手机系统问题,不知什么时候系统升级优化了,把我的APP   允许通知 关掉了(设置->通知管理->允许通知),因此有必要在程序中加入打开通知引导程序,引导用户打开通知开关:

 //检查手机是否打开通知,若没有,引导用户打开    public void checkAllowNotify(){        //判断APP通知是否已经打开        NotificationManagerCompat notification = NotificationManagerCompat.from(this);        boolean isEnabled = notification.areNotificationsEnabled();        //若未打卡,引导用户打开(考虑系统兼容性)        if (!isEnabled) {            //未打开通知            AlertDialog alertDialog = new AlertDialog.Builder(this)                    .setTitle("提示")                    .setMessage("请在“通知”中打开通知权限")                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialog, int which) {                            dialog.cancel();                        }                    })                    .setPositiveButton("去设置", new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialog, int which) {                            dialog.cancel();                            Intent intent = new Intent();                            //android 8.0引导                            if(Build.VERSION.SDK_INT >=26){                                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");                                intent.putExtra("android.provider.extra.APP_PACKAGE", MyApplication.getContext().getPackageName());                            }                            //android 5.0-7.0                            if(Build.VERSION.SDK_INT >=21 && Build.VERSION.SDK_INT <26) {                                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");                                intent.putExtra("app_package", MyApplication.getContext().getPackageName());                                intent.putExtra("app_uid", MyApplication.getContext().getApplicationInfo().uid);                            }                            //其他                            if(Build.VERSION.SDK_INT <21){                                intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");                                intent.setData(Uri.fromParts("package", MyApplication.getContext().getPackageName(), null));}                            startActivity(intent);                        }                    })                    .create();            alertDialog.show();        }    }

 

更多相关文章

  1. android 版本更新和下载安装 适配android 7.0
  2. Android(安卓)中文 API (26) ―― SeekBar
  3. android获取sdk更新
  4. 解决Eclipse New菜单没有Android(安卓)Project
  5. Android学习系列(2)--App自动更新之通知栏下载
  6. [Android]新功能引导高亮显示遮罩层View
  7. Android(安卓)Map (1) Android(安卓)Maps-api Key 申请
  8. Android(安卓)文件打开方式
  9. Android(安卓)通知的基本用法示例代码

随机推荐

  1. 整理出15个Android很有用的代码片段
  2. Android(安卓)中数据库查询方法query()中
  3. Android消息推送机制调研
  4. 关于Android中的radioGroup选择
  5. Android通过tcpdump抓包(wifi, 2g, 3g都
  6. android.database.CursorIndexOutOfBound
  7. Android(安卓)DownloadManager 用法
  8. android 之 菜单
  9. android 定时器的实现
  10. android --相机使用详解概述