接收方在呼叫设备时忽略screen_on动作

时间:2018-07-01 14:30:22

标签: android

我正在尝试使用接收器在设备屏幕打开时接收广播,问题是如果电话关闭并且有人打电话,那么接收器会收到“ SCREEN_ON”事件广播,

如果电话正在接听电话,我如何忽略screen_on事件?

我的Maniftest.xml

    <receiver
        android:name="com.lockscreen.wipe.MainActivity$DeviceAdminSampleReceiver"
        android:description="@string/sample_device_admin_description"
        android:label="@string/sample_device_admin"
        android:permission="android.permission.BIND_DEVICE_ADMIN">
        <meta-data
            android:name="android.app.device_admin"
            android:resource="@xml/device_admin_sample" />

        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            <action android:name="android.intent.action.SCREEN_ON" />
        </intent-filter>
    </receiver>

我的Receiver.java

package com.lockscreen.wipe;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class LockscreenIntentReceiver extends BroadcastReceiver {

    // Handle actions and display Lockscreen
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)
                || intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            start_lockscreen(context);
        }

    }

    // Display lock screen
    private void start_lockscreen(Context context) {
        Intent mIntent = new Intent(context, LockScreenActivity.class);
        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);
    }

}

1 个答案:

答案 0 :(得分:0)

您可以跟踪“呼叫”的状态。在这里看看:How to detect incoming calls, in an Android device?

一种方法是在收到action start_lockscreen()

后中止TelephonyManager.CALL_STATE_RINGING