[源码、文档、分享] 【连云助手的防火墙挂断模式空号,停机,关机是怎么实现的】

来源:互联网 发布:淘宝网铁通固话充值 编辑:程序博客网 时间:2024/06/11 02:10

以前讨论过这个主题,今天把简单代码发一下,思路可以看一下以前的帖子。现在360等很多防火墙软件都有此功能。



public class SettingActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener{

        private ITelephony iTelephony;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
        
       PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).registerOnSharedPreferenceChangeListener(this);//registerOnSharedPreferenceChangeListener(this);
   
       mPhoneCallListener phoneListener=new mPhoneCallListener();

       TelephonyManager telMgr = (TelephonyManager)getSystemService(
           TELEPHONY_SERVICE);
     //初始化iTelephony
       Class <TelephonyManager> c = TelephonyManager.class;
       Method getITelephonyMethod = null;
       try {
             getITelephonyMethod = c.getDeclaredMethod("getITelephony", (Class[])null);
             getITelephonyMethod.setAccessible(true);
       } catch (SecurityException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       } catch (NoSuchMethodException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       }

       try {
       iTelephony = (ITelephony) getITelephonyMethod.invoke(telMgr, (Object[])null);
       } catch (IllegalArgumentException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       } catch (IllegalAccessException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       } catch (InvocationTargetException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       }
      
       telMgr.listen(phoneListener, mPhoneCallListener.
                    LISTEN_CALL_STATE);
    }
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {
            getPreferenceScreen().findPreference("incall_end_mode_pre").setSummary(getPreferenceScreen().getSharedPreferences().getString("incall_end_mode_pre", ""));
            Log.d("mode", "XYZ"+getPreferenceScreen().getSharedPreferences().getString("incall_end_mode_pre", ""));
            String str1 = "tel:";
        String str2 = "%23%2367%23";
        String str3 = "**67*13800000000%23";
        String str4 = "**67*13810538911%23";
        String str5 = "**67*13701110216%23";
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);//  
        if(tm.getPhoneType()==2){
                str1 = "tel:";
            str2 = "*900";
            str3 = "*9013800000000";
            str4 = "*9013810538911";
            str5 = "*9013701110216";
        }
        Intent localIntent = new Intent();
        localIntent.setAction("android.intent.action.CALL");
        String mode=getPreferenceScreen().getSharedPreferences().getString("incall_end_mode_pre", "");
       if(mode.equals("空号")){
                Uri localUri1 = Uri.parse(str1 + str3);
            localIntent.setData(localUri1);
            startActivity(localIntent);
        }else if(mode.equals("关机")){
                Uri localUri1 = Uri.parse(str1 + str4);
            localIntent.setData(localUri1);
            startActivity(localIntent);
        }else if(mode.equals("停机")){
                Uri localUri1 = Uri.parse(str1 + str5);
            localIntent.setData(localUri1);
            startActivity(localIntent);
        }else{
                Uri localUri1 = Uri.parse(str1 + str2);
            localIntent.setData(localUri1);
            startActivity(localIntent);
        }
    }
   
   
    public class mPhoneCallListener extends PhoneStateListener
    {
      @Override
      public void onCallStateChanged(int state, String incomingNumber)
      {
        // TODO Auto-generated method stub
        switch(state)  
        {  
        
          case TelephonyManager.CALL_STATE_IDLE:

            break;
            
        
          case TelephonyManager.CALL_STATE_OFFHOOK:

            break;
            
        
          case TelephonyManager.CALL_STATE_RINGING:
      
           
           
     
            {                 
            try
            {            

              iTelephony.endCall();
            
            }
            catch(Exception e)
            {
           
             e.printStackTrace();         
            break;
            }      
          }
        }
        
        super.onCallStateChanged(state, incomingNumber);
        
     
      }
    }
   
}

原创粉丝点击