AndroidManifest.xml
- 注冊广播接收者
- 加入权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android_ipphone"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.android_ipphone.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <receiver android:name="com.example.android_ipphone.receiver.CallBreadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application> </manifest>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置外拨的ip" /> <EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView1"
android:ems="10"
android:text="17951"
android:inputType="number" > <requestFocus />
</EditText> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText1"
android:onClick="setIp"
android:text="设置" /> </RelativeLayout>
MainActivity.java
package com.example.android_ipphone; import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText; public class MainActivity extends Activity {
private EditText et_ip; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_ip = (EditText) findViewById(R.id.editText1);
} public void setIp(View v) {
// 获取你要设置的ip
String ipnum = et_ip.getText().toString().trim();
// 保存在哪里
SharedPreferences sp = getSharedPreferences("csdn",
Context.MODE_PRIVATE); Editor editor = sp.edit();
editor.putString("ip", ipnum).commit(); }
}
CallBreadcastReceiver.java
package com.example.android_ipphone.receiver; import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences; public class CallBreadcastReceiver extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) {
// 获取设置中的ip保存SharedPreference对象
SharedPreferences sp = context.getSharedPreferences("csdn",
Context.MODE_PRIVATE);
// 获取保存外拨电话ip值
String ipNum = sp.getString("ip", "");
// 改变你拨打的电话信息
// 获取电话号码
String phoneNum = getResultData();
// 改变后的电话号码
String data = ipNum + phoneNum;
// 设置返回的数据
setResultData(data);
//当前的BreadcastReceiver
abortBroadcast();
} }