以前讨论过这个主题,今天把简单代码发一下,思路可以看一下以前的帖子。现在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);
}
}
}
[源码、文档、分享] 【连云助手的防火墙挂断模式空号,停机,关机是怎么实现的】...
最新推荐文章于 2022-02-25 23:02:56 发布