Android-8.0新特性-通知通道配置

Android 8.0开始发送通知需要配置渠道id;

Application#onCreate()中配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//8.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 通知渠道的id
String id = "你的渠道id";
// 用户可以看到的通知渠道的名字.
CharSequence name = getString(R.string.app_name);
// 用户可以看到的通知渠道的描述
String description = getString(R.string.app_push_message_hint);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
// 配置通知渠道的属性
mChannel.setDescription(description);
// 设置通知出现时的闪灯(如果 android 设备支持的话)
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.setSound(Uri.parse("android.resource://" + BaseUtil.getPackageInfo(BaseUtil.getApp()).packageName + "/" + R.raw.hint_zz_four), null);
// 设置通知出现时的震动(如果 android 设备支持的话)
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
//最后在notificationmanager中创建该通知渠道
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannel(mChannel);
}
}

Android-8.0新特性-服务启动问题

问题描述

  • Android 8.0 启动后台service 出错 IllegalStateException: Not allowed to start service Intent

原因

  • 极光推送老版本sdk启动推送服务未兼容8.0才出现的问题;

Android O对应用在后台运行时可以执行的操作施加了限制,称为后台执行限制(Background Execution Limits),这可以大大减少应用的内存使用和耗电量,提高用户体验。后台执行限制分为两个部分:后台服务限制(Background Service Limitations)、广播限> 制(BroadcastLimitations)。

Android 6.0新特性-动态权限申请

Android 6.0之后,部分权限需要动态申请。
但是AndroidManifest.xml文件中同样需要申明。

常见处理方式

请求权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (Build.VERSION.SDK_INT > 22) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(MainActivity.this, "这里提示用户进入设置界面开启权限", Toast.LENGTH_SHORT).show();
} else {
//Request
ActivityCompat.requestPermissions(MainActivity.this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0x01);
}
} else {
//Allow...
}
} else {
//Allow...
}