Android使用Intent的Action和Data屬性實現點選按鈕跳轉到撥打電話和傳送簡訊介面
阿新 • • 發佈:2020-01-11
場景
點選撥打電話按鈕,跳轉到撥打電話頁面
點擊發送簡訊按鈕,跳轉到傳送簡訊頁面
注:
實現
將佈局改為LinearLayout,並通過android:orientation="vertical">
設定為垂直佈局,然後新增id屬性。
然後新增兩個按鈕,並設定Id屬性與顯示文字。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".IntentActivity"> <Button android:id="@+id/call" android:text="撥打電話" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/send" android:text="傳送簡訊" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
然後來到Activity,首先通過ID獲取者兩個Button
Button buttonCall = (Button) findViewById(R.id.call); Button buttonSend = (Button) findViewById(R.id.send);
又因為這兩個Button的點選事件監聽器差不多,所有抽離出一個公共的點選事件監聽器物件。
View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); //將view強轉為Button Button button = (Button) v; //根據button的id switch(button.getId()){ //如果是撥打電話按鈕 case R.id.call: //設定Action行為屬性 intent.setAction(intent.ACTION_DIAL); //設定資料 後面123456789是預設要撥打的電話 intent.setData(Uri.parse("tel:123456789")); startActivity(intent); break; case R.id.send: //設定行為為 傳送簡訊 intent.setAction(intent.ACTION_SENDTO); //設定傳送至 10086 intent.setData(Uri.parse("smsto:10086")); //設定簡訊的預設傳送內容 intent.putExtra("sms_body","公眾號:霸道的程式猿"); startActivity(intent); break; } } };
然後在OnCreate中對按鈕設定點選事件監聽器。
完整示例程式碼
package com.badao.relativelayouttest; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; public class IntentActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_intent); Button buttonCall = (Button) findViewById(R.id.call); Button buttonSend = (Button) findViewById(R.id.send); buttonCall.setOnClickListener(listener); buttonSend.setOnClickListener(listener); } View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); //將view強轉為Button Button button = (Button) v; //根據button的id switch(button.getId()){ //如果是撥打電話按鈕 case R.id.call: //設定Action行為屬性 intent.setAction(intent.ACTION_DIAL); //設定資料 後面123456789是預設要撥打的電話 intent.setData(Uri.parse("tel:123456789")); startActivity(intent); break; case R.id.send: //設定行為為 傳送簡訊 intent.setAction(intent.ACTION_SENDTO); //設定傳送至 10086 intent.setData(Uri.parse("smsto:10086")); //設定簡訊的預設傳送內容 intent.putExtra("sms_body","公眾號:霸道的程式猿"); startActivity(intent); break; } } }; }
因為用到了打電話和發動簡訊,所以需要宣告這兩個許可權,開啟AndroidMainfest.xml
<!--新增打電話許可權--> <uses-permission android:name="android.permission.CALL_PHONE"/> <!--添加發送簡訊許可權--> <uses-permission android:name="android.permission.SEND_SMS"/>
總結
以上所述是小編給大家介紹的Android使用Intent的Action和Data屬性實現點選按鈕跳轉到撥打電話和傳送簡訊介面,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!
如果你覺得本文對你有幫助,歡迎轉載,煩請註明出處,謝謝!