MainActivity
package com.example.zong.ipbohaoqi; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { 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(); SharedPreferences.Editor editor = sp.edit(); editor.putString("ipnumber", ipnumber); editor.commit(); Toast.makeText(this, "设置成功", 0).show(); } }
OutCallReceiver
package com.example.zong.ipbohaoqi; 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) { String outcallnumber = getResultData(); SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_APPEND); String ipnumber = sp.getString("ipnumber", ""); setResultData(ipnumber + outcallnumber); } }
activity_main
<?xml version="1.0" encoding="utf-8"?> <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="com.example.zong.ipbohaoqi.MainActivity"> <EditText android:id="@+id/et_ipnumber" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入IP号码" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/et_ipnumber" android:layout_centerHorizontal="true" android:onClick="click" android:text="请设置IP号码" /> </RelativeLayout>
mainfest
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>