Android development: hook shortcut on keyboard when phone app activity is focused

五迷三道 提交于 2021-02-19 03:09:19

问题


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

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.

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.


回答1:


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

I simply have this manifest :

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

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.

My activity:

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);
    }
}

And my receiver is like this :

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();
        }
    }
}

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

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



来源:https://stackoverflow.com/questions/38618323/android-development-hook-shortcut-on-keyboard-when-phone-app-activity-is-focuse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!