Andorid 8.1关机ACTION_REQUEST_SHUTDOWN的变化.

平台

RK3399 + Android 8.1

问题

在旧SDK使用的关机接口在8.1上测试不可用, 代码如下:

        //shutdown now
        String action = "android.intent.action.ACTION_REQUEST_SHUTDOWN";
        Intent shutdown = new Intent(action);
        shutdown.putExtra("android.intent.extra.KEY_CONFIRM", false);
        shutdown.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            startActivity(shutdown);
            finish();
        }catch(Exception e){
            e.printStackTrace();
        }

执行时发现找不到 activity.

原因

|-- frameworks/base/core/java/android/content/Intent.java
旧SDK:

public static final String ACTION_REQUEST_SHUTDOWN = "android.intent.action.ACTION_REQUEST_SHUTDOWN";

新SDK:

    public static final String ACTION_REQUEST_SHUTDOWN
            = "com.android.internal.intent.action.REQUEST_SHUTDOWN";

Action值变了, 新代码如下

        //shutdown now
        String action = "com.android.internal.intent.action.REQUEST_SHUTDOWN";
        if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.N){
            action = "android.intent.action.ACTION_REQUEST_SHUTDOWN";
        }
        Intent shutdown = new Intent(action);
        shutdown.putExtra("android.intent.extra.KEY_CONFIRM", false);
        shutdown.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            startActivity(shutdown);
            finish();
        }catch(Exception e){
            e.printStackTrace();
        }

你可能感兴趣的:(android,关机)