国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程簡單實現撥號器功能的方法

2019-12-12 02:23:46
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程簡單實現撥號器功能的方法。分享給大家供大家參考,具體如下:

學習Android已經有2天時間了,沒學習的時候覺得android可能很枯燥,但是學過之后我發覺其實這個比什么javaweb好玩多了。學習android可以見到一些很有趣的東西,這里呢也建議學習javaME的人不要在煎熬了,學習android吧。在寫程序之前也需要知道android的工作原理

1.獲取組件清單
2.登記或注冊組件
3.將組件封裝成意圖
4.把意圖交給意圖處理器進行處理
5.把界面顯示給用戶

看過網上android的開發流程,好多人都說可以把界面和activity并行開發,因為android也是遵循mvc設計模式,也就是說android也可有自己的業務層DAO。由于android發展歷史比較短,目前的分工還不是很明確,對于界面和后臺可以選擇其中一個作為自己的發展方向,對于android的任何一塊來說薪水都比較高。廢話就不多說了,來一步一步的實現功能吧。

1.編寫“文字”的配置文件,默認的配置文件是strings.xml,這里也可以重新寫一個配置文件,格式要保持一致就來寫這個配置文件(mystring.xml)吧

<?xml version="1.0" encoding="utf-8"?><resources>  <string name="tip">輸入號碼</string>  <string name="bottonname">撥打</string></resources>

2.編寫控件

<?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="@string/tip" />  <EditText android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:id="@+id/phonenumber"/>  <!-- 顯示一個文本框 id為phonenumber-->  <Button android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/bottonname"    android:id="@+id/botton"    />  <!-- 顯示一個按鈕 --></LinearLayout>

為了讓大家看的更清楚,我把R文件的內容也給大家

/* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */package org.lxh.phone;public final class R {  public static final class attr {  }  public static final class drawable {    public static final int icon=0x7f020000;  }  public static final class id {    public static final int botton=0x7f050001;    public static final int phonenumber=0x7f050000;  }  public static final class layout {    public static final int main=0x7f030000;  }  public static final class string {    public static final int app_name=0x7f040003;    public static final int bottonname=0x7f040001;    public static final int hello=0x7f040002;    public static final int tip=0x7f040000;  }}

3.編寫activity

package org.lxh.phone;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;public class PhoneActivity extends Activity {   private EditText edit;  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    edit=(EditText)this.findViewById(R.id.phonenumber);  //通過id取得文本輸入框    Button but=(Button)this.findViewById(R.id.botton);  //通過id取得按鈕    but.setOnClickListener(new MyListener()); //給按鈕添加監聽器  }  public final class MyListener implements View.OnClickListener{  //自定義的監聽器    public void onClick(View v) {      //實例化一個意圖(動作),用來撥打電話      Intent intent=new Intent("android.intent.action.CALL",Uri.parse("tel:"+edit.getText().toString()));      startActivity(intent); //封裝一個意圖    }  }}

上面是內部類的寫法,也可以使用下面的寫法

package org.lxh.activity;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;public class CallPhoneActivity extends Activity {  private EditText edittext;  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    //取得輸入框和按鈕    edittext=(EditText)this.findViewById(R.id.phonenum);    Button but=(Button)this.findViewById(R.id.button);    but.setOnClickListener(new View.OnClickListener() {      public void onClick(View v) {        String number=edittext.getText().toString();        //封裝一個意圖,用來撥打電話        Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+number));        startActivity(intent);      }    });  }}

開發的時候要注意Uri.parse不能少,tel:也不能少,少了就會出錯

這里要實現這個功能,首先要來看一下xml

<activity android:name="OutgoingCallBroadcaster"    android:permission="android.permission.CALL_PHONE"    android:theme="@android:style/Theme.NoDisplay"    android:configChanges="orientation|keyboardHidden">  <!-- CALL action intent filters, for the various ways    of initiating an outgoing call. -->  <intent-filter>    <action android:name="android.intent.action.CALL" />    <category android:name="android.intent.category.DEFAULT" />    <data android:scheme="tel" />  </intent-filter>  <intent-filter>    <action android:name="android.intent.action.CALL" />    <category android:name="android.intent.category.DEFAULT" />    <data android:scheme="voicemail" />  </intent-filter>  <intent-filter>    <action android:name="android.intent.action.CALL" />    <category android:name="android.intent.category.DEFAULT" />    <data android:mimeType="vnd.android.cursor.item/phone" />    <data android:mimeType="vnd.android.cursor.item/phone_v2" />    <data android:mimeType="vnd.android.cursor.item/person" />  </intent-filter></activity>

這里只需要看第一個filter,這里只需使用2條,那個默認的不用我們去管,另外這個也是需要獲得打電話的許可的,所以在組件清單里要加一點東西,如下所示

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"   package="org.lxh.phone"   android:versionCode="1"   android:versionName="1.0">  <application android:icon="@drawable/icon" android:label="@string/app_name">    <activity android:name=".PhoneActivity"         android:label="@string/app_name">      <intent-filter>        <action android:name="android.intent.action.MAIN" />        <category android:name="android.intent.category.LAUNCHER" />      </intent-filter>    </activity>  </application>  <uses-sdk android:minSdkVersion="7" />  <uses-permission android:name="android.permission.CALL_PHONE"/></manifest>

準備工作差不多做好了,來測試一下吧,這里為了測試方便,我弄了2個虛擬手機

電話打通了

這個比較好玩吧,至于那個應用圖標自己可以換成喜歡的,我就不改了

現在把那個strings.xml配置文件給大家

<?xml version="1.0" encoding="utf-8"?><resources>  <string name="hello">Hello World, PhoneActivity!</string>  <string name="app_name">我的手機撥號器</string></resources>

OK了,程序寫好了。

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android開發入門與進階教程》、《Android編程之activity操作技巧總結》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桐乡市| 德惠市| 寿宁县| 沙坪坝区| 深州市| 阿瓦提县| 高淳县| 桐梓县| 将乐县| 西和县| 大悟县| 疏附县| 大厂| 谢通门县| 乌拉特前旗| 尖扎县| 健康| 三台县| 高安市| 甘洛县| 奉化市| 漠河县| 北海市| 金阳县| 基隆市| 黎川县| 永安市| 青岛市| 嵊州市| 广宗县| 克什克腾旗| 介休市| 台中市| 沐川县| 蓬莱市| 鸡泽县| 上虞市| 乌拉特后旗| 巨野县| 民丰县| 呼伦贝尔市|