如何通过单击按钮在android上显示电源关闭菜单? [英] How to show power off menu on android on the click of a button?

查看:117
本文介绍了如何通过单击按钮在android上显示电源关闭菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一种功能,在该功能上,单击按钮会出现android菜单,我们会在长按电源按钮时看到该菜单,然后用户可以选择关闭设备.

I want to implement a functionality where on the click of a button the menu of android appear which we see on the long tap of power button, and then the user can choose to turn the device off.

我在说这个菜单

我需要没有root.商店中有一个无需root即可执行的应用程序. https://play.google.com/store/apps/详细信息?id = com.jjo.lockScreenButton

I need it to be without root. There's an app on store which does it without requiring root. https://play.google.com/store/apps/details?id=com.jjo.lockScreenButton

推荐答案

我可能会迟到,但这是可能的.为此,您必须使用可访问性服务,用户必须允许其访问.另外,请记住,可访问性服务旨在帮助残障人士,而不是针对此用例.

I might be late but this is possible. You have to use an accessibility service for that, which the user must allow access to. Also, keep in mind that accessibility services are meant to help handicapped people and not for this use case.

public class PowerMenuService extends AccessibilityService {

private BroadcastReceiver powerMenuReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(!performGlobalAction(intent.getIntExtra("action", -1)))
            Toast.makeText(context, "Not supported", Toast.LENGTH_SHORT).show();
    }
};

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {}

@Override
public void onInterrupt() {}

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

    LocalBroadcastManager.getInstance(this).registerReceiver(powerMenuReceiver, new IntentFilter("com.yourapp.ACCESSIBILITY_ACTION"));
}

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

    LocalBroadcastManager.getInstance(this).unregisterReceiver(powerMenuReceiver);
}
}

确保将com.yourapp替换为您的应用程序包

make sure to replace com.yourapp with your app package

<application>标记下添加以下内容:

Add the following under the <application> tag:

 <service
        android:name=".PowerMenuService"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService" />
        </intent-filter>
        <meta-data android:name="android.accessibilityservice"
            android:resource="@xml/accessibility_service" />
    </service>

3. accessibility_service.xml

在您的xml资源目录中,创建一个名为accessibility_service.xml的文件,其中包含以下内容:

3. accessibility_service.xml

In your xml resource directory create a file named accessibility_service.xml which contains the following:

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:packageNames="com.yourapp" />

再次,替换com.yourapp.您还应该提供说明.此处的更多信息: https://developer.android.com/guide/topics/ui/accessibility/services.html#service-config

Again, replace com.yourapp. You should also provide a description. More info here: https://developer.android.com/guide/topics/ui/accessibility/services.html#service-config

ComponentName component = new ComponentName(getApplicationContext(), PowerMenuService.class);
    getApplicationContext().getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);

    Intent intent = new Intent("com.yourapp.ACCESSIBILITY_ACTION");
    intent.putExtra("action", AccessibilityService.GLOBAL_ACTION_POWER_DIALOG);
    LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);

来自: https://github.com/farmerbb/Taskbar

这篇关于如何通过单击按钮在android上显示电源关闭菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆