SMS短信发送
直接看例子:
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android" Android:orientation="vertical" Android:layout_width="fill_parent" Android:layout_height="fill_parent" > <TextView Android:text="@string/inputMobile" Android:layout_width="fill_parent" Android:layout_height="wrap_content"> </TextView> <EditText Android:id="@+id/mobile" Android:layout_width="fill_parent" Android:layout_height="wrap_content"> </EditText> <TextView Android:text="@string/inputMessage" Android:layout_width="fill_parent" Android:layout_height="wrap_content"> </TextView> <EditText Android:id="@+id/Message" Android:layout_width="fill_parent" Android:layout_height="wrap_content"> </EditText> <Button Android:text="@string/Submit" Android:id="@+id/Submit" Android:layout_width="wrap_content" Android:layout_height="wrap_content"> </Button> </LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">短信发送器</string> <string name="inputMobile">请输入手机号:</string> <string name="inputMessage">请输入短信内容:</string> <string name="Submit">发送短信</string> </resources>
SmsautoActivity.java
package com.auto; import java.util.List; import android.app.Activity; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class SmsautoActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final EditText mobileNoEditText = (EditText) findViewById(R.id.mobile); final EditText messageEditText = (EditText) findViewById(R.id.Message); Button submitButton = (Button) findViewById(R.id.Submit); submitButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { String moblileString = mobileNoEditText.getText().toString(); String messageString = messageEditText.getText().toString(); SmsManager smsManager = SmsManager.getDefault(); PendingIntent sendingIntent = PendingIntent.getBroadcast(SmsautoActivity.this, 0, new Intent(), 0); List<String> msgsList = smsManager.divideMessage(messageString); for (String msg : msgsList) { smsManager.sendTextMessage(moblileString, null, msg, sendingIntent, null); } Toast.makeText(SmsautoActivity.this, "短信发送成功", Toast.LENGTH_LONG).show(); } }); } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.auto" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".SmsautoActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.SEND_SMS" /> </manifest>
PS:记得加入权限<uses-permissionandroid:name="android.permission.SEND_SMS"/>
</manifest>
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28