Android中使用廣播接收者實現IP撥號

來源:互聯網
上載者:User

標籤:broadcastreceiver   安卓   android   application   廣播接收者   

布局檔案中定義一下UI,雖然沒什麼UI.....
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:orientation="vertical"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" /><EditText     android:id="@+id/et_number"    android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:inputType="phone"    /><Button     android:layout_height="wrap_content"    android:layout_width="fill_parent"    android:text="儲存ip號碼"    android:onClick="save"    /></LinearLayout >

在mainactivity中實現部分代碼
package com.wzw.ipdial;import android.app.Activity;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {private EditText etNumber;private SharedPreferences sp;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);etNumber=(EditText) findViewById(R.id.et_number);sp=getSharedPreferences("config", MODE_PRIVATE);}public void save(View v){String ipnumber=etNumber.getText().toString().trim();if(TextUtils.isEmpty(ipnumber)){Toast.makeText(this, "清除IP號碼成功", 0).show();}else{Toast.makeText(this, "設定IP號碼成功", 0).show();}Editor editor=sp.edit();editor.putString("ipnumber", ipnumber);editor.commit();}}

建立一個類繼承廣播接收者
package com.wzw.ipdial;/** * 定義一個收音機 */import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.util.Log;public class Ipdial extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubString number=getResultData();SharedPreferences sp=context.getSharedPreferences("config", context.MODE_PRIVATE);String ipnumber=sp.getString("ipnumber", "");setResultData(ipnumber+number);}}


資訊清單檔中給許可權
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.wzw.ipdial"    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.wzw.ipdial.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.wzw.ipdial.Ipdial">            <intent-filter >                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>                </intent-filter>        </receiver>    </application></manifest>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.