要使用手机短信服务,在AndroidManifest.xml中必须添加短信服务权限

AndroidManifest.xml

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.me.sendsms">
<application>
<activity android:name=".MainActivity" android:label="MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3"/>
<uses-permission android:name="android.permission.SEND_SMS"/><!--添加权限-->
</manifest>


MainActivity.java



package org.me.sendsms;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.List;

public class MainActivity extends Activity {

private EditText txtNo;
private EditText txtContent;
private Button btnSend;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
txtNo = (EditText) findViewById(R.id.txtNo);
txtContent = (EditText) findViewById(R.id.txtContent);
btnSend = (Button) findViewById(R.id.btnSend);

btnSend.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
String strNo = txtNo.getText().toString();
String strContent = txtContent.getText().toString();
SmsManager smsManager = SmsManager.getDefault();
PendingIntent sentIntent = PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(), 0);
//如果字数超过70,需拆分成多条短信发送 http://www.my400800.cn
if (strContent.length() > 70) {
List<String> msgs = smsManager.divideMessage(strContent);
for (String msg : msgs) {
smsManager.sendTextMessage(strNo, null, msg, sentIntent, null);
}
} else {
smsManager.sendTextMessage(strNo, null, strContent, sentIntent, null);
}
Toast.makeText(MainActivity.this, "短信发送完成", Toast.LENGTH_LONG).show();
}
});
}
}

更多相关文章

  1. android EditText 添加图片表情以及在四个方向上绘制图片
  2. Android(安卓)config.gradle
  3. Android当方法总数超过64K时(Android(安卓)Studio)
  4. android权限管理
  5. Android开机自启
  6. Android(安卓)View转换成图片保存
  7. Android(安卓)方法数超过64k限制的解决办法
  8. Android中实现Launcher功能之一 ----- 添加快捷方式
  9. Android(安卓)studio SDK6.0 不支持HttpClient解决办法

随机推荐

  1. C#中关于逆变和协变的详解
  2. MemoryCache问题修复的解决方法
  3. ASP.NET Core Razor页面路由的详细介绍
  4. C#多线程之Semaphore的使用详解
  5. 详解有关asp.net mvc 动态编译生成Contro
  6. C#中Observer观察者模式如何解决牛顿童鞋
  7. C#如何实现JSON与对象之间互相转换功能示
  8. 有关ASP.NET Core 中的多语言支持的详细
  9. C#如何实现loading提示控件简单的实例
  10. C#中Observer观察者模式解决牛顿童鞋成绩