I want a service to monitor the calls I dial, and to perform a specific task parallel to a specific call.
Example. I call 123 from the dialer and a specific task gets triggered at that time.
But if I dial some other number then nothing should happen.
How can I implement such a thing?
You can use BroadcastReceiver for ACTION_NEW_OUTGOING_CALL
public class OutgoingBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent. getAction (). equals (Intent. ACTION_NEW_OUTGOING_CALL)) {
String number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.e("Number=", number);
if (number.equals("123")) {
// do your magic here
}
}
}
And don't forget and using permission:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
Related
onReceive of BroadcastReceiver not working.
I am trying different ways.
Here is my current code.
Activity code
BroadcastReceiver _receiver;
public static final String SMS_RECEIVED_ACTION =
"android.provider.Telephony.SMS_RECEIVED";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter _intentFilter = new IntentFilter(SMS_RECEIVED_ACTION);
_intentFilter.setPriority(1234567);
registerReceiver(_receiver,_intentFilter);
_receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Hello",Toast.LENGTH_LONG).show();
}
};
}
manifest file
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
Create your _receiver before registering it
_intentFilter.setPriority(999);
_receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Hello",Toast.LENGTH_LONG).show();
}
};
registerReceiver(_receiver,_intentFilter);
The priority should be bigger than -1000 and smaller than 1000
Documentation
android:priority
The priority that should be given to the parent component with regard to handling intents of the type described by the filter. This attribute has meaning for both activities and broadcast receivers:
It provides information about how able an activity is to respond to an intent that matches the filter, relative to other activities that could also respond to the intent. When an intent could be handled by multiple activities with different priorities, Android will consider only those with higher priority values as potential targets for the intent.
It controls the order in which broadcast receivers are executed to receive broadcast messages. Those with higher priority values are called before those with lower values. (The order applies only to synchronous messages; it's ignored for asynchronous messages.)
Use this attribute only if you really need to impose a specific order in which the broadcasts are received, or want to force Android to prefer one activity over others.
The value must be an integer, such as "100". Higher numbers have a higher priority. The default value is 0. The value must be greater than -1000 and less than 1000.
I am trying to launch my custom screen on top of native outgoing caller screen that may contain full Screen image of Caller and some buttons for actions like reject call. Using this I am able to make call, but is redirecting me to native caller screen...
How to replace\override the default call screen by my custom screen screen?
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phonenumber)));
public class GetOutgoingNUmber extends BroadcastReceiver {
final static String INTENT_PHONE_NUMBER = "android.intent.extra.PHONE_NUMBER";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.v("DileBroadCastReceiver","In onReceive()");
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(context, OutGoingScreen.class);
i.putExtras(intent);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(i);
}
}, 1000);
}
here OutGoingScreen is for displaying outgoing screen
public class OutGoingScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.outgoingscreen );
}
}
Now the problem is it is showing my screen for few msec and again showing native screen....?
Write a receiver for outgoing call
public class OutgoingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, final Intent intent) {
//Write intent for yout page
}
}
add these to Manifest
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<receiver android:name=.OutgoingCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
add below theme to activity theme
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen"
Open you intent after 1 second bcoz the original outgoing call screen takes 800ms to open so you need to over lay that screen, so you must call intent after 800ms.
it works for me.
The best way to do so is develop your own Phone app and expect the user to set it as the default app for making calls.
Edit:
Add an Activity which accepts ACTION_DIAL intent and have a numeric keypad and then once user has entered the call number, you can fire ACTION_CALL intent with the phone number which would invoke the native phone app. This way also, the user has to select your app to be set as default one for ACTION_DIAL intent.
You don't need to create a separate application.
Ultimately you just want to handle the new outgoing call requests,so create a BroadcastReceiver to listen the event of ACTION_NEW_OUTGOING_CALL,and create an Activity to invoke upon that event.
you will need to specify permission in manifest file about PROCESS_OUTGOING_CALLS.
Have a look at some references:
handling-phone-call-requests-right-way
android-dialer-application
I hope it will be helpful !
I've found a lot of pages about this in the web, but none of them helped me. I've been for hours stucked in this problem. That's why i decided to make my own question.
What I want to do is an application that receives an intent of the type ACTION_MEDIA_BUTTON and the method onReceive() of the Broadcastreceiver does something.
My Activity is like this:
public class MusicControlActivity extends Activity {
private MediaButtonIntentReceiver receiver = new MediaButtonIntentReceiver();
#Override
public void onCreate(Bundle p_SavedInstanceState) {
super.onCreate(p_SavedInstanceState);
setContentView(R.layout.main);
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
filter.setPriority(1000);
registerReceiver(receiver,filter);
}
#Override
public void onDestroy()
{
unregisterReceiver(receiver);
}
And my broadcastreceiver as follow:
public class MediaButtonIntentReceiver extends BroadcastReceiver {
public MediaButtonIntentReceiver()
{
super();
}
#Override
public void onReceive(Context context, Intent intent)
{
String v_IntentAction = intent.getAction();
if (!Intent.ACTION_MEDIA_BUTTON.equals(v_IntentAction)) {
return;
}
KeyEvent v_Event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (v_Event == null) {
return;
}
int v_Action = v_Event.getAction();
if (v_Action == KeyEvent.ACTION_DOWN) {
// do something
Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show();
}
abortBroadcast();
}
}
The problem is that it doesn't work, no matter what I do. I've tried registering it dynamically with registerReceiver as in the code above. Also I've tried statically in the AndroidManifest.xml like this:
<receiver android:name=".MediaButtonIntentReceiver" android:enabled="true">
<intent-filter android:priority="10000000">
<action android:name="android.intent.action.ACTION_MEDIA_BUTTON" />
</intent-filter>
</receiver>
And as you can see I've set the priority to a high level and even though it doesn't work.
I've been on this for the whole day and I don't know what to do. The method onReceive from the broadcastreceiver isn't called anytime.
Does anyone know what should I do?
Use android.intent.action.MEDIA_BUTTON Instead in your manifest. Check : http://developer.android.com/training/managing-audio/volume-playback.html
First, you should stop using Toast as your diagnostic test. Use the Log class to log to LogCat, or breakpoints, or something.
Next, you should get rid of your MediaButtonIntentReceiver constructor, as it is not needed.
Then, I would dump the if (!Intent.ACTION_MEDIA_BUTTON.equals(v_IntentAction)) block, since it is also not needed.
Next, I would make sure that your media button actually works. Does it control other applications, like a music player? It may be that your media button is not Android-compliant or something.
public class MyReceiver extends PhoneStateIntentReceiver {
#Override
public void onReceiveIntent(Context context, Intent intent) {
if (intent.action == Intent.CALL_ACTION) {
}
}
}
Assume that notifyPhoneCallState has been called to enable MyReceiver to receive notifications about phone call states, in which case the code will get executed?
when device receives an incoming call
when outgoing call is initiated on the device
when the user presses the call button
incoming phone call is terminated
or will the code not be executed at all?
Did you mean public static final String ACTION_CALL instead of CALL_ACTION?
Activity Action: Perform a call to someone specified by the data.
Input: If nothing, an empty dialer is started; else getData() is URI of a phone number to be dialed or a tel: URI of an explicit phone number.
Output: nothing.
Note: there will be restrictions on which applications can initiate a call; most applications should use the ACTION_DIAL.
Note: this Intent cannot be used to call emergency numbers. Applications can dial emergency numbers using ACTION_DIAL, however.
I am new to android platform.please help me out how the Broadcast Receiver and Intent Filter behaves in android.please explain in simple line or with example.thanks in advance...
A broadcast receiver is a class in your Android project which is responsible to receive all intents, which are sent by other activities by using android.content.ContextWreapper.sendBroadcast(Intent intent)
In the manifest file of you receicving activity, you have to declare which is your broadcast receiver class, for example:
<receiver android:name="xyz.games.pacman.network.MessageListener">
<intent-filter>
<action android:name="xyz.games.pacman.controller.BROADCAST" />
</intent-filter>
</receiver>
As you can see, you also define the intent filter here, that is, which intents should be received by the broadcas receiver.
Then you have to define a class which extends BroadcastReceiver. This is the class you defined in the manifest file:
public class MessageListener extends BroadcastReceiver {
/* (non-Javadoc)
* #see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
*/
#Override
public void onReceive(Context context, Intent intent) {
...
}
Here, all intents which are passed through the filter are received and you can access them using the parameter passed in the method call.
A BroadcastReceiver can be registered in two ways: dynamic or static. Static is nothing but declaring the action through an intent-filter in AndroidManifest.xml to register a new BroadcastReceiver class. Dynamic is registering the receiver from within another class. An intent-filter determines which action should be received.
To create a BroadcastReceiver, you have to extend the BroadcastReceiver class and override onReceive(Context,Intent) method. Here you can check the incoming intent with Intent.getAction() and execute code accordingly.
As a new class, static would be
public class Reciever1 extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String str = intent.getAction();
if(str.equalsIgnoreCase("HELLO1")) {
Log.d("Abrar", "reciever....");
new Thread() {
public void run() {
Log.d("Abrar", "reciever....");
System.out.println("Abrar");
}
}.start();
}
or, if placed inside an existing class, it is called dynamically with
intentFilter = new IntentFilter();
intentFilter.addAction("HELLO1");
//---register the receiver---
registerReceiver(new Reciever1(), intentFilter);
BroadcastReceiver : 'Gateway' with which your app tells to Android OS that, your app is interested in receiving information.
Intent-Filter : Works with BroadcastReceiver and tells the 'What' information it is interested to receive in. For example, your app wants to receive information on Battery level.