电话拨号前加上ip号码(广播)

利用广播给系统拨号添加ip号码。

activity_main:

<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.zhangli.IpCall.MainActivity" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请输入电话号码" />

    <EditText
        android:id="@+id/et"
        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="call"
        android:text="拨打" />

</LinearLayout>

MainActivity:

package com.zhangli.IpCall;

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;
	private SharedPreferences sp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		et = (EditText) findViewById(R.id.et);
		//定义一个容器存号码,以便下次开启应用得到号码
		sp = getSharedPreferences("config", MODE_PRIVATE);
		et.setText(sp.getString("ipNumber", ""));
	}

	public void call(View v) {
		String ipNumber = et.getText().toString().trim();
		Editor editor = sp.edit();
		editor.putString("ipNumber", ipNumber);
		editor.commit();
		Toast.makeText(this, "得到了ip号码", Toast.LENGTH_SHORT).show();
	}

}

OutCallReceiver:

package com.zhangli.IpCall;

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 number = getResultData();
		// 添加ip号到外播的号码上
		SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
		String ipNumber = sp.getString("ipNumber", "");
		String newNumber = ipNumber + number;
		setResultData(newNumber);
	}

}

配置清单中加权限:

  <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
在application节点下定义一个监听器:

<!--定义了一个广播接收者, new出来一个收音机,设置action 就相当于设置了监听的频道  -->
        <receiver android:name=".OutCallReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>



电话拨号前加上ip号码(广播)_第1张图片电话拨号前加上ip号码(广播)_第2张图片

你可能感兴趣的:(电话拨号前加上ip号码(广播))