电话状态的广播接收器更改无效 [英] Broadcast receiver for Phone State changed not working

查看:96
本文介绍了电话状态的广播接收器更改无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个用于更改电话状态的广播接收器。但广播无法正常工作。我已经尝试了几个小时并尝试了2,3个解决方案,但仍然无法正常工作。互联网上的其他人具有相同的代码,对于他们来说工作正常。我不知道我在哪里犯错。需要你的帮助!
这是我的清单文件

i have created a broadcast receiver for Phone state change. but the broadcast is not working. i have been trying from couple of hours and tried 2,3 solutions but still its not working. other guys over internet have same code and the is working fine for them. i don't know where i am making mistake. need your help! Here are my manifest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="veclar.map.callandsmsblocking">
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
        </activity>
        <receiver android:name=".PhoneCallReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
            </intent-filter>
        </receiver>
    </application>
</manifest> 

这是我的PhoneCallReceiver类

and here is my PhoneCallReceiver class

public abstract class PhoneCallReceiver extends BroadcastReceiver {

    //The receiver will be recreated whenever android feels like it.  We need a static variable to remember data between instantiations

    private static int lastState = TelephonyManager.CALL_STATE_IDLE;
    private static Date callStartTime;
    private static boolean isIncoming;
    private static String savedNumber;  //because the passed incoming is only valid in ringing

    @Override
    public void onReceive(Context context, Intent intent) {

        //We listen to two intents.  The new outgoing call only tells us of an outgoing call.  We use it to get the number.
        if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
            savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
        }
        else{
            String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
            String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            int state = 0;
            if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                state = TelephonyManager.CALL_STATE_IDLE;
            }
            else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
                state = TelephonyManager.CALL_STATE_OFFHOOK;
            }
            else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
                state = TelephonyManager.CALL_STATE_RINGING;
            }

            onCallStateChanged(context, state, number);
        }
    }

    //Incoming call-  goes from IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when its hung up
    //Outgoing call-  goes from IDLE to OFFHOOK when it dials out, to IDLE when hung up
    public void onCallStateChanged(Context context, int state, String number) {
        if(lastState == state){
            //No change, debounce extras
            return;
        }
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                isIncoming = true;
                callStartTime = new Date();
                savedNumber = number;

                Toast.makeText(context, "Incoming Call Ringing" , Toast.LENGTH_SHORT).show();
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                //Transition of ringing->offhook are pickups of incoming calls.  Nothing done on them
                if(lastState != TelephonyManager.CALL_STATE_RINGING){
                    isIncoming = false;
                    callStartTime = new Date();
                    Toast.makeText(context, "Outgoing Call Started" , Toast.LENGTH_SHORT).show();
                }

                break;
            case TelephonyManager.CALL_STATE_IDLE:
                //Went to idle-  this is the end of a call.  What type depends on previous state(s)
                if(lastState == TelephonyManager.CALL_STATE_RINGING){
                    //Ring but no pickup-  a miss
                    Toast.makeText(context, "Ringing but no pickup" + savedNumber + " Call time " + callStartTime +" Date " + new Date() , Toast.LENGTH_SHORT).show();
                }
                else if(isIncoming){

                    Toast.makeText(context, "Incoming " + savedNumber + " Call time " + callStartTime  , Toast.LENGTH_SHORT).show();
                }
                else{

                    Toast.makeText(context, "outgoing " + savedNumber + " Call time " + callStartTime +" Date " + new Date() , Toast.LENGTH_SHORT).show();

                }

                break;
        }

        lastState = state;
    }
}


推荐答案

您无法再通过这种方式接收PHONE_STATE_CHANGED广播。

You canno longer receive PHONE_STATE_CHANGED broadcast this way.

来自官方Android开发人员指南 https://developer.android.com/guide/components/broadcasts

From official Android developer guide https://developer.android.com/guide/components/broadcasts :


从Android开始8.0(API级别26),系统会对清单声明的接收者施加
的附加限制。

Beginning with Android 8.0 (API level 26), the system imposes additional restrictions on manifest-declared receivers.

如果您的应用程序针对Android 8.0或更高版本,则不能使用清单
声明大多数隐式广播的接收方(
并非专门针对您的应用的广播)。当用户正在积极使用您的应用时,您仍然可以使用
上下文注册的接收器。

If your app targets Android 8.0 or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that don't target your app specifically). You can still use a context-registered receiver when the user is actively using your app.

您必须使用显式广播收件人(从您的活动中注册)以接收PHONE_STATE_CHANGED广播。

You must use explicit broadcast receivers (registered from your activity) to receive PHONE_STATE_CHANGED broadcast.

public class ToastDisplay extends Activity {

    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT);
        }
    };

    @Override
    protected void onResume() {
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.intent.action.PHONE_STATE");
        registerReceiver(receiver, filter);
        super.onResume();
    }

    @Override
    protected void onPause() {
        unregisterReceiver(receiver);
        super.onPause();
    }
}

此外,您除了声明所需的许可外,例如<清单中的code> android.permission.READ_PHONE_STATE , android.permission.PROCESS_OUTGOING_CALLS ,您必须在运行时从用户明确获得这些权限-时间。否则,您将不会收到某些(大多数)系统广播。
Android开发人员指南很好地解释了如何向用户和代码示例请求权限。
https://developer.android.com/training/permissions/requesting

Also, you in addition to declare required permission like android.permission.READ_PHONE_STATE , android.permission.PROCESS_OUTGOING_CALLS in the manifest , you must obtain those permissions explicitly from user at run-time. Otherwise you will not receive some(most) system broadcasts. Android developer guide has a nice explanation on requesting permissions from user and code sample. https://developer.android.com/training/permissions/requesting

这篇关于电话状态的广播接收器更改无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆