安卓8.0适配遇到的坑整理
2018-08-27 本文已影响106人
程序猿的小生活
1.设计到一些需要访问清单文件或者覆盖安装包时,或者是pakageManager类的时候,适配8.0需要加入新的权限,安装权限。
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
2.通知安卓8.0做了处理相比以前系统版本
public void notify1(String title,
String message) {
final String CHANNEL_ID = "channel_id_1";
final String CHANNEL_NAME = "channel_name_1";
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
//只在Android O之上需要渠道
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,
CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
//如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道,
//通知才能正常弹出
mNotificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(LoginActivity.this, CHANNEL_ID);
// PendingIntent intent = PendingIntent.getActivity(LoginActivity.this, 0, new Intent(LoginActivity.this, LoginActivity.class), PendingIntent.FLAG_CANCEL_CURRENT);
builder.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle(title)
.setContentText(message)
// .setContentIntent(intent)
.setAutoCancel(true);
mNotificationManager.notify(1, builder.build());
}