Android开发:在关注手机应用活动时,键盘上的快捷键

这是一个特定的问题,我之前做过一些
Android开发,但没有深入到系统管理.

所以我需要创建一个在后台运行的应用程序(这部分没问题),并在手机上的手机应用程序软件键盘输入特殊快捷方式(比如#123 * 6)时自动启动应用程序的活动.

如果有可能,请指点我,如果可以,我应该使用哪种API /组件?在网上找不到相关的东西.

最佳答案 好的,我终于在HTC设备上工作了.事实是三星手机似乎没有对密码做出反应……

我只是有这个清单:

<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)的正确权限,因为安全性已经发生了一些变化.我们现在必须在应用程序运行时期间即时声明权限,而不是在安装时.

我的活动:

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).

测试的手机没有捕获密码意图:三星Galaxy S4和Galaxy S6(Android 6.0).

点赞