首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BroadcastReceiver不会调用onReceive方法

BroadcastReceiver不会调用onReceive方法
EN

Stack Overflow用户
提问于 2016-12-10 02:51:14
回答 1查看 108关注 0票数 0

我正在尝试制作一个应用程序,根据我的主活动中按下的单选按钮,使用ITelephony阻止呼叫。但是,我的onReceive方法从未被调用过,因此不会阻塞调用。

下面是我的扩展BroadcastReceiver的类

代码语言:javascript
复制
   public class CallBlocking extends BroadcastReceiver {
        private static final int MODE_WORLD_READABLE = 1;
        private String mPhoneNumber;
        private String mCallerName;
        private SharedPreferences mPreferences;


        @Override
        public void onReceive(Context context, Intent intent) {
            mPreferences = context.getSharedPreferences("mPreferences", MODE_WORLD_READABLE);
            String blockMode = mPreferences.getString("mode", "not retrieved");
            if (!blockMode.equals("cancel")) {
                Bundle b = intent.getExtras();
                String phoneState = b.getString(TelephonyManager.EXTRA_STATE);
                if ((phoneState.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))) {
                    mPhoneNumber = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                    if (blockMode.equals("all")) {
                        disconnect(context);
                    } else if (blockMode.equals("unsaved")) {
                        mCallerName = getContactName( mPhoneNumber, context);
                        if((mCallerName == null) || (mCallerName.length() < 2))
                            disconnect(context);
                        else if (blockMode.equals("list"))
                        {
                           if(CallBlockerFragment.sBlockedList.contains(new BlockedList(mPhoneNumber)))
                              disconnect(context);
                        }
                    }
                }
            }
        }

        @SuppressWarnings({"rawtypes", "unchecked"})
        private void disconnect(Context context) {
            ITelephony telephonyService;
            TelephonyManager telephony = (TelephonyManager)
                    context.getSystemService(Context.TELEPHONY_SERVICE);
            try {
                Class c = Class.forName(telephony.getClass().getName());
                Method m = c.getDeclaredMethod("getITelephony");
                m.setAccessible(true);
                telephonyService = (ITelephony) m.invoke(telephony);
                telephonyService.endCall();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public String getContactName(String phoneNumber, Context context) {
            Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
            String callerName = "?";
            String data = null;
            ContentResolver contentResolver = context.getContentResolver();
            Cursor findContact = contentResolver.query(uri, new String[] {BaseColumns._ID,
                ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);

            try {
                if (findContact != null && findContact.getCount() > 0) {
                    findContact.moveToNext();
                    data = findContact.getString((findContact.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
                }
            }
            finally
            {
                if(findContact != null)
                    findContact.close();
            }

            return data;
        }

    }

这是我的AndroidManifest.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="edu.uwp.sean.pike.csci323.callblocker">

    <application
        android:allowBackup="true"
        android:icon="@drawable/quevedo"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".CallBlockerActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".AddToBlockedListActivity">
            android:label="@string/app_name"
            android:parentActivityName=".CallBlockerActivity">
        </activity>
        <activity
            android:name=".BlockedListActivity">
            android:label="@string/app_name"
            android:parentActivityName=".CallBlockerActivity">
        </activity>
        <receiver  android:name=".CallBlocking">
            <intent-filter  android:priority="100" >
                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter>
        </receiver>
    </application>

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

</manifest>

为什么它不调用onReceive方法呢?

EN

回答 1

Stack Overflow用户

发布于 2016-12-10 05:32:05

也许您想要实现的功能是监听电话状态changes...please请查看以下链接Listen to Change of Phone State State - Android

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41067038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档