2015-06-16 138 views
0

我的注意:因爲我在戰後初期已經明確,我不認爲這是一個重複的, 我已經嘗試過這些方法,它不適合我, 工作下面的代碼似乎只對2.2工作,它需要MODIFY_PHONE_STATE這是不允許的Android 2.2 ****後如何阻止呼叫在Android 4.2

這是不重複的,因爲我已經看過很多其他職位的問題在這裏,它不適合工作我 我遵循從下面的鏈接解決方案: block phone call

TelephonyManager tm = (TelephonyManager) 
context.getSystemService(Context.TELEPHONY_SERVICE); 
Class<?> c = Class.forName(tm.getClass().getName()); 
Method m = c.getDeclaredMethod("getITelephony"); 

但代碼給我例外,當真實設備(這是Android 4.2版本) java.lang.NoSuchMethodException運行:getITelephony

所以,做它仍然可以使用在Android 4.2這個解決方案,如果不,有沒有其他解決方案我可以嘗試?

感謝很多提前

+0

http://stackoverflow.com/q/24580508/1864610 –

+0

可能的重複[如何在android中阻止調用](http:// stackoverflow。com/questions/1083527 /如何在塊中調用在Android) – josedlujan

回答

1

創建一個名爲ITelephony.aidl其文件應包含以下數據:

package com.android.internal.telephony; 
interface ITelephony 
{ 

    boolean endCall();  

    void answerRingingCall();  

    void silenceRinger(); 

} 

src下

的Android>內部>電話

創建這些文件夾

然後將ITelephony.adl置於遠端假的文件夾。

複製此DeviceStateListener類並將其放置在項目的任何包下。

import android.content.Context; 
import android.os.RemoteException; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
import java.lang.reflect.Method; 

public class DeviceStateListener extends PhoneStateListener { 
    private ITelephony telephonyService; 
    private Context context; 
    public DeviceStateListener(Context context) { 
     this.context = context; 
     initializeTelephonyService(); 
    } 
    private void initializeTelephonyService() { 
     try { 
      TelephonyManager telephonyManager = (TelephonyManager) context 
        .getSystemService(Context.TELEPHONY_SERVICE); 
      Class clase = Class.forName(telephonyManager.getClass().getName()); 
      try{ 
       Method method = clase.getDeclaredMethod("getITelephony"); 
      }catch (NoSuchMethodException e){ 
       e.printStackTrace(); 
      } 
      method.setAccessible(true); 
      telephonyService = (ITelephony) method.invoke(telephonyManager); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onCallStateChanged(int state, final String incomingNumber) { 
     switch (state) { 
      case TelephonyManager.CALL_STATE_RINGING: 
       boolean isNumberIsBlocked=false; 
       // You can check here if incomingNumber string is under your blacklisted numbers 
       if (isNumberIsBlocked) { 
        try { 
       // This is the main code that block the incoming call. 
         telephonyService.endCall(); 
         Thread t = new Thread(new Runnable() { 
          @Override 
          public void run() { 
           // You can run anything here lets say a notice to the user if a call is blocked 
          } 
         }); 
         t.start(); 
        } catch (RemoteException e) { 
         e.printStackTrace(); 
        } 
       } 
       break; 
     } 
    } 
} 

這裏是另一個重要的類「ServiceReceiver」的地方,還你的項目的任何包下,並解決所有可能的進口。

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 

public class ServiceReciever extends BroadcastReceiver 
{ 

    private static TelephonyManager telephony; 
    private static DeviceStateListener phoneListener; 
    private static boolean firstTime=true; 

    public ServiceReciever(Context context) 
    { 
     telephony=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
     phoneListener=new DeviceStateListener(context); 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     if(firstTime) 
     { 
      telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE); 
      firstTime = false; 
     } 
    } 

    // You can use this in the future to stop the call blocker feature. 
    public void stopListening() { 
     telephony.listen(phoneListener, PhoneStateListener.LISTEN_NONE); 
     firstTime=true; 
    } 

} 

複製此CallBlockerService類也放在您的項目的任何包。這是一個調用ServiceReceiver類的不可驅動的服務。

import android.app.NotificationManager; 
import android.app.Service; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.IBinder; 
import com.katadigital.phone.callsmsblocker.callListener.ServiceReciever; 

public class CallBlockerService extends Service { 

    public static final int notification_id = 111; 

    // --------------------------------------- 
    // Listening Services 
    // --------------------------------------- 
    private static ServiceReciever service; 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     service = new ServiceReciever(getApplicationContext()); 
     registerReceiver(service, new IntentFilter(
       "android.intent.action.PHONE_STATE")); 
     System.out.println("Call blocker is running now"); 
    } 

    @Override 
    public void onDestroy() { 
     service.stopListening(); 
     unregisterReceiver(service); 
     service = null; 
     cancelStatusBarNotification(); 
     super.onDestroy(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     return START_STICKY; 
    } 

    public void cancelStatusBarNotification() { 
     NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     manager.cancel(notification_id); 
    } 
} 

把這個AfterBootReceiver類放在我們的CallBlockerService旁邊。它的工作是在電話從關機開始時重新啓動阻塞服務。

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 

public class AfterBootReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
      Intent serviceLauncher = new Intent(context, CallBlockerService.class); 
      context.startService(serviceLauncher); 
    } 
} 

最後把它放在你的AndroidManifest標籤下。

<receiver android:name="com.callblocker.services.AfterBootReceiver" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </receiver> 

     <service android:name="com.callblocker.services.CallBlockerService" > 
     </service> 

替換 「com.callblocker.services」 與CallBlockerService的文件夾位置和你的AfterBootReceiver

我已經測試此代碼,直到Android 4.4的奇巧。我希望你能遵循這些步驟,並幫助你解決問題。

+0

嗨icaneatclouds,謝謝你的帖子,但getITelephony的方式不適合我,你確定你的代碼工作在4.2及以上? – Gerry

+0

你好,很抱歉,很晚回覆。您是否已經在src> android> internal> telephony下創建了ITelephony.aidl? 是的上面的代碼工作從4.0及以上。我不知道它是否適用於棒棒糖Android版本 – icaneatclouds

+0

請將getITelephony包裝在NoSuchMethodException try catch中。請參閱我上面的編輯。 – icaneatclouds