Android app应用调用系统关机菜单

来源:互联网 发布:日本j2联赛网络直播 编辑:程序博客网 时间:2024/06/06 12:59

        在Android系统中,长按Power按键一定时间会弹出关机菜单。系统并没有提供相应的接口给应用开发者调用。但是客户自己做了个widgets,里面有个Power选项,点击Power图片时必须弹出系统关机菜单。

Intent shutdown = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);shutdown.putExtra(Intent.EXTRA_KEY_CONFIRM, true);shutdown.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(shutdown);
        在PowerActivity.java中处理Power选项的动作,添加上述代码后,出来的是一个选择关机或取消关机的Dialog,明显不符合要求。开始考虑自己写一个界面包括“Power off 、Airplane mode、Silentmode”,后来想还是用系统功能算了。也就是想办法把系统的这个现实菜单的功能给暴露出来。

        分析可知,系统发现Power长按按键后,是在frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java中的showGlobalActionsDialog()处理现实关机菜单的。使用广播接收器,将其暴露给用户。

        在Intent.java中添加Intent.ACTION_SOFTWARE_POWER = “com.pda.power”;

        在PhoneWindowManager.java的Init函数中添加广播接收器:

import android.content.Intent;import android.content.IntentFilter;IntentFilter filter = new IntentFilter();filter.addAction(Intent.ACTION_SOFTWARE_POWER);context.registerReceiver(mPowerReceiver, filter);BroadcastReceiver mPowerReceiver = new BroadcastReceiver() {    public void onReceive(Context context, Intent intent) {        if (Intent.ACTION_SOFTWARE_POWER.equals(intent.getAction())) {            showGlobalActionsDialog();        }    }};
        在PowerActivity.java中发送广播:

Intent shutdown = new Intent(Intent.ACTION_SOFTWARE_POWER);sendBroadcast(shutdown);

        测试成功,showGlobalActionsDialog()通过广播暴露给应用开发者。通过广播来与系统类通信,比添加系统调用接口方便很多。

0 0
原创粉丝点击