这里做一个IP拨号器:
如果拨打外地号码,将会自动在头部添加输入的号码
一个简单的界面:
<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" > <EditText android:id="@+id/et_ipnumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入ip号码" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="保存" /> </LinearLayout>
MainActivity:
package org.dreamtech.ipdail; import android.os.Bundle; import android.app.Activity; import android.content.SharedPreferences; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_number; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_number = (EditText) findViewById(R.id.et_ipnumber); } public void click(View v) { String ipnumber = et_number.getText().toString().trim(); SharedPreferences sp = getSharedPreferences("config", 0); sp.edit().putString("ipnumber", ipnumber).commit(); Toast.makeText(getApplicationContext(), "保存成功", 1).show(); } }
OutGoingCallReceiver:
package org.dreamtech.ipdail; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; public class OutGoingCallReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { SharedPreferences sp = context.getSharedPreferences("config", 0); String ipNumber = sp.getString("ipnumber", ""); String currentNumber = getResultData(); if (currentNumber.startsWith("0")) { setResultData(ipNumber + currentNumber); } } }
注意在配置文件中声明、加上权限:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.dreamtech.ipdail" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <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="org.dreamtech.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> <receiver android:name="org.dreamtech.ipdail.OutGoingCallReceiver" > <intent-filter> <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> </intent-filter> </receiver> </application> </manifest>