1、创建activity_main.xml
<LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请输入设置的ip电话号码" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="phone" > </EditText> <Button android:id="@+id/button1" android:onClick="click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定" /> </LinearLayout>
2、创建MainActivity.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.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_number; private SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_number = (EditText) findViewById(R.id.editText1); sp=getSharedPreferences("config", MODE_PRIVATE); String ipNumber=sp.getString("number", ""); et_number.setText(ipNumber); } public void click(View view) { Editor editor=sp.edit(); editor.putString("number", et_number.getText().toString().trim()); editor.commit(); Toast.makeText(this, "设置完成", 0).show(); } }
3、编写OutCallReceiver.java类
/** * */ package com.example.ipdail; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.AvoidXfermode.Mode; /** * @author hyzhou * * 2013-12-5 */ public class OutCallReceiver extends BroadcastReceiver { /* (non-Javadoc) * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent) */ @Override public void onReceive(Context context, Intent intent) { // 获得外拨电话号码 String number=getResultData(); SharedPreferences sp=context.getSharedPreferences("config",context.MODE_PRIVATE); String ipnumber=sp.getString("number", ""); String newnumber=ipnumber+number; setResultData(newnumber); } }
4、注册广播AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.ipdail" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <!-- 添加外拨权限 -->> <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.ipdail.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> <!-- 定义一个广播接收者 new 设置action --> <receiver android:name="com.example.ipdail.OutCallReceiver" > <intent-filter> <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> </intent-filter> </receiver> </application> </manifest>