android 监听去电实现ip拨号 广播接收者

利用广播实现ip拨号

布局文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

              android:orientation="vertical"

              android:layout_width="fill_parent"

              android:layout_height="fill_parent"

        >



    <TextView

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="请输入ip号码前缀"

            />    

    

    <EditText

            android:id="@+id/phoneNum"

            android:inputType="number"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"/>



    <Button

            android:text="保存"

            android:onClick="click"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"/>

</LinearLayout>

activity:

package com.heyiyong.ipdial;



import android.app.Activity;

import android.content.Context;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.view.View;

import android.widget.EditText;



public class MyActivity extends Activity {

    EditText editText;



    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);



        this.editText = (EditText) findViewById(R.id.phoneNum);



        SharedPreferences sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE);

        String number = sharedPreferences.getString("number", "");

        editText.setText(number);

    }



    public void click(View view) {

        SharedPreferences sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE);

        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.putString("number", editText.getText().toString());

        editor.commit();

    }

}

接收者:

package com.heyiyong.ipdial;



import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.SharedPreferences;



/**

 * Created by Administrator on 14-1-2.

 */

public class DialReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {

        String currentNumber = getResultData();

        //获取设置的ip号前缀

        SharedPreferences sharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);

        String ipNumber = sharedPreferences.getString("number","");

        String newNumber = ipNumber+currentNumber;

        setResultData(newNumber);

    }

}

清单文件:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

          package="com.heyiyong.ipdial"

          android:versionCode="1"

          android:versionName="1.0">



    <uses-sdk android:minSdkVersion="18"/>



    <!--去电号码获取-->

    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>



    <application

            android:label="@string/app_name" android:icon="@drawable/ic_launcher">

        <activity

                android:name="MyActivity"

                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=".DialReceiver">

            <intent-filter>

                <!--监听去电-->

                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>

            </intent-filter>

        </receiver>



    </application>

</manifest>

 

你可能感兴趣的:(android)