2016-12-09 5 views
0

Ich versuche eine App zu erstellen, die Anrufe mit ITelefony blockiert, je nachdem, welcher Radioknopf in meiner Hauptaktivität gedrückt wird. Meine onReceive-Methode wird jedoch nie aufgerufen und blockiert daher den Anruf nicht.BroadcastReceiver ruft onReceive-Methode nicht auf

Hier ist meine Klasse, die 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; 
     } 

    } 

und hier ist mein 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> 

Warum ist es nicht den Aufruf der OnReceive Methode erweitert?

+0

Sind Sie sicher, Ihre Methode wird nicht aufgerufen werden? Haben Sie versucht, dem Logcat am Einstiegspunkt einen Ausdruck hinzuzufügen, um es zu überprüfen? – TDG

+0

Sie müssen einen PhoneStateListener haben, um diese Callbacks zu erhalten, also läuft vielleicht onReceive, tut aber nichts, weil es keinen Listener gibt? –

Antwort

Verwandte Themen