android广播接收者实例_IP拨号器

一、项目目录结构

二、activity_main.xml代码

<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"
    tools:context="com.zgs.bcrIPDialer.MainActivity" >

    <EditText
        android:id="@+id/et_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入要保存的ip号码" />

    <Button
        android:onClick="click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="保存" />

</LinearLayout>
三、MainActivity.java代码
package com.zgs.bcrIPDialer;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private TextView et_number;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		et_number = (TextView) findViewById(R.id.et_number);
	}

	//点击按钮 获取用户输入的iP号码 进行保存 
	public void click(View v){
		//[1]获取到用户输入的number
		String number = et_number.getText().toString().trim();
		//[2]使用sp保存起来 
		SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE); //会在私有目录下创建个config.xml文件
		//[3]存数据 
		sp.edit().putString("ipnumber", number).commit();
		Toast.makeText(getApplicationContext(), "保存成功", 0).show();
	}

}
四、NewOutGoingCallReceiver.java代码
package com.zgs.bcrIPDialer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

/**
 * 定义一个广播接收者 就要在清单文件里面配置一下
 * @author zgs
 *
 */
public class NewOutGoingCallReceiver extends BroadcastReceiver {
	//当接收到外拨电话的事件的时候回执行这个方法
	@Override
	public void onReceive(Context context, Intent intent) {
		//[0]获取到我们保存的ip号码 
		SharedPreferences sp = context.getSharedPreferences("config",0);
		//[0.1]获取我们保存的ip号码
		String ipnumber = sp.getString("ipnumber", "");


		//[1]获取当前我们要拨打的电话号码 
		String currentNumber = getResultData();

		//[1.1]判断拨打的电话是否是长途 
		if (currentNumber.startsWith("0")) {
			//[2]在当前的号码前面加上一个17951 
			setResultData(ipnumber+currentNumber);
		}
	}

}
五、AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zgs.bcrIPDialer"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="22" />
    <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=".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.zgs.bcrIPDialer.NewOutGoingCallReceiver">
            <intent-filter >
                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>
六、操作演示

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值