Make my app receive a phone call notification - android

My app need to know if the user is current on a phone call or not, then do some response accordingly.
All I need to know if the user is on a phone call or not. I do not need any other rights to deal with anything related to the phone number or any personal information of the user.
What should I do to achieve this? Do I need to get user's permission for it?enter code here

Yes First add these permission to your Manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
You should use Broadcast Receiver with this intent filters.
android.intent.action.NEW_OUTGOING_CALL
android.intent.action.NEW_INCOMING_CALL
You can use service for this register. Something like these:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
registerReceiver(receiver,new IntentFilter("android.intent.action.NEW_OUTGOING_CALL"));
registerReceiver(receiver2,new IntentFilter("android.intent.action.NEW_INCOMING_CALL"));
return START_STICKY;
}
And our Broadcast receiver you can use this:
public class CallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//
RequestQueue
String savedNumber="";
String savedNumber2= intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
}
Log.e("Saved number","Numara: "+savedNumber);
Log.e("İt is ","İt is it");
Log.e("Saved number 2 ",savedNumber2);
}
}
And lastly in your manifest file you could use intent filter in your receiver like these:
<receiver
android:name=".CallReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>

Related

BroadcastReceiver doesn't work accurately unless registered Manually

To put it simply; BroadcastReceiver Works when defined in Manifest but it works with a short Delay and it doesn't Always trigger unless Registered Manually.
Here is the BroadcastReceiver I created to capture event when date changes (day is passed):
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, intent.getAction(), Toast.LENGTH_LONG).show();
switch (intent.getAction()){
case Intent.ACTION_TIME_TICK:
case Intent.ACTION_TIME_CHANGED:
case Intent.ACTION_TIMEZONE_CHANGED:
case Intent.ACTION_DATE_CHANGED:
case Intent.ACTION_BOOT_COMPLETED:
Log.d("BroadcastReceiver", intent.getAction().toString());
}
}
}
Here registered in Manifest, It works but has a short delay and also doesn't always trigger:
<receiver android:name=".Receivers.BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.TIME_TICK" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.DATE_CHANGED"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
But when registered manually via my background service it works just fine:
public class ApplicationService extends Service{
...
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_DATE_CHANGED);
intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
intentFilter.addAction(Intent.ACTION_TIME_TICK);
registerReceiver(new BootBroadcastReceiver(), intentFilter);
return START_STICKY;
}
}
To investigate this issue further I created two similar app which in one BroadcastReceiver registered in Manifest and in the other one receiver is registered Manually via Service.
In the Manifest one all events are triggered few second after Manual one..
Try this :
<receiver android:name=".Receivers.BootBroadcastReceiver">
<intent-filter
android:enabled="true"
android:exported="true">
<action android:name="android.intent.action.TIME_TICK" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.DATE_CHANGED"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
I guess you can have multiple each one having its action.
https://developer.android.com/guide/components/intents-filters.html
The documentation says that "An app component should declare separate filters for each unique job it can do." This would allow finer grained detail for different actions
The answer was hidden in Intent documentation:
ACTION_TIME_TICK Added in API level 1
public static final String ACTION_TIME_TICK
Broadcast Action: The current time has changed. Sent every minute. You
cannot receive this through components declared in manifests, only by
explicitly registering for it with
Context#registerReceiver(BroadcastReceiver, IntentFilter).
To simply put, you can't receive TIME_TICK through registering the BroadcastReceiver in manifest, it has to be registered in your application components (Service, Activity, etc.)
Define a BootBroadcastReceiver anywhere in Activity/Fragment like this:
mBootBroadcastReceiver = new BootBroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG," BootBroadcastReceiver"); //do something with intent
}
};
mIntentFilter=new IntentFilter("action_name");
Now register the BootBroadcastReceiver in onResume() and Unregister in onPause()
#Override
protected void onResume() {
super.onResume();
registerReceiver(mBootBroadcastReceiver, mIntentFilter);
}
#Override
protected void onPause() {
if(mReceiver != null) {
unregisterReceiver(mBootBroadcastReceiver);
mBootBroadcastReceiver = null;
}
super.onPause();
}
add permission in your Manifest.
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="ANDROID.PERMISSION.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />

No listener when call state reject on voice phone

iam using app to phone sinch call, when user execute call, everything fine from call progressing until call ended, but when another user reject the phone call, listener from first user not return anything, is there any clue/information about this, really appreciate anything to make it clear.
For this you need to create a BroadcastReceiver which receive your call state and using that you can implement your another logic.
Here is demo for call receiver :
public class IncomingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (TelephonyManager.EXTRA_STATE_RINGING.equals(intent.getStringExtra(TelephonyManager.EXTRA_STATE))){
Log.d("mytag","call ringing");
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
// Toast.makeText(context, incomingNumber, Toast.LENGTH_LONG).show();
}
if (TelephonyManager.EXTRA_STATE_IDLE.equals(intent.getStringExtra(TelephonyManager.EXTRA_STATE))){
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Call Ended.", Toast.LENGTH_LONG).show();
}
}
}
Add these in your manifest file :
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<receiver
android:name=".phonemidea.IncomingCallReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

BroadcastReceiver for incoming calls?

I want a BroadcastReceiver for incoming calls.
I have searched about this on Stackoverflow and have read the answers but it does not log when Android device starts ringing. Here is my code -
public class Mybrd extends BroadcastReceiver {
#Override public void onReceive(Context context, Intent intent) {
Log.i("LOG", "LOG ME");
}
}
manifest:
<receiver android:name=".Mybrd">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Check you have register receiver correctly in manifest file.Please register recevier with fully qualified package name if it is not in the root package.
public void onReceive(Context context, Intent intent) {
String phoneState =intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (phoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Log.i("LOG", "LOG ME");}}
Try using the above code. Add permission
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

To block the out going call from my android app.

I want to make an application in which once the application starts, it will show two button(start and stop button) and once the user clicks the start button the call function will be blocked for the time period till the user again start the application and click the stop button to stop this function. any help please its urgent
in short I Will tell I want to block the outgoing call from my phone by using this activity only
please is there any way to do so???
You can block the outgoing call using the setResultData(null) function in the onReceive method of the Broaadcast receiver.
public class BlockOutgoing extends BroadcastReceiver
{
String number;
#Override
public void onReceive(Context context, Intent intent)
{
Log.d("12280", "asdasNumber is-->> " + number);
number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
setResultData(null);
Toast.makeText(context, "Outgoing Call Blocked" , 5000).show();
}
}
In the manifest file, you need to register the receiver like this,
<receiver
android:name=".BlockOutgoing"
android:label="#string/app_name" >
<intent-filter android:priority="1">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Also define the permission to intercept the outgoing call,
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
Edit-
To unregister a broadcast receiver, follow this link
public class BlockOutgoing extends BroadcastReceiver {
String number;
#SuppressLint("WrongConstant")
#Override
public void onReceive(Context context, Intent intent)
{
// Log.d("12280", "asdasNumber is-->> " + number);
number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
setResultData(null);
Toast.makeText(context, "Outgoing Call Blocked" , 5000).show();
}
}
<receiver
android:name=".BlockOutgoing"
android:label="#string/app_name" >
<intent-filter android:priority="1">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>

How to show incoming call notification in android application

I want to display one dialog box after incoming call, so that I can run my application in background while receiving call.
How to catch that incoming call in android application???
In AndroidManifest.xml you shoud make a receiver:
<receiver android:name="IncomingCallInterceptor">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
and declare permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Then,
public class IncomingCallInterceptor extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
// Phone is ringing
}
}
}
Maybe this broadcast intent is what you need ACTION_PHONE_STATE_CHANGED

Categories

Resources