Notes on Android 8.0 in the code

Keywords: Android Windows

1, Cancel most static registration broadcasts

Today, when I was doing the download task, I monitored the download progress through the broadcast. It was normal in 7.0, but running on 8.0, it failed. After checking, I found that 8.0 started to cancel most of the broadcast static registration

1. Register dynamically on the page to be monitored

      IntentFilter filter = new IntentFilter();
      filter.addAction(GUANGBO);
      mUpdateAppReceiver = new UpdateAppReceiver();
      registerReceiver(mUpdateAppReceiver, filter);

2. When the page is destroyed (ondestore()), remember to cancel listening

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mUpdateAppReceiver);
    }

3. Send broadcast. The action must be the same as the action when registering

    
    private static void sendProgress(Context context, int progress, String downloadMsg) {
         Intent intent = new Intent();
         intent.putExtra("progress", progress);
         intent.putExtra("downloadMsg", downloadMsg);
         intent.setAction(GUANGBO);
         context.sendBroadcast(intent);
    }

 

2, The usage of Notification and 8.0 is slightly different from that before, and the settings must be judged separately

String id = "channel_01";
String name = "Channel name";
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//Android 8.0 settings
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
    nm.createNotificationChannel(mChannel);
    notification = new Notification.Builder(this)
            .setChannelId(id)
            .setContentTitle("Downloading " + versionName)
            .setProgress(100, progress, false)
            .setSmallIcon(R.mipmap.ic_launcher).build();
} else {
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("Downloading " + versionName)
            .setProgress(100, progress, false)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setOngoing(true) // If Ongoing is set to true, notification will not be able to slide delete
            .setChannel(id);//invalid
    //      . builder. Setautoconcel (true); / / after setting autoconcel to true, when you click notification in the notification bar, it will be automatically cancelled and disappear
    notification = notificationBuilder.build();
}
nm.notify(notifyId, notification);
//Cancellation notice
private void cancelNotificaion() {
    if (nm != null) nm.cancel(notifyId);
}

 

3, If sd card is needed, and photo taking is limited, you must apply for permission manually, otherwise you cannot use it

 

4, Other new features of 8.0, such as support for multiple windows and adaptive icons (no need to adapt the image). For details, please see another friend's

New features of Android 8.0

 

 

 

Posted by Derleek on Sun, 26 Jan 2020 06:55:40 -0800