Android 調(diào)用發(fā)送短信的方法
Android 調(diào)用發(fā)送短信的方法
功能:調(diào)用發(fā)送短信功能
1 、 權(quán)限
<uses-permission android:name="android.permission.SEND_SMS"/>
2、具體實(shí)現(xiàn)
Uri smstoUri = Uri.parse("smsto:");
Intent intent = new Intent(Intent.ACTION_VIEW,smstoUri);
intent.putExtra("address","電話號(hào)碼"); // 沒(méi)有電話號(hào)碼的話為默認(rèn)的,即顯示的時(shí)候是為空的
intent.putExtra("sms_body","短信內(nèi)容"); // 設(shè)置發(fā)送的內(nèi)容
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
Activity 代碼:
public class MainActivity extends Activity {
private EditText phone ,message;
private Button sendbtn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phone = (EditText) findViewById(R.id.phone);
message = (EditText) findViewById(R.id.message);
sendbtn = (Button) findViewById(R.id.sendbtn);
//點(diǎn)擊發(fā)送短信
sendbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String p = phone.getText().toString();
String m = message.getText().toString();
Uri smstoUri = Uri.parse("smsto:"); // 解析地址
Intent intent = new Intent(Intent.ACTION_VIEW,smstoUri);
intent.putExtra("address",p); // 沒(méi)有電話號(hào)碼的話為默認(rèn)的,即顯示的時(shí)候是為空的
intent.putExtra("sms_body",m); // 設(shè)置發(fā)送的內(nèi)容
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
}
});
}
}
Mainfest.xml 配置文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.message"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.message.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!-- 發(fā)送短信權(quán)限 -->
<uses-permission android:name="android.permission.SEND_SMS" />
</manifest>
布局示意圖:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<Button
android:id="@+id/sendbtn"
style="?android:attr/buttonStyleSmall"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:text="Send" />
<EditText
android:id="@+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/sendbtn"
android:layout_alignLeft="@+id/phone"
android:layout_marginBottom="48dp"
android:ems="10" />
</RelativeLayout>
以上就是Android 調(diào)用短信的方法,如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Android Jetpack組件之ViewModel使用詳解
Android中的ViewModel是一個(gè)可以用來(lái)存儲(chǔ)UI相關(guān)的數(shù)據(jù)的類。ViewModel的生命周期會(huì)比創(chuàng)建它的Activity、Fragment的生命周期長(zhǎng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-04-04
Android中AlertDialog四種對(duì)話框的最科學(xué)編寫(xiě)用法(實(shí)例代碼)
這篇文章主要介紹了Android中AlertDialog四種對(duì)話框的最科學(xué)編寫(xiě)用法,本文通過(guò)代碼講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
Android遠(yuǎn)程服務(wù)編寫(xiě)和調(diào)用教程
這篇文章主要介紹了Android遠(yuǎn)程服務(wù)編寫(xiě)和調(diào)用教程,本文教大家如何編寫(xiě)或者調(diào)用Android的遠(yuǎn)程服務(wù),感興趣的小伙伴們可以參考一下2016-02-02
Android使用CountDownTimer類實(shí)現(xiàn)倒計(jì)時(shí)鬧鐘
這篇文章主要為大家詳細(xì)介紹了Android使用CountDownTimer類實(shí)現(xiàn)倒計(jì)時(shí)鬧鐘,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
9個(gè)非常棒的Android代碼編輯器 移動(dòng)開(kāi)發(fā)者的最愛(ài)
這篇文章主要為大家分享了9個(gè)非常棒的Android代碼編輯器,據(jù)說(shuō)這可是移動(dòng)開(kāi)發(fā)者的最愛(ài),知道是哪九個(gè)Android代碼編輯器2015-12-12
Android開(kāi)發(fā)實(shí)現(xiàn)布局中為控件添加選擇器的方法
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)布局中為控件添加選擇器的方法,涉及Android開(kāi)發(fā)中布局設(shè)置的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
詳解Android端與JavaWeb傳輸加密(DES+RSA)
這篇文章主要介紹了詳解Android端與JavaWeb傳輸加密(DES+RSA),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
全面解析Android應(yīng)用開(kāi)發(fā)中Activity類的用法
這篇文章主要介紹了Android應(yīng)用開(kāi)發(fā)中Activity類的用法,包括Activity間的數(shù)據(jù)傳遞以及Activity的創(chuàng)建方式等,需要的朋友可以參考下2016-02-02
Flutter實(shí)現(xiàn)切換應(yīng)用時(shí)隱藏應(yīng)用預(yù)覽
如果您要顯示敏感數(shù)據(jù),例如錢包金額,或者只是當(dāng)?shù)卿洷韱物@示插入的密碼清晰時(shí),當(dāng)您不在應(yīng)用程序中時(shí),您必須隱藏敏感數(shù)據(jù)。本文將利用Flutter實(shí)現(xiàn)切換應(yīng)用時(shí)隱藏應(yīng)用預(yù)覽,需要的可以參考一下2022-06-06

