How to create app which can be launched with dialpad - android

I wanted to know how can i launch my android app with some code from Dialpad. Like if you
##3214789650##
from your galaxy it launches angryGps application.
How to implement that?
Thanks.

try this.use Broadcast Receivers to listen outgoing call number:
Manifest.xml
uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<receiver android:name=".OutgoingCallReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
OutgoingCallReceiver.java
public class OutgoingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
if (null == bundle)
return;
// outgoingNumber=intent.getStringExtra(Intent.ACTION_NEW_OUTGOING_CALL);
String phoneNubmer = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
//START APPLICATION HERE
}
}

What you are looking for is part of contacts application and Although its very likely that the implementation is same for every manufacturer but i am not sure about this.
The intent for starting a new activity is passed by the function handleSecretCode in the file SpecialCharSequenceMgr. The code snippet is
static boolean handleSecretCode(Context context, String input) {
// Secret codes are in the form *#*#<code>#*#*
int len = input.length();
if (len > 8 && input.startsWith("*#*#") && input.endsWith("#*#*")) {
Intent intent = new Intent(Intents.SECRET_CODE_ACTION,
Uri.parse("android_secret_code://" + input.substring(4, len - 4)));
context.sendBroadcast(intent);
What you need to do is to register broadcast receiver for the intent action Intents.SECRET_CODE_ACTION and the uri android_secret_code://"code" and in the broadcast receiver you can launch your application.
Also you can see how some applications are already implementing, One of the code that works on emulator is * #* #4636 #* #*.

Related

How to pass data from one app to another app without opening the latter?

I want to trigger an application, B from application, A.
To achieve this i wrote the following in A
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.somepackage.appb");
intent.putExtra("secret", "message");
startActivity(intent);
However this opens up the application B which is not desired.
Please suggest a work around to avoid B from opening up and receive the data in background from application A.
Write a BroadcastReceiver in application B and declare it in your manifest.
In application A, craft an intent with extras to target that receiver, and call sendBroadcast with that intent.
Application B:
Manifest
<application>
...
<receiver android:name=".IncomingReceiver" android:enabled="true">
<intent-filter>
<action android:name="jason.wei.custom.intent.action.TEST"></action>
</intent-filter>
</receiver>
</application>
IncomingReceiver.java
public class IncomingReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String CUSTOM_INTENT = "jason.wei.custom.intent.action.TEST";
if (intent.getAction().equals(CUSTOM_INTENT)) {
System.out.println("GOT THE INTENT");
Toast.makeText(context, "GOT THE INTENT", Toast.LENGTH_LONG).show();
}
}
}
Application A:
String CUSTOM_INTENT = "jason.wei.custom.intent.action.TEST";
Intent i = new Intent();
i.setAction(CUSTOM_INTENT);
sendBroadcast(i);
You can use SharedPreferences.
As you can see there are many ways to do this...myself i prefer creating a service which can update data without interacting with app B

Launch application using dial pad in android

I want to launch my application through dial pad.I am using the following code. For dial pad to launch application (in Broadcast receiver)
public class HiddenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try{
// Toast.makeText(context,"Number Dialed",1).show();
Intent serviceIntent = new Intent(context,MainActivity.class);
serviceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
}
catch(Exception e)
{
Log.d(TAG, ""+e.getMessage());
}
While pressing key through dial pad I want to launch my main activity in which I used the following
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hidden_receiver);
//Intent call here
Intent intent=getIntent();
String message = intent.getStringExtra(MainActivity.TELEPHONY_SERVICE);
//text here
But when I press my code its dialed number disappear but neither dialer pad disappear nor MainActivity launches.
how can this issues be resolved?Help me out.....
Thanks.
Use the BroadcastReceiver as follows:
public class MyOutgoingCallHandler extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Extract phone number reformatted by previous receivers
String phoneNumber = getResultData();
if (phoneNumber == null) {
// No reformatted number, use the original
phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}
if(phoneNumber.equals("1234")){ // DialedNumber checking.
// My app will bring up, so cancel the broadcast
setResultData(null);
// Start my app
Intent i=new Intent(context,MainActivity.class);
i.putExtra("extra_phone", phoneNumber);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
Don't forget to register this receiver in your manifest
<receiver android:name="MyOutgoingCallHandler">
<intent-filter >
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
Also, include the permission:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
Now, if you are ignoring the number checking in receiver, you'll get dialed number inside your MainActivity,
String phone=getIntent().getStringExtra("extra_phone");
if(!phone.equals(null)){
Toast.makeText(getBaseContext(), phone, Toast.LENGTH_LONG).show();
}

Get confirmation call answered on android

I'm doing a handle on android phone numbers, you can know if a call is made​​, ie confirm that the call was answered. Use the following code to make the call:
Intent callIntent = new Intent (Intent.ACTION_CALL, Uri.parse (number));
startActivity (callIntent);
When you end the call back to my application and that's when I want to know if the call was answered and if it could be the duration of the call.
Yes you can, for this you need create BroadCastReceiver with intent
for example
<receiver android:name=".broadcast.DontMissReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
and then on Receiver
#Override
public void onReceive(final Context context, Intent intent) {
String action = intent.getAction();
if (action.equalsIgnoreCase(PHONE_ACTION)) {
this.receivePhoneCall(context, intent);
}
}
private void receivePhoneCall(Context context, Intent intent) {
String curState = intent.getExtras().getString("state");
if (curState.equalsIgnoreCase("RINGING")) {
} else
if (curState.equalsIgnoreCase("IDLE") && state.length() > 0) {
if (!state.equalsIgnoreCase("OFFHOOK")) {
}
}
}

How to detect incoming call with the help of Broadcast Receiver?

I'm trying to recognize incoming calls in thru a broadcast receiver. I'm UNABLE to do so! Infact, I'm unable to 'trigger' the broadcast!
Here's my code:
activate.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Clicked",1).show();
final String BROADCAST_ACTION_NAME = ".BroadcastMM";
Intent intent = new Intent();
intent.setAction(BROADCAST_ACTION_NAME);
sendBroadcast(intent);
}
}
I dunno if this 'sendBroadcast' is ever triggered! In my Broadcast Receiver file:
public void onReceive(Context context, Intent intent)
{
if(intent.getAction()=="android.intent.action.PHONE_STATE"){
Toast.makeText(c,"BroadCast fired!",1).show();}
Bundle extras = intent.getExtras();
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Toast.makeText(context, "Ringing", 1).show();
}
}
My manifest file:
<receiver android:name=".BroadcastMM" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" >
</action>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Is there some logic I'm missing? I'm very new to Android, so please help me out.
intent.getAction()=="android.intent.action.PHONE_STATE"
should be
TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(intent.getAction());
Since this is how you compare Strings (with equals()).
Also, the code you use to broadcast, should never broadcast - there is no ".BroadcastMM" action. Try making an explicit one instead:
Intent intent = new Intent(v.getContext(),BroadcastMM.class);
sendBroadcast(intent);
It is also likely that you can't broadcast android.intent.action.PHONE_STATE, so your if won't be executed if you make an explicit Intent.
If you really want to check that your BroadcastReceiver is working, put printouts/Toasts outside all ifs. Then once you establish that the BroadcastReceiver responds, do your check. Keep in mind though, that since you only listen for one Intent-Filter, the if checking if the Intent is a PHONE_STATE Intent is a bit redundant.

Save incoming number in a variable to use in the Activity

I was looking at the BroadcastReceiver and Activity and I didn´t find a right mode to use the incoming number that I have from the BroadcastReceiver in my Activity. This is the code that I use to intercept the incoming number and visualize in a Toast:
public class CustomBroadcastReceiver extends BroadcastReceiver {
String ophoneNumber;
#Override
public void onReceive(Context context, Intent incoming) {
Bundle bundle = incoming.getExtras();
ophoneNumber= bundle.getString("incoming_number");
Toast.makeText(context, ophoneNumber, Toast.LENGTH_LONG).show();
}
}
Manifest part:
<receiver android:name=".CustomBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
My problem is that I have to use the incoming number in an Activity that is running, so I have also read about the possibility to put the BroadcastReceiver inside the Activity like this:
public void monitorIncomingCalls(){
INcall = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent) {
final Bundle extras = intent.getExtras();
if(intent.getAction().equalsTelephonyManager.ACTION_PHONE_STATE_CHANGED))
inphoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
}
};
registerReceiver(INcall, new IntentFilter(Intent.ACTION_ANSWER));
}
But with this I don´t have nothing in the inphoneNumber variable, I think because I can´t register correctly the BroadcastReceiver because I miss the permission.
I didn´t find in the web an example that is working for what I want, that is basically put in a variable in my Activity the number that is calling me.
Retrieve incoming call's phone number in Android
Check out this link to save the incoming number.
Extend PhoneStateListener
Extend BroadCastReceiver
Display the toast.

Categories

Resources