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

来源:互联网 发布:mod达人邢凯淘宝店 编辑:程序博客网 时间:2024/05/20 20:22

一、项目目录结构

二、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;@Overrideprotected 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]获取到用户输入的numberString 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 {//当接收到外拨电话的事件的时候回执行这个方法@Overridepublic 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>
六、操作演示

0 0
原创粉丝点击