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

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

注:
實(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">
<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>
然后來到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)聽器對(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)聽器。
完整示例代碼
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)限,打開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屬性實(shí)現(xiàn)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- Android顯式Intent與隱式Intent的使用詳解
- Android Intent傳遞大量數(shù)據(jù)出現(xiàn)問題解決
- Android開發(fā)Intent跳轉(zhuǎn)傳遞list集合實(shí)現(xiàn)示例
- Android13?加強(qiáng)Intent?filters?的安全性
- android使用intent傳遞參數(shù)實(shí)現(xiàn)乘法計(jì)算
- Android Intent傳遞數(shù)據(jù)大小限制詳解
- Android開發(fā)中Intent.Action各種常見的作用匯總
- Android使用Intent隱式實(shí)現(xiàn)頁面跳轉(zhuǎn)
- Android Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)的兩種方法
- Android Intent基礎(chǔ)用法及作用詳解
相關(guān)文章
Android TextView顯示Html類解析的網(wǎng)頁和圖片及自定義標(biāo)簽用法示例
這篇文章主要介紹了Android TextView顯示Html類解析的網(wǎng)頁和圖片及自定義標(biāo)簽用法,實(shí)例分析了Android中TextView控件的使用技巧,需要的朋友可以參考下2016-07-07
Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果(八)
這篇文章主要為大家詳細(xì)介紹了Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果的第八篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Kotlin注解實(shí)現(xiàn)Parcelable序列化流程詳解
有時(shí)我們會(huì)在界面跳轉(zhuǎn)的過程中,做對(duì)象傳值,這時(shí)就需要對(duì)該對(duì)象做序列化處理了。Android中對(duì)對(duì)象的序列化處理有兩種方式,這篇文章主要介紹了Kotlin注解實(shí)現(xiàn)Parcelable序列化2022-12-12
android與asp.net服務(wù)端共享session的方法詳解
這篇文章主要給大家介紹了關(guān)于android與asp.net服務(wù)端如何共享session的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)下吧。2017-09-09
Android中TabLayout結(jié)合ViewPager實(shí)現(xiàn)頁面切換效果
這篇文章主要介紹了Android中TabLayout結(jié)合ViewPager實(shí)現(xiàn)頁面切換效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android實(shí)現(xiàn)倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)倒計(jì)時(shí)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
Android實(shí)現(xiàn)listview動(dòng)態(tài)加載數(shù)據(jù)分頁的兩種方法
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)listview動(dòng)態(tài)加載的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
Android編程實(shí)現(xiàn)兩個(gè)Activity相互切換而不使用onCreate()的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)兩個(gè)Activity相互切換而不使用onCreate()的方法,結(jié)合實(shí)例形式分析了多個(gè)Activity切換而不重新創(chuàng)建的操作技巧,需要的朋友可以參考下2017-01-01

