Android 調用發(fā)送短信的方法
更新時間:2017年09月01日 15:05:24 作者:1140566087
這篇文章主要介紹了Android 調用發(fā)送短信的方法的相關資料,主要實現Android 調用短信的使用,希望通過本文能幫助到大家,需要的朋友可以參考下
Android 調用發(fā)送短信的方法
功能:調用發(fā)送短信功能
1 、 權限
<uses-permission android:name="android.permission.SEND_SMS"/>
2、具體實現
Uri smstoUri = Uri.parse("smsto:");
Intent intent = new Intent(Intent.ACTION_VIEW,smstoUri);
intent.putExtra("address","電話號碼"); // 沒有電話號碼的話為默認的,即顯示的時候是為空的
intent.putExtra("sms_body","短信內容"); // 設置發(fā)送的內容
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);
//點擊發(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); // 沒有電話號碼的話為默認的,即顯示的時候是為空的
intent.putExtra("sms_body",m); // 設置發(fā)送的內容
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ā)送短信權限 -->
<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 調用短信的方法,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:
相關文章
Android Jetpack組件之ViewModel使用詳解
Android中的ViewModel是一個可以用來存儲UI相關的數據的類。ViewModel的生命周期會比創(chuàng)建它的Activity、Fragment的生命周期長,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-04-04
Android中AlertDialog四種對話框的最科學編寫用法(實例代碼)
這篇文章主要介紹了Android中AlertDialog四種對話框的最科學編寫用法,本文通過代碼講解的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11
Android使用CountDownTimer類實現倒計時鬧鐘
這篇文章主要為大家詳細介紹了Android使用CountDownTimer類實現倒計時鬧鐘,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01
9個非常棒的Android代碼編輯器 移動開發(fā)者的最愛
這篇文章主要為大家分享了9個非常棒的Android代碼編輯器,據說這可是移動開發(fā)者的最愛,知道是哪九個Android代碼編輯器2015-12-12
詳解Android端與JavaWeb傳輸加密(DES+RSA)
這篇文章主要介紹了詳解Android端與JavaWeb傳輸加密(DES+RSA),小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
全面解析Android應用開發(fā)中Activity類的用法
這篇文章主要介紹了Android應用開發(fā)中Activity類的用法,包括Activity間的數據傳遞以及Activity的創(chuàng)建方式等,需要的朋友可以參考下2016-02-02

