如何在PhoneGap Android应用程序中像truecaller一样在来电屏幕上弹出窗口

问题描述 投票:-1回答:1

在我的应用程序中,我想在PhoneGap for android应用程序中打开一个调用者名称如Truecaller的窗口,用于传入/传出呼叫。我的意思是当我接到电话或拨打电话时,即使我的应用程序关闭,我也想打开一个弹出窗口。

我的守则

// 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);
}

但是当我的应用关闭时,它无法正常工作。

android cordova ionic-framework phonegap-plugins cordova-plugins
1个回答
1
投票

我已经使用以下步骤解决了这个问题

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)您可以在设置>应用程序>您的应用程序>权限或以编程方式设置权限

参考链接:http://www.theappguruz.com/blog/detecting-incoming-phone-calls-in-android

© www.soinside.com 2019 - 2024. All rights reserved.