Android(java)學習筆記175:撥出電話的廣播接收者

來源:互聯網
上載者:User

標籤:

首先我們樣本工程一覽表如下:

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:撥出電話的廣播接收者

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.