欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

手机发送短信,,package my.y

来源: javaer 分享于  点击 24712 次 点评:31

手机发送短信,,package my.y


package my.yaner.android.component;import android.app.Activity;import android.app.PendingIntent;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.net.Uri;import android.os.Bundle;import android.telephony.SmsManager;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup.LayoutParams;import android.widget.Button;import android.widget.EditText;import android.widget.LinearLayout;import android.widget.Toast;public class SendSmsActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //设置布局        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);        LinearLayout linearlayout = new LinearLayout(this);        linearlayout.setLayoutParams(layoutParams);        linearlayout.setOrientation(LinearLayout.VERTICAL);        setContentView(linearlayout);        //添加组件        final EditText tvPhoneNumber = new EditText(this);        tvPhoneNumber.setHint("手机号码");        linearlayout.addView(tvPhoneNumber);        final EditText tvContent = new EditText(this);        tvContent.setHint("短信内容");        linearlayout.addView(tvContent);        Button btn_send = new Button(this);        btn_send.setText("发送");        btn_send.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                String phoneNumber = tvPhoneNumber.getText().toString();                String phoneContent = tvContent.getText().toString();                if(phoneNumber != null && phoneContent != null) {                    sendSMS(phoneNumber, phoneContent);                } else {                    Toast.makeText(SendSmsActivity.this, "请填写要发送的手机号码和短信内容", Toast.LENGTH_SHORT).show();                }            }        });        linearlayout.addView(btn_send);        //添加广播        registerReceiver(send, new IntentFilter("my.sms.send"));        registerReceiver(delivery, new IntentFilter("my.sms.delivery"));    }    private void sendSMS(String phoneNumber, String phoneContent) {        //方法一//      Uri smsToUri = Uri.parse("smsto:" + phoneNumber);//      Intent mIntent = new Intent(android.content.Intent.ACTION_SENDTO, smsToUri);//      mIntent.putExtra("sms_body", phoneContent);//      startActivity(mIntent);        //方法二        SmsManager smsMgr = SmsManager.getDefault();        Intent sent = new Intent();        sent.setAction("my.sms.send");        PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0, sent, 0);        Intent delivery = new Intent();        delivery.setAction("my.sms.delivery");        PendingIntent deliveryIntent = PendingIntent.getBroadcast(this, 0, delivery, 0);        smsMgr.sendTextMessage(phoneNumber, null, phoneContent, sentIntent, deliveryIntent);    }    BroadcastReceiver send = new BroadcastReceiver() {               @Override             public void onReceive(Context context, Intent intent) {                   if("my.sms.send".equals(intent.getAction()))                       Toast.makeText(SendSmsActivity.this, "SMS send  success",                                Toast.LENGTH_LONG).show();               }       };        BroadcastReceiver delivery = new BroadcastReceiver() {               @Override             public void onReceive(Context context, Intent intent) {                   if("my.sms.delivery".equals(intent.getAction()))                       Toast.makeText(SendSmsActivity.this, "SMS delivery success",                            Toast.LENGTH_LONG).show();               }        };     @Override     protected void onDestroy() {        super.onDestroy();        unregisterReceiver(delivery);        unregisterReceiver(send);     }}//该片段来自于http://byrx.net
相关栏目:

用户点评