android8.0適切な作業

6633 ワード

8.0全体が適合し、設計された部分は6部分を含む(私のプロジェクトでは、他の適合感はプロジェクトの実行に影響しない)
1)ランタイム権限
2)通知Notifaction適合
3)不明なソースapkインストール適合
4)バックグラウンド実行制限(バックグラウンドサービス)
5)浮上枠適合
6)集合の処理(AbstractCollection使用制限)
一:ランタイム権限
android6.0のランタイム権限は、1つの権限が許可されると、同じグループの権限もデフォルトで許可され、8.0でバグが修正されます.各権限は単一の申請に行かなければならない.
二:通知Notifaction適合
8.0アンドロイドには、「通知チャネル」という概念が追加されました.すべての通知は通知チャネルを追加しなければなりません.そうしないとcrashになります.適切に組み合わせるのも簡単です.チャネルオブジェクトを作成し、パラメータでオブジェクトを転送すればいいです.白話:Notification.Builderオブジェクトの作成には、チャネルidが追加されます.
8.0通知の作成方法:

 mBuilder = new Notification.Builder(MainApplicaton.getInstance());

8.0 :

:


NotificationChannel channel = new NotificationChannel(“01”, “  ”, NotificationManager.IMPORTANCE_HIGH);
            channel.canBypassDnd();//          
            channel.enableLights(true);//   
            channel.setLockscreenVisibility(VISIBILITY_PUBLIC);//      
            channel.setLightColor(android.graphics.Color.RED);//        
            channel.canShowBadge();//  launcher     
            channel.getGroup();//       
            channel.setBypassDnd(true);//         
            channel.shouldShowLights();//      


mNotificationManager = (NotificationManager) mGContext.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannel(channel);


mBuilder = new Notification.Builder(mGContext, “01”); //   01   


private NotificationUtil() {
        mGContext = MainApplicaton.getInstance();
        mNotificationManager = (NotificationManager) mGContext.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    /**
     *    notifacation     8.0  channel
     */
    private void initNotification() {
        if (Build.VERSION.SDK_INT >= 26) {
            NotificationChannel channel = new NotificationChannel(“01”, “  ”, NotificationManager.IMPORTANCE_HIGH);
            channel.canBypassDnd();//          
            channel.enableLights(true);//   
            channel.setLockscreenVisibility(VISIBILITY_PUBLIC);//      
            channel.setLightColor(android.graphics.Color.RED);//        
            channel.canShowBadge();//  launcher     
            channel.getGroup();//       
            channel.setBypassDnd(true);//         
            channel.shouldShowLights();//      
            mNotificationManager.createNotificationChannel(channel);
            mBuilder = new Notification.Builder(mGContext, "01");
        } else {
            mBuilder = new Notification.Builder(MainApplicaton.getInstance());
        }
    }

: apk

8.0 apk , , 。


if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            boolean haveInstallPermission = context.getPackageManager().canRequestPackageInstalls();
            //                        
            if (!haveInstallPermission) {
                Toast.makeText(context, "              ,         ", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
                context.startActivityForResult(intent, 0);
            } else {
                installApk(context, localUri); //  apk
            }
        } else {
            installApk(context, localUri); //  apk
        }

。。。。
//  apk

 private void installApk(Activity context, @Nullable String apkPath) {
        if (TextUtils.isEmpty(apkPath) || context == null) {
            return;
        }
        File file = new File(apkPath);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        } else {
            //Android7.0    uri  contentProvider
            Uri apkUri = FileProvider.getUriForFile(context, context.getPackageName(), file);
            //Granting Temporary Permissions to a URI
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

: ( + )

: , 。

: , service, crash。 。


**
 * Created by malei on 2019/1/22.
 *     service8.0     
 */

public class BaseService extends Service {

    public static final String CHANNEL_ID_STRING = "guagua001";
    public static final String CHANNEL_NAME = "guazi";

    //  8.0   service 8.0       ,5              
    public static void startService(Context context, Intent intent) {
        if (context == null || intent == null) {
            return;
        }
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            context.startForegroundService(intent);
        } else {
            context.startService(intent);
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();

        adapter8();
    }

    //  8.0service
    private void adapter8() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID_STRING, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(mChannel);
            Notification notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID_STRING).build();
            startForeground(1, notification);
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

service , BaseService 。

, ,  JobSchedulerは、これらの を する.