Reputation: 113
I am learning to launch my application through dialer pad.I am using the following code. for dialer pad to launch application (in Broadcast receiver)
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
if (null == bundle)
return;
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
//here change the number to your desired number
String compare_num="5556";
if(phoneNumber.equals(compare_num))
{
setResultData(null);
// Guardian.changeStealthMode(context,PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
//Intent to move to next Activity
Intent myintent=new Intent(context,MainActivity.class);
myintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myintent);
// abortBroadcast();
and the launching app containing the main activity
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//Intent call here
Intent intent=getIntent();
String message = intent.getStringExtra(MainActivity.TELEPHONY_SERVICE);
//text here
EditText et = (EditText) findViewById(R.id.editText1);
//Button here
Button Login = (Button) findViewById(R.id.button1);
}
while I am launching this code my app don't launch through dialerpad. Help me please.........
Upvotes: 0
Views: 2064
Reputation: 2593
Android class-
public class SecretCodeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try{
Toast.makeText(context,"Number Dialed",1).show();
Intent serviceIntent = new Intent(context,CalledClass.class);
serviceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
}
catch(Exception e)
{
Log.d(TAG, ""+e.getMessage());
}
}
}
Android manifest-
<receiver android:name=".SecretCodeReceiver" android:enabled="true"
>
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code" android:host="111" />
</intent-filter>
</receiver>
When user will dial
*#*#111#*#*
then the required event will fire. If you have have requirement to fire event on dial of number then need to implement ACTION_NEW_OUTGOING_CALL as mentioned in answer by "Shyam".
Upvotes: 1
Reputation: 6444
try this
public class Example extends BroadcastReceiver
{
@Override
public void onReceive(Context context, final Intent intent) {
if (intent.getAction().equals(android.intent.action.NEW_OUTGOING_CALL)) {
String phoneNumber = intent.getExtras().getString( android.intent.extra.PHONE_NUMBER );
if(phoneNumber.equals("#1234#")) {
Intent intent1 = new Intent(context , YourActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
context.startActivity(intent1);
}
}
}
}
in your manifest file
<receiver
android:name=".receiver.DialReceiver"
android:exported="true"
android:process=":background"
tools:ignore="ExportedReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Upvotes: 0