2015-06-16 93 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