Android简单的发短信案例

来源:互联网 发布:动态图截取软件 编辑:程序博客网 时间:2024/04/30 22:02
package cn.csdn.hr.activity;

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.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class SmsActivity extends Activity {


private Button sendBtn;
private EditText etPhone;
private EditText etSms;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置显示的视图
setContentView(R.layout.main);


// 初始化组件对象
sendBtn = (Button) findViewById(R.id.sendBtn);
etPhone = (EditText) findViewById(R.id.etphone);
etSms = (EditText) findViewById(R.id.etsms);


// 注册按钮的点击事件
sendBtn.setOnClickListener(new View.OnClickListener() {


public void onClick(View v) {
// 获取电话号码
Editable phone = etPhone.getText();
// 获取短信内容
Editable sms = etSms.getText();


// 发送短信
// 要使用短信的管理器
SmsManager smsManager = SmsManager.getDefault();
// 声明一个意图
PendingIntent pendingIntent = PendingIntent.getBroadcast(
SmsActivity.this, 0, new Intent(), 0);


Log.v("TAG",sms.toString().length()+"");

if (sms.toString().length() > 70 ) {
Log.v("TAG", sms.toString());
//拆分的时候  并没有拆分  smsManager 本身 默认的大小 多少个字符 
List<String> content = smsManager.divideMessage(sms
.toString());

for (String str : content) {
Log.v("TAG", str);
smsManager.sendTextMessage(phone.toString(), null, str,
pendingIntent, null);
}
} else {
// 管理器对象 进行发送短信
smsManager.sendTextMessage(phone.toString(), null,
sms.toString(), pendingIntent, null);
// 要干事情 需要这个干这个事情的权限
}

Toast.makeText(SmsActivity.this, "短信成功", Toast.LENGTH_LONG).show();
}

});


}

}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/etcallphone" />


    <EditText
        android:id="@+id/etphone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="phone" >


        <requestFocus />
    </EditText>


    <CheckedTextView
        android:id="@+id/checkedTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/etSms" />


    <EditText
        android:id="@+id/etsms"
        android:layout_width="match_parent"
        android:layout_height="82dp"
        android:layout_weight="0.19"
        android:ems="10"
        android:inputType="textMultiLine" />


    <Button
        android:id="@+id/sendBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sendBtn" />


</LinearLayout>




<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="hello">Hello World, SmsActivity!</string>
    <string name="app_name">Sms</string>
    <string name="etcallphone">请输入的电话号码</string>
    <string name="etSms">请输入短信内容</string>
    <string name="sendBtn">发送</string>


</resources>

原创粉丝点击