繁体   English   中英

BroadcastReceiver不会调用onReceive方法

[英]BroadcastReceiver won't call onReceive method

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

这是我的类,扩展了BroadcastReceiver

   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

<?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方法?

也许您想实现的是监听电话状态变化...请查看以下链接收听电话状态变化-Android

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM