BroadCast Receiver calling intent not working for second time - android

In my i have a receiver to predict incoming calls,Once i have the call i need bring my app to front of screen(Over call accept and reject scree), the code for receiver is shown below,
public class IncomingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
Log.i("IncomingCallReceiver",bundle.toString());
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
Log.i("IncomingCallReceiver","State: "+ state);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i("IncomingCallReceiver","Incomng Number: " + phonenumber);
String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
Intent intent2open = new Intent(context, com.varma.samples.detectcalls.ui.MainActivity.class);
intent2open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent2open.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
String name = "KEY";
String value = "String you want to pass";
intent2open.putExtra(name, value);
context.startActivity(intent2open);
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
}
}
}
in my manifest i added following parameters,
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="com.varma.samples.detectcalls.ui.MainActivity"
android:label="#string/app_name" android:launchMode="singleTop" android:taskAffinity="#android:string/no">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.varma.samples.detectcalls.receivers.OutgoingCallReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
<receiver android:name="com.varma.samples.detectcalls.receivers.IncomingCallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
But when i launch the activity the receiver is started to listen incoming calls,then i close all instance of app in Settings, when call is coming for first time, my app is bring front over answer & reject screen.
Then i push back button to reject my call. Then when i make a call again the activity was not bring in front of my dialing screen. I don`t know where is the problem.

Related

Open Activity With Dial Code

I want to develop app that doesn't have icon launcher. The app will run alarmscheduler which triggered when app is installed or phone is rebooted.
The problem is how can I open the activity since the app doesn't have intent filter like below:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Is there a way to handle dial code such as ##4635*#*# to open the activity ?
or any other solutions are welcomed.
You can do it with two ways:
1) as answer by #KishuDroid
2) By define code in manifest
public class MySecretCodeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SECRET_CODE")) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
In manifest file
<receiver android:name=".MySecretCodeReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code" android:host="4635" />
</intent-filter>
</receiver>
Note:
In Second method you must have to dial *#*#your_code#*#* and dont need to press call button
But in first method you can customise your prefix or postfix of code. For example *#your_code# or **your_code##. but you need to press call button.
You have to use Broadcast Receiver...
public class OutgoingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("OutgoingCallReceiver",phonenumber);
Log.i("OutgoingCallReceiver",bundle.toString());
if(code.equals("#056700") {
intent.setComponent(new ComponentName("com.example", "com.example.yourActivity"));
And Your Android Manifest
<receiver android:name="com.varma.samples.detectcalls.receivers.OutgoingCallReceiver">
<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"/>

BroadcastReceiver is not able to receive the Intent

I want to write a BroadcastReceiver to receive the application install action. But it failed, so I test if my receiver is well or not. So custom a intent, it also filed. below is my code. Please help me correct it.
public class MyInstallReceiver extends BroadcastReceiver {
// public MyInstallReceiver() {
// }
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
Log.d("receiver", "Intent Detected");
if (intent.getAction (). equals ("android.intent.action.PACKAGE_ADDED")) {
String packageName = intent.getDataString ();
//System.out.println ("installed:" + packageName + "package name of the program");
Log.d("receiver","installed:" + packageName + "package name of the program");
}
}
}
custom intent
public void installAPK(View v){
startActivity(intent);
Intent intent = new Intent();
intent.setAction("com.tutorialspoint.CUSTOM_INTENT");
sendBroadcast(intent);
Log.d("receiver", "Intent sent");
}
Manifest.xml
<receiver
android:name=".MyInstallReceiver"
android:enabled="true"
android:exported="true" >
<Intent-filter>
<action android:name = "android.intent.action.PACKAGE_ADDED"/>
<action android:name = "android.intent.action.PACKAGE_REMOVED"/>
<action android:name="com.tutorialspoint.CUSTOM_INTENT">
</action>
<Data android:scheme = "package" />
</Intent-filter>
</receiver>
enter code here
I don't know about correct spelling in your manifest, but this code is definitely works very well:
<receiver android:name=".MyInstallReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
Every application install/uninstall will trigger this receiver.
Everythong looks good, expect a typo in your manifest. It should be <intent-filter> and not <Intent-filter>

How to make BroadcastReceiver work after reboot?

I have a written a program which intercepts incoming call using BroadcastReceiver. When I start my app, it starts working. Now the problem is when I restart my android phone, this BroadcastReceiver doesn't work. So I am assuming that I need to make a service for this. But I don't know when to start service and where to start BroadcastReceiver.
BroadcastReceiver code -
public class CallInterceptor extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Service started", Toast.LENGTH_LONG).show();
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Toast.makeText(context, "Phone is " + state, Toast.LENGTH_LONG).show();
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Call from " + phoneNumber, Toast.LENGTH_LONG).show();
//sendSMS(phoneNumber);
//toggle ringer mode
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
toggleMode(am);
}
}
}
AndroidManifest file -
<receiver android:name="com.nagarro.service.CallInterceptor" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</action>
</intent-filter>
</receiver>
I don't think <action android:name="android.intent.action.BOOT_COMPLETED" /> is working.
Also, is it possible to do this without service? Please suggest approach which will help me to intercept call evertime (even after reboot).
You've improperly closed your first action tag. Your receiver section should look like:
<receiver android:name="com.nagarro.service.CallInterceptor" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Also, you are not using a Service. You're using a Broadcast Receiver.
See this page for the basics Application Fundamentals.

get number, which customer dialed from standard dialpad

i try catch a call from standard dialpad using that code:
<action android:name="android.intent.action.CALL" />
<data android:scheme="tel" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.CALL_PRIVILEGED" />
</intent-filter>
</activity>
Everything fine, when user dial from standard phone dialpad, my app opened.
But i don't find solution, how i can get a phone number, which user was dialed. That code inside PhonePadActivity activity onCreate block:
Intent intent = getIntent();
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(this, "Call was made to-->>" + number, 5000).show();
gives me a null finally :(
tried to using brodacast receiver:
in manifest:
<receiver
android:exported="true"
android:name="com.myapps.android.DialBroadcastReceiver" >
<intent-filter >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
this is class DialBroadcastReceiver:
package com.myapps.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class DialBroadcastReceiver extends BroadcastReceiver {
private static final String THIS_FILE = "PhonePadActivity";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.e(THIS_FILE,"In onReceive()");
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.e(THIS_FILE,"Number is: "+number);
}
}
}
but logs nod fired, when user press dial
This works for me:
In Manifest:
<intent-filter>
<action android:name="android.intent.action.CALL"/>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.CALL_PRIVILEGED"/>
<data android:scheme="tel"/>
</intent-filter>
In Activity:
String inputURI = this.getIntent().getDataString();
if (inputURI != null) {
Uri uri = Uri.parse(Uri.decode(inputURI));
if (uri.getScheme().equals("tel")) {
String calledNumber = uri.toString();
}
}
You are getting the number with incorrect code. Replace:
Intent intent = getIntent();
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(this, "Call was made to-->>" + number, 5000).show();
With:
Uri data = getIntent().getData();
if (data != null && ("tel".equals(data.getScheme()))) {
String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
if (number != null) {
Toast.makeText(this, "Call was made to-->>" + number, 5000).show();
}
}
Add <intent-filter android:priority="9999"> to the intent-filter declaration in the manifest, to make sure you're first in line
Remove the com.myapps.android-part from the android:name property of the receiver declaration in the manifest (i.e. it should be: android:name=".DialBroadcastReceiver")
Register a BroadcastReceiver like this in Manifest file:
<!-- DIAL Receiver -->
<receiver
android:exported="true"
android:name="receivers.DialBroadcastReceiver" >
<intent-filter >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
You need Permission for NEW_OUTGOING_CALL :
Eventually you can Receive the broadcast and retrieve the dialed number like this:
public class DialBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.v("DileBroadCastReceiver","In onReceive()");
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.v("DialBroadcast Receiver","Number is: "+number);
}
}
}

Handling incoming call in android

In my app i want to detect incoming call and i want to hide default incoming call layout with my custom layout,far now i have managed to get the state of incoming call but i'm not able to hide the default incoming call screen...below is my code and my android manifest file...any help will be appreciated..
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("DEBUG", "on recive called");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state))
{
abortBroadcast();
Log.d("MPR", "Its Ringing [" + number + "]");
//start activity
Intent i = new Intent();
i.setClassName("com.ezest.callerid", "com.ezest.callerid.CustomCallActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
if (TelephonyManager.EXTRA_STATE_IDLE.equals(state))
{
Log.d("MPR", "Its Idle");
}
if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state))
{
Log.d("MPR", "Its OffHook");
}
}
Manifest
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name="MYPhoneStateListener">
<intent-filter android:priority="999999">
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>
<activity
android:label="#string/app_name"
android:name=".CallerIDActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="CustomCallActivity">
</activity>
</application>
when defined action is happened android operating system send broadcast to all defined broadcastreceiver for this action. it does this job according to priority order. I can see you have done it. copy that code where you want to cancel broadcast.
abortBroadcast()
Android operating system will stop broadcasting to other broadcastreceivers

Categories

Resources