溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Android如何使用Intent的Action和Data屬性實(shí)現(xiàn)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話(huà)和發(fā)送短信界面

發(fā)布時(shí)間:2021-09-27 11:50:37 來(lái)源:億速云 閱讀:163 作者:小新 欄目:編程語(yǔ)言

小編給大家分享一下Android如何使用Intent的Action和Data屬性實(shí)現(xiàn)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話(huà)和發(fā)送短信界面,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

實(shí)現(xiàn)

將布局改為L(zhǎng)inearLayout,并通過(guò)android:orientation="vertical">設(shè)置為垂直布局,然后添加id屬性。

然后添加兩個(gè)按鈕,并設(shè)置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="撥打電話(huà)"    android:layout_width="wrap_content"    android:layout_height="wrap_content"/>  <Button    android:id="@+id/send"    android:text="發(fā)送短信"    android:layout_width="wrap_content"    android:layout_height="wrap_content"/></LinearLayout>

然后來(lái)到Activity,首先通過(guò)ID獲取者兩個(gè)Button

Button buttonCall = (Button) findViewById(R.id.call);    Button buttonSend = (Button) findViewById(R.id.send);

又因?yàn)檫@兩個(gè)Button的點(diǎn)擊事件監(jiān)聽(tīng)器差不多,所有抽離出一個(gè)公共的點(diǎn)擊事件監(jiān)聽(tīng)器對(duì)象。

View.OnClickListener listener = new View.OnClickListener() {    @Override    public void onClick(View v) {      Intent intent = new Intent();      //將view強(qiáng)轉(zhuǎn)為Button      Button button = (Button) v;      //根據(jù)button的id      switch(button.getId()){        //如果是撥打電話(huà)按鈕        case R.id.call:          //設(shè)置Action行為屬性          intent.setAction(intent.ACTION_DIAL);          //設(shè)置數(shù)據(jù) 后面123456789是默認(rèn)要撥打的電話(huà)          intent.setData(Uri.parse("tel:123456789"));          startActivity(intent);          break;        case R.id.send:          //設(shè)置行為為 發(fā)送短信          intent.setAction(intent.ACTION_SENDTO);          //設(shè)置發(fā)送至 10086          intent.setData(Uri.parse("smsto:10086"));          //設(shè)置短信的默認(rèn)發(fā)送內(nèi)容          intent.putExtra("sms_body","公眾號(hào):霸道的程序猿");          startActivity(intent);          break;      }    }  };

然后在OnCreate中對(duì)按鈕設(shè)置點(diǎn)擊事件監(jiān)聽(tīng)器。

完整示例代碼

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強(qiáng)轉(zhuǎn)為Button      Button button = (Button) v;      //根據(jù)button的id      switch(button.getId()){        //如果是撥打電話(huà)按鈕        case R.id.call:          //設(shè)置Action行為屬性          intent.setAction(intent.ACTION_DIAL);          //設(shè)置數(shù)據(jù) 后面123456789是默認(rèn)要撥打的電話(huà)          intent.setData(Uri.parse("tel:123456789"));          startActivity(intent);          break;        case R.id.send:          //設(shè)置行為為 發(fā)送短信          intent.setAction(intent.ACTION_SENDTO);          //設(shè)置發(fā)送至 10086          intent.setData(Uri.parse("smsto:10086"));          //設(shè)置短信的默認(rèn)發(fā)送內(nèi)容          intent.putExtra("sms_body","公眾號(hào):霸道的程序猿");          startActivity(intent);          break;      }    }  };}

因?yàn)橛玫搅舜螂娫?huà)和發(fā)動(dòng)短信,所以需要聲明這兩個(gè)權(quán)限,打開(kāi)AndroidMainfest.xml

<!--添加打電話(huà)權(quán)限-->  <uses-permission android:name="android.permission.CALL_PHONE"/>  <!--添加發(fā)送短信權(quán)限-->  <uses-permission android:name="android.permission.SEND_SMS"/>

看完了這篇文章,相信你對(duì)“Android如何使用Intent的Action和Data屬性實(shí)現(xiàn)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話(huà)和發(fā)送短信界面”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI