In my application I want to open a window with caller name like Truecaller for incoming/outgoing call in PhoneGap for android application. I mean when I get call or outgoing call, I want to open a pop up window even when my application is closed.
My Code
// Enable background mode
cordova.plugins.backgroundMode.enable();
// Called when background mode has been activated
cordova.plugins.backgroundMode.onactivate = function() {
setInterval(function() {
PhoneCallTrap.onCall(function(state) {
console.log("CHANGE STATE: " + state);
if (state == "RINGING") {
var now = new Date().getTime(),
_5_sec_from_now = new Date(now + 1 * 1000);
var sound = device.platform == 'Android' ? 'file://sound.mp3' : 'file://beep.caf';
cordova.plugins.notification.local.schedule({
id: 1,
title: 'Call',
text: 'Test Message 1',
at: _5_sec_from_now,
sound: sound,
badge: 12
});
}
});
}, 1000);
}
But it is not working when my app is closed.
I haved solve this issue using following steps
1) Add these permissions in AndroiMainFest.xml
<receiver android:name=".PhoneStateReceiver" android:enabled="true"
android:exported="true">
<intent-filter android:exported="true">
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
2) Create PhoneStateReceiver.java
public class PhoneStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
// This is where you start your service
try {
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Toast.makeText(context, "Incoming Call State", Toast.LENGTH_SHORT).show();
Toast.makeText(context, "Ringing State Number is -" + incomingNumber, Toast.LENGTH_SHORT).show();
}
if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))) {
Toast.makeText(context, "Call Received State", Toast.LENGTH_SHORT).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
Toast.makeText(context, "Call Idle State", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
3) Add in MainActivity.java
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.READ_PHONE_STATE) !=
PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_CONTACTS)) {} else {
ActivityCompat.requestPermissions(this,
new String[] {
Manifest.permission.READ_PHONE_STATE
},
MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
}
}
4) You can on permission in setting > App > Your App > permission OR On programtically
Reference Link: http://www.theappguruz.com/blog/detecting-incoming-phone-calls-in-android