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

來源:互聯網
上載者:User

資料視頻可參考傳智播客  打電話視頻

 

 

  一:布局檔案先設計撥號器的簡單介面,可以先用畫圖軟體構思 介面

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

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

 

   三:注意必須在AndroidManifset,xml檔案進行打電話的使用權限設定 

 

    

 

核心原始碼 (包自己去引)

 

public class PhoneDuanXINActivity extends Activity {<br /> private EditText mobileText; //在這裡寫個成員變數,就可以直接調用</p><p> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> mobileText = (EditText)this.findViewById(R.id.mobile); //調用成員變數mobileText 強轉給右邊</p><p> // 使用匿名類進行加監聽<br /> Button button = (Button)this.findViewById(R.id.button);<br /> button.setOnClickListener(new View.OnClickListener(){<br /> public void onClick(View v){</p><p> String mobile = mobileText.getText().toString(); //得到了使用者輸入的手機號<br /> Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ mobile )); //意圖 Intent(行為, 行為資料) intent類用於描述一個應用將會做什麼事 在windos中就有種東西 開始菜單》》運行》》 http://www.baidu.com 它可以直接開啟,這是為什嗎? 是因為瀏覽器 認出了http:<br /> startActivity(intent); //這就是內部類訪問外部類的執行個體<br /> }<br /> });<br /> }<br />}

strings.xml

 

<?xml version="1.0" encoding="utf-8"?><br /><resources><br /> <string name="hello">下午好</string><br /> <string name="app_name">摩托羅拉手機 撥號器</string><br /><string name="mobile">請輸入號碼</string><br /><string name="button">撥打</string><br /></resources><br />

main.xml

 

 

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:orientation="vertical" ></p><p> <TextView<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="@string/mobile"<br /> /><br /><EditText<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:id = "@+id/mobile"<br /> /><br /><Button<br /> android:layout_width ="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text = "@string/button"<br /> android:id = "@+id/button"<br /> />

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

</LinearLayout>

 

這裡其實最重要的就要屬使用權限設定,打電話許可權,  還有理解 intent 意圖 (行為,行為資料)的用法 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.