日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android中使用Intent的Action和Data属性实现点击按钮跳转到拨打电话和发送短信

發(fā)布時(shí)間:2025/3/19 Android 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android中使用Intent的Action和Data属性实现点击按钮跳转到拨打电话和发送短信 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

場景

點(diǎn)擊撥打電話按鈕,跳轉(zhuǎn)到撥打電話頁面

?

點(diǎn)擊發(fā)送短信按鈕,跳轉(zhuǎn)到發(fā)送短信頁面

?

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
關(guān)注公眾號
霸道的程序猿
獲取編程相關(guān)電子書、教程推送與免費(fèi)下載。

實(shí)現(xiàn)

將布局改為LinearLayout,并通過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"><Buttonandroid:id="@+id/call"android:text="撥打電話"android:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:id="@+id/send"android:text="發(fā)送短信"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>

然后來到Activity,首先通過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)聽器差不多,所有抽離出一個(gè)公共的點(diǎn)擊事件監(jiān)聽器對象。

?View.OnClickListener listener = new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();//將view強(qiáng)轉(zhuǎn)為ButtonButton button = (Button) v;//根據(jù)button的idswitch(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ā)送至 10086intent.setData(Uri.parse("smsto:10086"));//設(shè)置短信的默認(rèn)發(fā)送內(nèi)容intent.putExtra("sms_body","公眾號:霸道的程序猿");startActivity(intent);break;}}};

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

完整示例代碼

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 {@Overrideprotected 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() {@Overridepublic void onClick(View v) {Intent intent = new Intent();//將view強(qiáng)轉(zhuǎn)為ButtonButton button = (Button) v;//根據(jù)button的idswitch(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ā)送至 10086intent.setData(Uri.parse("smsto:10086"));//設(shè)置短信的默認(rèn)發(fā)送內(nèi)容intent.putExtra("sms_body","公眾號:霸道的程序猿");startActivity(intent);break;}}}; }

因?yàn)橛玫搅舜螂娫捄桶l(fā)動短信,所以需要聲明這兩個(gè)權(quán)限,打開AndroidMainfest.xml

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

添加位置如下

?

?

總結(jié)

以上是生活随笔為你收集整理的Android中使用Intent的Action和Data属性实现点击按钮跳转到拨打电话和发送短信的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。