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. 讨论会3:Android用户研究及4.0界面设计分析
  2. Android学习系列(2)--App自动更新之通知栏下载
  3. android用户界面之WebView教程实例汇总
  4. AndroidStudio多渠道打包心得
  5. 如何提高Android用户体验
  6. Android 通知的基本用法示例代码
  7. android 通过wifi 获取经纬度和获取渠道号/屏幕宽高
  8. android用户界面之TabHost教程实例汇总
  9. Android手机用户隐私获取,包括读取通讯录、读取通话记录、读取浏

随机推荐

  1. Android ListView中item之间的分割线
  2. Android空间EditText的InputType属性
  3. android 动态切换主题,动态换肤
  4. android makefile prebuild
  5. Android layout文件中 '?' 的作用
  6. 使用 Android Compatibility Package 来
  7. Mac下Android Studio使用
  8. android 网络实时监听网络状态变化 及 网
  9. C#开发Android手机应用全接触(mono for a
  10. Android ListView 不显示分割条 分隔条