android BroadcastReceiver组件简单的使用
1.清单文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.ipdail" 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=".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=".OutCallReceiver"> <intent-filter> <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> </intent-filter> </receiver> </application> </manifest>
2.layout
<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.example.ipdail.MainActivity" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请输入要设置ip电话" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/et_ip" android:inputType="phone" /> <Button android:onClick="click" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="设置" /> </LinearLayout>
3.activity
package com.example.ipdail; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends ActionBarActivity { private EditText et_ip; private SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_ip = (EditText)findViewById(R.id.et_ip); sp = getSharedPreferences("ip", MODE_PRIVATE); String ip = sp.getString("ip", ""); et_ip.setText(ip); } public void click(View view) { String ip = et_ip.getText().toString().trim(); if(TextUtils.isEmpty(ip)) { Toast.makeText(this, "设置ip不能为空", 0).show(); return; } Editor editor = sp.edit(); editor.putString("ip", ip); editor.commit(); Toast.makeText(this, "设置成功", 0).show(); } }
4.OutCallReceiver
package com.example.ipdail; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; public class OutCallReceiver extends BroadcastReceiver { //当有广播事假产生的时候就会执行onreceive方法 @Override public void onReceive(Context context, Intent intent) { //获取外拨的电话号码 String number = getResultData(); //替换掉这个号码 SharedPreferences sp = context.getSharedPreferences("ip", Context.MODE_PRIVATE); String ip = sp.getString("ip", ""); String newnumber = ip + number; //设置外拨电话号码 setResultData(newnumber); } }