利用广播实现ip拨号

来源:互联网 发布:网站源码小偷v18.0 编辑:程序博客网 时间:2024/05/21 15:02
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
        <!-- 定义了一个广播接收者,new出来了一个收音机,设置action就是相当于设置了监听的频道 -->        <receiver android:name=".OutCallReceiver" >            <intent-filter>                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />            </intent-filter>        </receiver>
    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="请输入要设置的ip电话号码" />    <EditText        android:id="@+id/et_ipnumber"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:inputType="phone" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="click"        android:text="确定" />
OutCallReceiver
package org.gentry.ipdail;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;public class OutCallReceiver extends BroadcastReceiver {/** * 当有广播时间产生的时候,就会执行onReceive方法 */@Overridepublic void onReceive(Context context, Intent intent) {String number = getResultData(); // 外拨的电话号码// 替换掉这个号码SharedPreferences sp = context.getSharedPreferences("config",Context.MODE_PRIVATE);String ipnumber = sp.getString("ipnumber", "");String newnumber = ipnumber + number;// 设置外拨的电话号码setResultData(newnumber);}}
MainActivity
package org.gentry.ipdail;import android.app.Activity;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {private EditText et_ipnumber;private SharedPreferences sp;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_ipnumber = (EditText) findViewById(R.id.et_ipnumber);// 程序下次打开时edittext内容存在sp = getSharedPreferences("config", MODE_PRIVATE);et_ipnumber.setText(sp.getString("ipnumber", ""));}public void click(View view) {String ipnumber = et_ipnumber.getText().toString().trim();Editor editor = sp.edit();editor.putString("ipnumber", ipnumber);editor.commit();Toast.makeText(this, "设置完成", Toast.LENGTH_SHORT).show();}}



0 0
原创粉丝点击