1、继承广播接收者类
package com.example.ipdail; import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences; public class OutCallReceiver extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
//获取拨打的号码
String number = getResultData();
//获取config数据里面的数据
SharedPreferences sp = context.getSharedPreferences("config", context.MODE_PRIVATE);
String ipnumber = sp.getString("ipnumber", ""); //修改号码并返回
String newNumber = ipnumber + number;
setResultData(newNumber);
} }
2、在清单文件配置广播接收者(因为属于四大组件之一),并通过action来表名要监听的频道
<receiver android:name=".OutCallReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"></action>
</intent-filter>
</receiver>
3、由于要监听外拨电话,所以要加一个权限
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
4、其他java代码
package com.example.ipdail; import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.widget.EditText; public class MainActivity extends Activity { private EditText et_ipnumber; private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_ipnumber = (EditText)findViewById(R.id.et_ipnumber);
sp = getSharedPreferences("config", MODE_PRIVATE);
et_ipnumber.setText(sp.getString("ipnumber", ""));
} public void click(View view){
String ipnumber = et_ipnumber.getText().toString().trim(); Editor editor = sp.edit();
editor.putString("ipnumber", ipnumber);
editor.commit();
}
}