2016-12-09 2 views
0

주요 활동에서 어떤 라디오 버튼이 눌러져 있는지에 따라 ITelephony를 사용하여 호출을 차단하는 앱을 만들려고합니다. 그러나, 내 onReceive 메서드를 호출 한 적이 있으므로 호출을 차단하지 않습니다. 여기 BroadcastReceiver는 onReceive 메소드를 호출하지 않습니다.

여기에 브로드 캐스트 리시버

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 메서드를 호출되지이다?

+0

당신은 확실히 당신의 메서드가 호출되지 않고 있습니까을 확인하시기 바랍니다? 진입 점의 logcat에 프린트를 추가하려고 했습니까? – TDG

+0

이러한 콜백을 수신하려면 PhoneStateListener가 있어야합니다. 따라서 onReceive가 실행되고 있지만 리스너가 없으므로 아무 것도하지 않는 것일 수 있습니까? –

답변

관련 문제