Android(java)学习笔记118:BroadcastReceiver之 外拨电话的广播接收者

1. 外拨电话的广播接收者:

首先我们示例工程一览表如下:

(2)首先我们还是买一个收音机,定义一个OutCallReceiver继承自BroadcastReceiver,onReceive()方法中定义了监听到广播,要执行的操作

public class OutCallReceiver extends BroadcastReceiver {

          @Override
          public void onReceive(Context context, Intent intent) {
                  SharedPreferences sp = context.getSharedPreferences("config", 0);
                  // 修改用户拨打的电话号码, 在号码前面加上17951
                  String number = getResultData();
                  System.out.println("------有外拨电话出去了。" + number);
                  if (number.startsWith("0")) {
                        setResultData(sp.getString("ipnumber", "")+ number);
                  }
                  setResultData(null);
          }

 }

 

(3)装电池调频道,在AndroidManifest.xml文件中配置:

<receiver android:name="com.itheima.ipdailer.OutCallReceiver">
              <intent-filter >
                       <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
              </intent-filter>
</receiver>

 

这里注意添加一个外接电话拨入权限
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

 

2. 工程代码总览图:

(1)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=".MainActivity" >

    <EditText
        android:id="@+id/et_ipnumber"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入要设置的ip号码前缀" />
    
    <Button 
        android:onClick="save"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="保存"
        />

</LinearLayout>

 

(2)MainActivity.java:

package com.itheima.ipdailer;

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;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_ipnumber = (EditText) findViewById(R.id.et_ipnumber);
        sp = this.getSharedPreferences("config", 0);
        et_ipnumber.setText(sp.getString("ipnumber", ""));
    }

    public void save(View view){
        String ipnumber = et_ipnumber.getText().toString().trim();
        Editor editor = sp.edit();
        editor.putString("ipnumber", ipnumber);
        editor.commit();
        Toast.makeText(this, "设置成功", 0).show();
    }

}

(3)OutCallReceiver.java:

package com.itheima.ipdailer;

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) {
        SharedPreferences sp = context.getSharedPreferences("config", 0);
        // 修改用户拨打的电话号码, 在号码前面加上17951
        String number = getResultData();
        System.out.println("------有外拨电话出去了。" + number);
        if (number.startsWith("0")) {
            setResultData(sp.getString("ipnumber", "")+ number);
        }
        setResultData(null);
    }

}

(4)AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima.ipdailer"
    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.itheima.ipdailer.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.itheima.ipdailer.OutCallReceiver"> <intent-filter > <action android:name="android.intent.action.NEW_OUTGOING_CALL"/> </intent-filter> </receiver>
    </application>

</manifest>

 

posted on 2015-08-19 10:55  鸿钧老祖  阅读(493)  评论(0编辑  收藏  举报

导航