user2945545
user2945545

Reputation: 143

how to activate app from dial pad?

I have an application which should activate its activity as soon as the user dials a number -- for example a digit "6". My question is: how do i activate my app or the activity in the app as soon as a number is dialed from the keypad?

Upvotes: 2

Views: 178

Answers (1)

Bryan Herbst
Bryan Herbst

Reputation: 67209

It sounds like you are looking for the "secret codes" feature. It doesn't let you listen for a single number (I don't believe that is possible), but it is what allows apps to launch with codes such as ##123456##.

You can register a BroadcastReceiver to listen for a secret code in your manifest like so:

<receiver android:name=".MyBroadcastReceiver">
    <intent-filter>
        <action android:name="android.provider.Telephony.SECRET_CODE"/>
        <data android:scheme="android_secret_code" android:host="123456"/>
    </intent-filter>
</receiver>

Your BroadcastReceiver might look something like this:

public class MyBroadcastReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        if ("android.provider.Telephony.SECRET_CODE".equals(intent.getAction())) {
            Intent i = new Intent(Intent.ACTION_MAIN);
            i.setClass(context, MyBroadcastReceiver.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
     }
}

Upvotes: 2

Related Questions