怎么在Android中調(diào)用發(fā)送的短信

怎么在Android中調(diào)用發(fā)送的短信?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

創(chuàng)新互聯(lián)建站專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、崗巴網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5建站、電子商務(wù)商城網(wǎng)站建設(shè)、集團公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為崗巴等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

功能:調(diào)用發(fā)送短信功能

1 、 權(quán)限

<uses-permission android:name="android.permission.SEND_SMS"/>

2、具體實現(xiàn)

Uri smstoUri = Uri.parse("smsto:"); 
Intent intent = new Intent(Intent.ACTION_VIEW,smstoUri); 
intent.putExtra("address","電話號碼"); // 沒有電話號碼的話為默認的,即顯示的時候是為空的 
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); 
     
    //點擊發(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); // 設(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" 
     
    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>

關(guān)于怎么在Android中調(diào)用發(fā)送的短信問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。

新聞名稱:怎么在Android中調(diào)用發(fā)送的短信
地址分享:http://muchs.cn/article40/ighdho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、虛擬主機、動態(tài)網(wǎng)站、定制網(wǎng)站商城網(wǎng)站、搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)