Android中使用广播接收者实现IP拨号

布局文件中定义一下UI,虽然没什么UI.....

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
	<EditText 
	    android:id="@+id/et_number"
	    android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"
	    />
	
		<Button 
		    android:layout_height="wrap_content"
		    android:layout_width="fill_parent"
		    android:text="保存ip号码"
		    android:onClick="save"
		    />
	
</LinearLayout >

在mainactivity中实现部分代码

package com.wzw.ipdial;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	private EditText etNumber;
	private SharedPreferences sp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		etNumber=(EditText) findViewById(R.id.et_number);
		sp=getSharedPreferences("config", MODE_PRIVATE);
	}

	public void save(View v){
		String ipnumber=etNumber.getText().toString().trim();
		if(TextUtils.isEmpty(ipnumber)){
			Toast.makeText(this, "清除IP号码成功", 0).show();
		}else{
			Toast.makeText(this, "设置IP号码成功", 0).show();
		}
		Editor editor=sp.edit();
		editor.putString("ipnumber", ipnumber);
		editor.commit();
		
		
	}
}

新建一个类继承广播接收者

package com.wzw.ipdial;

/**
 * 定义一个收音机
 */
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

public class Ipdial extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		String number=getResultData();
		
		SharedPreferences sp=context.getSharedPreferences("config", context.MODE_PRIVATE);
		String ipnumber=sp.getString("ipnumber", "");
		
		
		setResultData(ipnumber+number);
		
	}

}


清单文件中给权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wzw.ipdial"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.wzw.ipdial.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- 设置广播接受者,并指定接收拨打电话的动作 -->
        <receiver android:name="com.wzw.ipdial.Ipdial">
            <intent-filter >
                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
                </intent-filter>
        </receiver>
    </application>

</manifest>


Android中使用广播接收者实现IP拨号,布布扣,bubuko.com

Android中使用广播接收者实现IP拨号

上一篇:NetBios网络基础及编程


下一篇:iOS开发Swift篇—(十)方法