Android development: shortcut on the keyboard when working with a mobile phone

This is a specific question, and I have already worked a bit on Android, but not so deeply in the management of the system.

So, I need to create an application that works in the background (this part is in order) and automatically launch the application when a special shortcut (let it say # 123 * 6) is entered from the software keyboard for the phone on the phone.

Could you tell me if this is possible, and if so, which API / component should I use? It is impossible to find something relevant on the Internet.

+4
source share
1 answer

, , HTC. , Samsung, , ...

:

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

, , : Samsung Galaxy S4 Galaxy S6 (Android 6.0).

+1

Source: https://habr.com/ru/post/1649311/


All Articles