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();
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章