##1. 佈局文件 <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" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="設置ip號" /> <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="提交" android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="save" />
</LinearLayout>android
##2 main文件app
package com.tang.ipdail; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.Toast; import android.os.Build; public class MainActivity extends Activity { private EditText et; private SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et = (EditText) findViewById(R.id.et); sp = getSharedPreferences("config", MODE_PRIVATE); String num = sp.getString("number", ""); et.setText(num); } public void save(View view) { String number = et.getText().toString().trim(); Editor editor = sp.edit(); editor.putString("number", number); editor.commit(); Toast.makeText(this, "設置成功", Toast.LENGTH_SHORT).show(); } }
##3 清單文件(AndroidMainfest.xml)ide
<receiver android:name=".OutCallReceiver"> <intent-filter > <action android:name="android.intent.action.NEW_OUTGOING_CALL"/> </intent-filter> </receiver>
##4. 定義 OutCallReceiver 類佈局
package com.tang.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) { SharedPreferences sp = context.getSharedPreferences("config", context.MODE_PRIVATE); String ip = sp.getString("number", ""); String number = getResultData(); String newNum = ip+number; System.out.println(newNum); setResultData(newNum); } }