溫馨提示×

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

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

怎么在Android中利用Intent實(shí)現(xiàn)一個(gè)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面

發(fā)布時(shí)間:2021-03-01 15:33:37 來(lái)源:億速云 閱讀:227 作者:戴恩恩 欄目:移動(dòng)開(kāi)發(fā)

這篇文章主要介紹了怎么在Android中利用Intent實(shí)現(xiàn)一個(gè)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面,此處通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考價(jià)值,需要的朋友可以參考下:

Android是什么

Android是一種基于Linux內(nèi)核的自由及開(kāi)放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開(kāi)放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開(kāi)發(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="撥打電話"
    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()){
        //如果是撥打電話按鈕
        case R.id.call:
          //設(shè)置Action行為屬性
          intent.setAction(intent.ACTION_DIAL);
          //設(shè)置數(shù)據(jù) 后面123456789是默認(rèn)要撥打的電話
          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()){
        //如果是撥打電話按鈕
        case R.id.call:
          //設(shè)置Action行為屬性
          intent.setAction(intent.ACTION_DIAL);
          //設(shè)置數(shù)據(jù) 后面123456789是默認(rèn)要撥打的電話
          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)橛玫搅舜螂娫捄桶l(fā)動(dòng)短信,所以需要聲明這兩個(gè)權(quán)限,打開(kāi)AndroidMainfest.xml

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

怎么在Android中利用Intent實(shí)現(xiàn)一個(gè)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面

到此這篇關(guān)于怎么在Android中利用Intent實(shí)現(xiàn)一個(gè)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面的文章就介紹到這了,更多相關(guān)怎么在Android中利用Intent實(shí)現(xiàn)一個(gè)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面的內(nèi)容請(qǐng)搜索億速云以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持億速云!

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

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎ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