Android之自學筆記(二)

電話撥號器實現


如何引入android源代碼

1、安裝 Git-1.7.0.2-preview20100309.exe

        TortoiseGit-1.4.4.0-32bit.msi

2、安裝完後,建一個用於存放源文件的文件夾,在文件夾上點擊右鍵菜單“Git Clone”,輸入項目下載路徑即可。Android源碼網址:http://android.git.kernel.org/

3、在對應平臺(比如android-8)中新建一個sources文件夾,放入下載好的源代碼。

 

在android中,查看/sources/Phone/源代碼的清單文件AndroidManifest.xml,查找到系統自帶的電話撥號器的activity: OutgoingCallBroadcaster

1、新建phone的Android應用程序

2、在value/strings.xml中定義變量

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="hello">Hello World, MainActivity!</string>

    <string name="app_name">電話撥號器</string>

    <string name="mobile">請輸入手機號</string>

    <string name="button">撥號</string>

</resources>

3、在/layout/main.xml下定義撥號器視圖

<?xml version="1.0" encoding="utf-8"?>

<!-- fill_parent充滿整個視圖

      vertical垂直佈局

 -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

    <!-- 顯示請輸入手機號文字  layout_height高度爲包裹文字的高度-->

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/mobile" />

    <!-- 輸入手機號文本框 -->

    <EditText

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:id="@+id/mobile"/>

    <!-- 撥號按鈕@+id/button 該表達式的作用是:在R文件中添加一個id內部類,並在該內部類中添加一個button常量,並且使用這個常量的值做爲這個buttonid屬性-->

    <Button

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:text="@string/button"

         android:id="@+id/button"/>

</LinearLayout>

4、在清單文件中出示電話撥打權限

<!--android爲了保護用戶的安全和隱私,當一些應用牽扯到用戶隱私和安全的時候,就要求這個應用出示這個權限,那麼軟件在安裝的時候,操作系統將會做出提示,是否允許該應用程序使用某項系統功能 -->

    <!-- 因爲默認的電話撥號activity需要有權限纔可以執行,所以這邊要要把這個權限註冊進來 -->

    <uses-permission android:name="android.permission.CALL_PHONE"/>

5、爲撥號按鈕添加點擊事件處理對象

public class MainActivity extends Activity {

    /** Called when the activity is firstcreated. */

    @Override

    public void onCreate(BundlesavedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

       /** 所有的控件都繼承自View,所以要強制轉換對控件的類型  */

       Button button = (Button) this.findViewById(R.id.button);

       /** 按鈕點擊事件  */

       button.setOnClickListener(new ButtonClickListener());

       

    }

   

    private final class ButtonClickListener implements View.OnClickListener{

        @Override

        public void onClick(View v) { // 這個參數v就是目前點擊的按鈕對象

            /** 找到該文本輸入框對象  */

            EditTextmobile = (EditText) findViewById(R.id.mobile);

            Stringnumber = mobile.getText().toString();

           

            /** 創建一個Intent實例  */

            Intentintent = new Intent();

            /** 設置OutgoingCallBroadcaster中意圖過濾器的參數  */

            intent.setAction("android.intent.action.CALL");

//          intent.addCategory("android.intent.category.DEFAULT");

            intent.setData(Uri.parse("tel:" + number));

            /** Intent實例傳給操作系統,激活符合匹配的Activity */

            startActivity(intent);// startActivity內部會自動爲Intent對象註冊一個類別,而默認的註冊類別就是DEFAULT,所以這邊可以省略

        }

    }

}

發佈了39 篇原創文章 · 獲贊 7 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章