歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android實現電話撥號器和短信發送器

Android實現電話撥號器和短信發送器

日期:2017/3/1 11:17:30   编辑:Linux編程

電話撥號器

實現原理:用戶輸入電話號碼,當點擊撥打的時候,由監聽對象捕獲,監聽對象通過文本控件獲取到用戶輸入的電話號碼,由於系統已經實現了電話撥號功能,所以我們只需要調用這個功能就可以了。

步驟:

1.界面布局

2.編寫Activity

3.使用意圖過濾器激活電話撥號功能

4.添加電話服務權限(用手機的電話服務,要在清單文件AndroidManifest.xml中添加電話服務權限)

如圖所示這三個控件是垂直擺放的,所以要使用線性布局來擱置顯示控件

效果圖:

界面布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <!--提示信息-->
  8. <TextView
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="@string/Mobile"
  12. />
  13. <!--文本框按鈕-->
  14. <EditText
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:id="@+id/moblie"
  18. />
  19. <!--撥號按鈕 -->
  20. <Button
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:text="@string/button"
  24. android:id="@+id/button"
  25. />
  26. </LinearLayout>

Activity:

  1. package cn.test.phone;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. public class MainActivity extends Activity {
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. //根據控件的id查找到按鈕控件
  15. Button button =(Button)this.findViewById(R.id.button);
  16. button.setOnClickListener(new ButtonClickLister()); //點擊事件的處理對象
  17. }
  18. //監聽對象實現撥打功能
  19. private class ButtonClickLister implements View.OnClickListener{
  20. public void onClick(View v){
  21. EditText mobileText=(EditText)findViewById(R.id.moblie);
  22. String moblie=mobileText.getText().toString(); //獲取到用戶輸入的時間
  23. Intent intent =new Intent();
  24. intent.setAction("android.intent.action.CALL");
  25. intent.setData(Uri.parse("tel:"+moblie));
  26. //根據意圖過濾器參數激活電話撥號功能
  27. startActivity(intent);
  28. }
  29. }
  30. }

添加電話服務權限:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="cn.itcast.action"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. 略....
  7. <uses-sdk android:minSdkVersion=“6" />
  8. <!-- 電話服務權限 -->
  9. <uses-permission android:name="android.permission.CALL_PHONE"/>
  10. </manifest>
Copyright © Linux教程網 All Rights Reserved