欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android-发短信代码实现

程序员文章站 2022-05-16 23:34:47
...
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 Android_sendMessageActivity extends Activity {

    private EditText et_phone, et_content;
    private Button bt;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_content = (EditText)findViewById(R.id.et_content);
        bt = (Button)findViewById(R.id.button);
        
        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(Android_sendMessageActivity.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(Android_sendMessageActivity.this, "发送成功!", Toast.LENGTH_LONG).show();
            }
        });       
    }
}

 

相关标签: 短信