安卓发短信源码

来源:互联网 发布:益阳市网络招聘 编辑:程序博客网 时间:2024/04/28 02:53
<?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="tv_phone"/>


<!-- 电话号码输入 -->
<EditText
    android:id="@+id/et_phone"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>


<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="tv_content"/>


<!-- 短信内容编辑 -->
<EditText 
    android:id="@+id/et_content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minLines="3"
    android:gravity="left"/>
    <!-- 可3行显示 -->
    <!-- 设置左边输入 -->


<Button 
    android:id="@+id/bt_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="send"/>


</LinearLayout>










package com.example.sqlist;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {


    private EditText et_phone, et_content;
    private Button bt;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_content = (EditText)findViewById(R.id.et_content);
        bt = (Button)findViewById(R.id.bt_send);
        
        bt.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                String mobile = et_phone.getText().toString();
                String content = et_content.getText().toString();
                
                SmsManager smsManager = SmsManager.getDefault();
                PendingIntent sentIntent = PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(), 0);
                
                if(content.length() >= 70)
                {
                    //短信字数大于70,自动分条
                    List<String> ms = smsManager.divideMessage(content);
                    
                    for(String str : ms )
                    {
                        //短信发送
                        smsManager.sendTextMessage(mobile, null, str, sentIntent, null);
                    }
                }
                else
                {
                    smsManager.sendTextMessage(mobile, null, content, sentIntent, null);
                }
                
                Toast.makeText(MainActivity.this, "发送成功!", Toast.LENGTH_LONG).show();
            }
        });       
    }
}








 <uses-permission android:name="android.permission.SEND_SMS"/>
0 0
原创粉丝点击