歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android實現打電話的功能-使用Intent和AndroidManifset.xml中加入權限

Android實現打電話的功能-使用Intent和AndroidManifset.xml中加入權限

日期:2017/3/1 10:29:18   编辑:Linux編程
一:布局文件先設計撥號器的簡單界面,可以先用畫圖軟件構思 界面

二 :Activity中進行獲取EditText中的電話號碼,然後點擊,使用Intent(意圖)進行實現打電話的功能

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ mobile));

三:注意必須在AndroidManifset,xml文件進行打電話的權限設置

核心源代碼 (包自己去引)

  1. public class PhoneDuanXINActivity extends Activity {
  2. private EditText mobileText; //在這裡寫個成員變量,就可以直接調用
  3. @Override
  4. public void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main);
  7. mobileText = (EditText)this.findViewById(R.id.mobile); //調用成員變量mobileText 強轉給右邊
  8. // 使用匿名類進行加監聽
  9. Button button = (Button)this.findViewById(R.id.button);
  10. button.setOnClickListener(new View.OnClickListener(){
  11. public void onClick(View v){
  12. String mobile = mobileText.getText().toString(); //得到了用戶輸入的手機號
  13. Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ mobile )); //意圖 Intent(行為, 行為數據) intent類用於描述一個應用將會做什麼事 在windos中就有種東西 開始菜單》》運行》》 http://www.baidu.com 它可以直接打開,這是為什麼? 是因為浏覽器 認出了http:
  14. startActivity(intent); //這就是內部類訪問外部類的實例
  15. }
  16. });
  17. }
  18. }

strings.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello">下午好</string>
  4. <string name="app_name">摩托羅拉手機 撥號器</string>
  5. <string name="mobile">請輸入號碼</string>
  6. <string name="button">撥打</string>
  7. </resources>
Copyright © Linux教程網 All Rights Reserved