在Android中,您可以使用SmsManager API或设备内置SMS应用程序来发送短信.在本教程中,我们向您展示了发送SMS消息的两个基本示例 :
SmsManager API
SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage("phoneNo", null, "sms message", null, null);
内置短信应用程序
Intent sendIntent = new Intent(Intent.ACTION_VIEW); sendIntent.putExtra("sms_body", "default content"); sendIntent.setType("vnd.android-dir/mms-sms"); startActivity(sendIntent);
当然,两者都需要 SEND_SMS权限.
<uses-permission android:name="android.permission.SEND_SMS" />
除了上述方法之外,SmsManager类中还有其他一些重要的功能.这些方法列在下面 :
Sr.不. | 方法&描述 |
---|---|
1 | ArrayList<String> divideMessage(String text) 此方法将消息文本分成几个片段,不大于最大SMS消息大小. |
2 | static SmsManager getDefault() 此方法用于获取SmsManager的默认实例 |
3 | void sendDataMessage(String destinationAddress,String scAddress,short destinationPort,byte [] data,PendingIntent sentIntent,PendingIntent deliveryIntent) 此方法用于将基于数据的SMS发送到特定的应用程序端口. |
4 | void sendMultipartTextMessage(String destinationAddress,String scAddress,ArrayList< String> parts,ArrayList< PendingIntent> sentIntents,ArrayList< PendingIntent> deliveryIntents) 发送基于多部分文本的短信. |
5 | void sendTextMessage(String destinationAddress,String scAddress,String text,PendingIntent sentIntent ,PendingIntent deliveryIntent) 发送基于短信的文字. |
以下示例显示了实际如何使用SmsManager对象向给定的手机号码发送短信.
要试验这个例子,你需要配备最新Android操作系统的实际移动设备,否则你将不得不努力使用可能无效的模拟器.
Step | 描述 |
---|---|
1 | 您将使用Android Studio IDE创建Android应用程序并将其命名为 it1352包 com.example.it1352. |
2 | 修改 src/Mai nActivity.java 文件并添加所需的代码来处理发送短信. |
3 | 修改布局XML文件 res/layout/activity_main.xml 如果需要,添加任何GUI组件.我正在添加一个简单的GUI来发送手机号码和SMS文本以及一个发送短信的简单按钮. |
4 | 无需在res/values/strings.xml中定义默认字符串常量. Android工作室负责默认常量. |
5 | 修改 AndroidManifest.xml 如下所示 |
6 | 运行应用程序启动Android模拟器并验证应用程序中所做更改的结果. |
以下是修改后的主要活动文件 src/com.example.it1352/MainActivity.java.
package com.example.it1352; import android.Manifest; import android.content.pm.PackageManager; import android.os.Bundle; import android.app.Activity; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.telephony.SmsManager; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private static final int MY_PERMISSIONS_REQUEST_SEND_SMS =0 ; Button sendBtn; EditText txtphoneNo; EditText txtMessage; String phoneNo; String message; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendBtn = (Button) findViewById(R.id.btnSendSMS); txtphoneNo = (EditText) findViewById(R.id.editText); txtMessage = (EditText) findViewById(R.id.editText2); sendBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { sendSMSMessage(); } }); } protected void sendSMSMessage() { phoneNo = txtphoneNo.getText().toString(); message = txtMessage.getText().toString(); if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.SEND_SMS)) { } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, MY_PERMISSIONS_REQUEST_SEND_SMS); } } } @Override public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_SEND_SMS: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(phoneNo, null, message, null, null); Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show(); return; } } } } }
以下是 res/layout/activity_main.xml 文件的内容 :
这里abc表示it1352 logo
<?xml version="1.0" encoding="utf-8"?> <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"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sending SMS Example" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="30dp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials point " android:textColor="#ff87ff09" android:textSize="30dp" android:layout_below="@+id/textView1" android:layout_alignRight="@+id/imageButton" android:layout_alignEnd="@+id/imageButton" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton" android:src="@drawable/abc" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:hint="Enter Phone Number" android:phoneNumber="true" android:textColorHint="@color/abc_primary_text_material_dark" android:layout_below="@+id/imageButton" android:layout_centerHorizontal="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText2" android:layout_below="@+id/editText" android:layout_alignLeft="@+id/editText" android:layout_alignStart="@+id/editText" android:textColorHint="@color/abc_primary_text_material_dark" android:layout_alignRight="@+id/imageButton" android:layout_alignEnd="@+id/imageButton" android:hint="Enter SMS" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Sms" android:id="@+id/btnSendSMS" android:layout_below="@+id/editText2" android:layout_centerHorizontal="true" android:layout_marginTop="48dp" /> </RelativeLayout>
以下将是 res/values/strings.xml 的内容来定义两个新的常量 :
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">it1352</string> </resources>
以下是 AndroidManifest.xml的默认内容 :
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.it1352" > <uses-permission android:name="android.permission.SEND_SMS" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.it1352.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> </manifest>
让我们试着运行你的 it1352应用程序.我假设您已将实际的Android移动设备与计算机相关联.要从Android工作室运行应用程序,请打开项目的一个活动文件,然后单击运行 icon从工具栏中.在开始申请之前,Android studio安装程序将显示以下窗口,以选择您要运行Android应用程序的选项.
现在,您可以输入所需的手机号码和要在该号码上发送的短信.最后点击发送短信按钮发送短信.确保您的GSM/CDMA连接正常工作,以便将短信发送给收件人.
您可以用逗号分隔一些短信,然后在程序中你必须解析它们到数组字符串,最后你可以使用循环向所有给定的数字发送消息.这就是你可以编写自己的SMS客户端的方法.下一节将向您展示如何使用现有的SMS客户端发送短信.
您可以使用Android Intent通过调用Android的内置SMS功能发送短信.以下部分解释了发送SMS所需的Intent对象的不同部分.
您将使用 ACTION_VIEW 启动Android设备上安装的SMS客户端的操作.以下是使用ACTION_VIEW操作创建intent的简单语法.
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
要发送短信,您需要指定 smsto :作为URI使用setData()方法和数据类型将使用setType()方法 vnd.android-dir/mms-sms ,如下所示 :
smsIntent.setData(Uri.parse("smsto:")); smsIntent.setType("vnd.android-dir/mms-sms");
Android内置支持添加电话号码和短信到发送短信如下 :
smsIntent.putExtra("address" , new String("0123456789;3393993300")); smsIntent.putExtra("sms_body" , "Test SMS to Angilla");
这里的地址和sms_body区分大小写,应该只用小字符指定.您可以在单个字符串中指定多个数字,但用分号(;)分隔.
以下示例向您展示实际如何使用Intent对象启动SMS客户端以向给定的收件人发送短信.
要试验这个例子,您需要配备最新Android操作系统的实际移动设备,否则您将拥有与可能无效的模拟器斗争.
Step | 描述 |
---|---|
1 | 你会使用Android studio IDE创建一个Android应用程序,并将其命名为 com.example.it1352下的 it1352. |
2 | 修改 src/MainActivity.java 文件并添加所需的代码以处理发送SMS. |
3 | 修改布局XML文件 res/layout/activity_mai n.xml 根据需要添加任何GUI组件.我正在添加一个简单的按钮来启动SMS客户端. |
4 | 无需定义默认常量.Android工作室负责默认常量. |
5 | 修改 AndroidManifest.xml ,如下所示 |
6 | 运行应用程序以启动Android模拟器并验证应用程序中所做更改的结果. |
以下是修改后的主要活动文件的内容 src/com.example.it1352/MainActivity.java.
package com.example.it1352; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button startBtn = (Button) findViewById(R.id.button); startBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { sendSMS(); } }); } protected void sendSMS() { Log.i("Send SMS", ""); Intent smsIntent = new Intent(Intent.ACTION_VIEW); smsIntent.setData(Uri.parse("smsto:")); smsIntent.setType("vnd.android-dir/mms-sms"); smsIntent.putExtra("address" , new String ("01234")); smsIntent.putExtra("sms_body" , "Test "); try { startActivity(smsIntent); finish(); Log.i("Finished sending SMS...", ""); } catch (android.content.ActivityNotFoundException ex) { Toast.makeText(MainActivity.this, "SMS faild, please try again later.", Toast.LENGTH_SHORT).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
以下是 res/layout/activity_main.xml 文件的内容&减号;
这里abc表示关于it1352 logo
<?xml version="1.0" encoding="utf-8"?> <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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Drag and Drop Example" android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials Point " android:id="@+id/textView2" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:textSize="30dp" android:textColor="#ff14be3c" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/abc" android:layout_marginTop="48dp" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Compose SMS" android:id="@+id/button" android:layout_below="@+id/imageView" android:layout_alignRight="@+id/textView2" android:layout_alignEnd="@+id/textView2" android:layout_marginTop="54dp" android:layout_alignLeft="@+id/imageView" android:layout_alignStart="@+id/imageView" /> </RelativeLayout>
以下将是 res/values/strings.xml 的内容来定义两个新的常量 :
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">it1352</string> </resources>
以下是 AndroidManifest.xml的默认内容 :
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.it1352" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.it1352.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> </manifest>
让我们试着运行你的 it1352应用程序.我假设您已将实际的Android移动设备与计算机相关联.要从Android工作室运行应用程序,请打开项目的一个活动文件,然后单击运行 icon从工具栏中.在开始申请之前,Android studio将显示以下窗口,以选择您要运行Android应用程序的选项.
选择您的移动设备作为选项,然后检查您的移动设备,它将显示以下屏幕 :
现在使用撰写短信按钮启动Android内置短信客户端低于&=;
你可以修改任何一个给定的默认字段,最后使用发送短信按钮将短信发送给提到的收件人.