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","電話號碼"); // 沒有電話號碼的話為默認(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); // 沒有電話號碼的話為默認(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)用短信的方法,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android應(yīng)用中利用ViewPager實(shí)現(xiàn)多頁面滑動(dòng)切換效果示例
這篇文章主要介紹了Android應(yīng)用中利用ViewPager實(shí)現(xiàn)多頁面滑動(dòng)切換效果示例,配合了Fragment和PagerTabStrip,需要的朋友可以參考下2016-03-03Android查看電池電量的方法(基于BroadcastReceiver)
這篇文章主要介紹了Android查看電池電量的方法,結(jié)合實(shí)例分析了Android使用BroadcastReceiver實(shí)現(xiàn)針對電池電量的查詢技巧,需要的朋友可以參考下2016-01-01Android中buildToolVersion與CompileSdkVersion的區(qū)別
今天小編就為大家分享一篇關(guān)于Android中buildToolVersion與CompileSdkVersion的區(qū)別,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12android實(shí)現(xiàn)可上下回彈的scrollview
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)可上下回彈的scrollview,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android使用WebView實(shí)現(xiàn)文件下載功能
這篇文章主要為大家詳細(xì)介紹了Android使用WebView實(shí)現(xiàn)文件下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05android編程實(shí)現(xiàn)的自定義注釋模板實(shí)例
這篇文章主要介紹了android編程實(shí)現(xiàn)的自定義注釋模板,以完整實(shí)例形式分析了Android自定義魔板的定義及具體實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2015-11-11Android開發(fā)中setContentView和inflate的區(qū)別分析
這篇文章主要介紹了Android開發(fā)中setContentView和inflate的區(qū)別,較為詳細(xì)的分析了setContentView和inflate的功能、用法及二者的區(qū)別,需要的朋友可以參考下2016-07-07