溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2021-03-10 14:43:06 來源:億速云 閱讀:114 作者:Leah 欄目:移動開發(fā)

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

功能:調(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)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI