Android开发:专注于手机应用活动时在键盘上挂钩快捷方式 [英] Android development: hook shortcut on keyboard when phone app activity is focused

查看:47
本文介绍了Android开发:专注于手机应用活动时在键盘上挂钩快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个特定的问题,我之前已经做过一些Android开发,但是对系统管理的了解还不够.

It's a specific question and I've done a bit of Android development before, but not so deep into the system management.

因此,我需要创建一个在后台运行的应用程序(这部分是可以的),并在从手机应用程序软件键盘上键入特殊快捷方式(例如#123 * 6)时自动启动该应用程序的活动.电话.

So I need to create an app which run in background (this part is OK) and to launch automatically an activity of the app when a special shortcut (let's say #123*6) is typed from the phone app software keyboard on the phone.

如果可以的话,请您告诉我,如果可以,我应该使用什么API/组件?在网上找不到相关的内容.

Can you please indicate me if it's possible, and if yes, what API/Component I should use? Can't find something relevant on the Web.

推荐答案

好,我终于在HTC设备上完成了这项工作.事实是三星手机似乎对密码没有反应...

Ok I finally got this working on the HTC device. The fact is Samsung phones doesn't seems to react to secret codes ...

我只是有此清单:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MenuBrowser"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".ShortcodeService"
            android:exported="false">
        </service>

        <receiver
            android:name=".ShortcodeReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.provider.Telephony.SECRET_CODE" />
                <data android:scheme="android_secret_code" android:host="1992" />
            </intent-filter>
        </receiver>
</application>

我的服务只是检查Android M(6.0)的正确权限,因为安全性已经有所变化.现在,我们必须在应用程序运行时而不是在安装过程中即时声明权限.

My service is simply checking for the right permissions for Android M (6.0), because the security has changed a bit. We now have to declare permission on-the-fly during the application runtime, and not at installation.

我的活动:

public class MenuBrowser extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("USSDBrowser", "Start app");

        //if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE, Manifest.permission.PROCESS_OUTGOING_CALLS}, 10);

        Intent serviceIntent = new Intent(this.getApplicationContext(), ShortcodeService.class);
        startService(serviceIntent);

        //setContentView(R.layout.activity_menu_browser);
        finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_menu_browser, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

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

        return super.onOptionsItemSelected(item);
    }
}

我的接收器是这样的:

public class ShortcodeReceiver extends BroadcastReceiver {

    private static String defaultCode = "1992";

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d("USSDBrowser", "Intent received");

        if(intent.getAction().equals("android.provider.Telephony.SECRET_CODE")) {
            String code = intent.getDataString();

            if(code.equals("android_secret_code://" + defaultCode)) {
                Log.d("USSDBrowser", "Code received !!! ");
            }
            //Intent in = new Intent(context, MenuBrowser.class);
            //context.startActivity(in);

            Toast.makeText(context, "You typed a shortcode, hype !", Toast.LENGTH_LONG).show();
        }
    }
}

但是现在我进行了测试,它可以用于HTC One S(Android 4.1.1)和Aquaris E4(Android 4.4.2).

But now I tested that and it's working for HTC One S (Android 4.1.1) and Aquaris E4 (Android 4.4.2).

经过测试的未捕获秘密代码意图的手机是:三星Galaxy S4和Galaxy S6(Android 6.0).

The phones tested that does not capture secret code intents are : Samsung Galaxy S4 and Galaxy S6 (Android 6.0).

这篇关于Android开发:专注于手机应用活动时在键盘上挂钩快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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