標籤:
首先我們樣本工程一覽表如下:
1.首先我們還是買一個收音機,定義一個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);
}
}
2.裝電池 和 調頻道,在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"/>
3.工程代碼總覽圖:
(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>
Android(java)學習筆記175:撥出電話的廣播接收者