Android使用SharedPreferences保存讀取文件,BroadcastReceiver廣播模擬IP撥號

File: MainActivity.java

將 輸入框裡的 IP號碼保存到 SharedPreferences 指定的 config.xml文件中,再將數據回顯出來。

package com.jiangge.ipdial;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	EditText et_ipnumber;
	Button btn_confirm;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		et_ipnumber = (EditText) findViewById(R.id.et_phonenumber);
		btn_confirm = (Button) findViewById(R.id.btn_confirm);
		
		SharedPreferences share = getSharedPreferences("config", MODE_PRIVATE);
		String value = share.getString("ipnumber", "");
		et_ipnumber.setText(value);
		
		btn_confirm.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String ipnumber = et_ipnumber.getText().toString().trim();
				SharedPreferences share = getSharedPreferences("config", MODE_PRIVATE);
				Editor editor = share.edit();
				editor.putString("ipnumber", ipnumber);
				editor.commit();
			}
		});
	}

}

File:OutCallReceiver.java

從廣播中獲取外撥號碼,並和 IP 號碼拼湊成新的號碼( 也即 IP+oldNum)

package com.jiangge.ipdial;

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 shared = context.getSharedPreferences("config", Context.MODE_PRIVATE);
		String ipnumber = shared.getString("ipnumber", "");
		String phoneNum = getResultData();//從廣播中獲取外撥電話號碼
		System.out.println("===>>>>>" + phoneNum);
		
		String newNum = ipnumber + phoneNum;
		setResultData(newNum); //設置新的IP外撥電話號碼
		System.out.println("===>>" + newNum);
	}
}

清單文件:



    
    

    
        
            
                

                
            
        
		
        
            
                
            
        
    

註意:

配置 receiver 節點

外撥電話權限

佈局文件:


    

    

        
    

    

效果圖片:

vcD4KPHA+PGltZyBzcmM9″https://www.aiwalls.com/uploadfile/Collfiles/20131202/20131130173648765.jpg” alt=””>

發佈留言