平台
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();
}

本文解决了一个在Android8.1上使用旧SDK关机接口失效的问题,通过对比新旧SDK中ACTION_REQUEST_SHUTDOWN的变化,给出了正确的代码实现。
595

被折叠的 条评论
为什么被折叠?



