利用广播实现ip拨号
布局文件:
activity:
package com.heyiyong.ipdial;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.os.Bundle;import android.view.View;import android.widget.EditText;public class MyActivity extends Activity { EditText editText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.editText = (EditText) findViewById(R.id.phoneNum); SharedPreferences sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE); String number = sharedPreferences.getString("number", ""); editText.setText(number); } public void click(View view) { SharedPreferences sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("number", editText.getText().toString()); editor.commit(); }}
接收者:
package com.heyiyong.ipdial;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;/** * Created by Administrator on 14-1-2. */public class DialReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { String currentNumber = getResultData(); //获取设置的ip号前缀 SharedPreferences sharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE); String ipNumber = sharedPreferences.getString("number",""); String newNumber = ipNumber+currentNumber; setResultData(newNumber); }}
清单文件: