如何像 PhoneGap Android 应用中的 truecaller 一样在来电屏幕上弹出 window
How to popup window on incoming call screen like truecaller in PhoneGap Android app
在我的应用程序中,我想在 PhoneGap 中为 android 应用程序打开一个 window 来电者姓名,例如 Truecaller for incoming/outgoing call。我的意思是,当我接到电话或拨出电话时,我想打开一个弹出窗口 window,即使我的应用程序已关闭。
我的代码
// 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);
}
但是当我的应用程序关闭时它不起作用。
我已经通过以下步骤解决了这个问题
1) 在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) 创建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) 添加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) 您可以在设置 > 应用程序 > 您的应用程序 > 权限中启用权限 或 以编程方式启用
参考 Link: http://www.theappguruz.com/blog/detecting-incoming-phone-calls-in-android
在我的应用程序中,我想在 PhoneGap 中为 android 应用程序打开一个 window 来电者姓名,例如 Truecaller for incoming/outgoing call。我的意思是,当我接到电话或拨出电话时,我想打开一个弹出窗口 window,即使我的应用程序已关闭。
我的代码
// 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);
}
但是当我的应用程序关闭时它不起作用。
我已经通过以下步骤解决了这个问题
1) 在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) 创建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) 添加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) 您可以在设置 > 应用程序 > 您的应用程序 > 权限中启用权限 或 以编程方式启用
参考 Link: http://www.theappguruz.com/blog/detecting-incoming-phone-calls-in-android