我可以通过编程方式打开/关闭对讲吗?

时间:2018-12-18 11:39:47

标签: java android accessibility talkback braille

我正在为一个使用Android手机(盲文键盘)的盲人开展大学项目。

因此,当用户使用键盘时,我需要禁用对讲功能,然后再次启用它。 (用于在屏幕上使用多点触摸)。因此,我为android 8.0创建了一个新的示例简单项目,活动为空,并添加了一些内容。

我的示例android清单:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

还有我简单的MainActivity:

package com.jaime.prueba;
import android.os.Bundle;
import android.provider.Settings;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    updateTalkBackState(true);


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

private static final String TALKBACK_SERVICE_NAME = "com.google.android.marvin.talkback/.TalkBackService";

private void updateTalkBackState(boolean enableTalkBack) {
    if (enableTalkBack) {
        enableAccessibilityService(TALKBACK_SERVICE_NAME);
    } else {
        disableAccessibilityServices();
    }
}

private void enableAccessibilityService(String name) {
    Settings.Secure.putString(getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, name);
    Settings.Secure.putString(getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, "1");
}

private void disableAccessibilityServices() {
    Settings.Secure.putString(getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, "");
    Settings.Secure.putString(getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, "0");
}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

所以...

首先:我检查了以下答案:https://stackoverflow.com/a/44461236/10802519

//  It's not possible to turn TalkBack on or off from within your app unless you have been granted the system permission WRITE_SECURE_SETTINGS (via ADB) by the user.

private static final String TALKBACK_SERVICE_NAME = "com.google.android.marvin.talkback/.TalkBackService";

private void updateTalkBackState(boolean enableTalkBack) {
    if (enableTalkBack) {
        enableAccessibilityService(TALKBACK_SERVICE_NAME);
    } else {
        disableAccessibilityServices();
    }
}

private void enableAccessibilityService(String name) {
    Settings.Secure.putString(getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, name);
    Settings.Secure.putString(getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, VALUE_ENABLED);
}

private void disableAccessibilityServices() {
    Settings.Secure.putString(getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, "");
    Settings.Secure.putString(getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, VALUE_DISABLED);
}

第二:在我的终端中输入:

adb shell pm grant com.jaime.prueba android.permission.WRITE_SECURE_SETTINGS

第三:但是我得到了以下内容:

java.lang.SecurityException: Package com.jaime.prueba has not requested permission android.permission.WRITE_SECURE_SETTINGS
at com.android.server.pm.permission.BasePermission.enforceDeclaredUsedAndRuntimeOrDevelopment(BasePermission.java:379)
at com.android.server.pm.permission.PermissionManagerService.grantRuntimePermission(PermissionManagerService.java:1404)
at com.android.server.pm.permission.PermissionManagerService.access$900(PermissionManagerService.java:89)
at com.android.server.pm.permission.PermissionManagerService$PermissionManagerInternalImpl.grantRuntimePermission(PermissionManagerService.java:2093)
at com.android.server.pm.PackageManagerService.grantRuntimePermission(PackageManagerService.java:5411)
at com.android.server.pm.PackageManagerShellCommand.runGrantRevokePermission(PackageManagerShellCommand.java:1730)
at com.android.server.pm.PackageManagerShellCommand.onCommand(PackageManagerShellCommand.java:217)
at android.os.ShellCommand.exec(ShellCommand.java:103)
at com.android.server.pm.PackageManagerService.onShellCommand(PackageManagerService.java:21330)
at android.os.Binder.shellCommand(Binder.java:634)
at android.os.Binder.onTransact(Binder.java:532)
at android.content.pm.IPackageManager$Stub.onTransact(IPackageManager.java:2821)
at com.android.server.pm.PackageManagerService.onTransact(PackageManagerService.java:3856)
at android.os.Binder.execTransact(Binder.java:731)

第四我在Android Studio上构建了我的应用,一切正常,没有红色,因此我在手机上运行了该应用……而我的应用无法打开,无法正常工作。

我希望我的问题是可以理解的。让我知道,如果我的问题还有其他问题-第一个问题,用外语-谢谢!

1 个答案:

答案 0 :(得分:1)

清单文件中的通知:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

但是在终端中输入:

adb shell pm grant com.jaime.prueba android.permission.WRITE_SECURE_SETTINGS

WRITE_SETTINGSWRITE_SECURE_SETTINGS是两个不同的权限。

关于WRITE_SECURE_SETTINGS,您可以阅读此post