1.布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ma_itc_et"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="确认"
android:onClick="yes"/>
</LinearLayout>
2.java代码
public class MainActivity extends AppCompatActivity {
private EditText ma_itc_et;
private String number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ma_itc_et = (EditText) findViewById(R.id.ma_itc_et);
//从SharedPreferences获取保存号码的路径
SharedPreferences spe = this.getSharedPreferences("yyy", this.MODE_PRIVATE);
//根据路径来得到取出保存的号码
String data =spe.getString("code", "");
ma_itc_et.setText(data);
}
public void yes(View view) {
number = ma_itc_et.getText().toString();
//把输入框里面的值通过SharedPreferences保存,第一个参数是取个文件名
SharedPreferences.Editor editor = getSharedPreferences("yyy", MODE_WORLD_WRITEABLE).edit();
//第一个参数是键,第二个是电话号码
editor.putString("code", number);
editor.commit();
}
}
3.注册一个广播
public class MyphoneState extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.PHONE_STATE".equals(intent.getAction())) {
String phone = intent.getStringExtra("incoming_number");
//从SharedPreferences获取保存号码的路径
SharedPreferences spe = context.getSharedPreferences("yyy", context.MODE_PRIVATE);
//根据路径来得到取出保存的号码
String data =spe.getString("code", "");
//把来电的号码跟你输入的号码判断
//其实可以不用判断也不用页面那样的话任何来电都会挂断
if (phone.equals(data)) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
int state = telephonyManager.getCallState();
switch (state) {
//来电状态
case TelephonyManager.CALL_STATE_RINGING:
//得到TelephonyManager的Class对象
Class<TelephonyManager> telephonyManagerClass = TelephonyManager.class;
try {
//得到TelephonyManager.getITelephony方法的Method对象
Method method = telephonyManagerClass.getDeclaredMethod("getITelephony", null);
//允许访问私有方法
method.setAccessible(true);
//调用getITelephony方法发挥ITelephony对象
ITelephony telephony = (ITelephony) method.invoke(telephonyManager, null);
//挂断电话
telephony.endCall();
} catch (Exception e) {
e.printStackTrace();
}
break;
//通话状态
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
//挂断状态
case TelephonyManager.CALL_STATE_IDLE:
break;
}
}
}
}
}
4.在AndroidManifest文件添加权限和配置广播
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<!--允许一个程序初始化一个电话拨号不需通过拨号用户界面需要用户确认-->
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<receiver android:name=".MyphoneState">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>
5.在项目下建个aidl文件夹,把这两个文件夹复制进去