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

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

日期:2017/3/1 10:50:05   编辑:Linux編程

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

自己剛剛閒著沒事做了一個很簡單的撥號器,初學

步驟:

一:布局文件先設計撥號器的簡單界面

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

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


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


Demo源代碼:

  1. package com.jiangqq.activity;
  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 PhoneActivity extends Activity
  10. {
  11. // 定義EditText
  12. private EditText mobileText;
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. mobileText = (EditText) this.findViewById(R.id.mobile);
  18. Button button = (Button) this.findViewById(R.id.button);
  19. // 使用匿名類進行加監聽
  20. button.setOnClickListener(new View.OnClickListener() {
  21. @Override
  22. public void onClick(View v) {
  23. String mobile = mobileText.getText().toString();
  24. // 使用系統的電話撥號服務,必須去聲明權限,在AndroidManifest.xml中進行聲明
  25. Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
  26. + mobile));
  27. PhoneActivity.this.startActivity(intent);
  28. }
  29. });
  30. }
  31. }
Copyright © Linux教程網 All Rights Reserved