android 短信发送器(SmsManager)

来源:互联网 发布:白金数据结局是什么 编辑:程序博客网 时间:2024/06/05 11:47

android的短信发送器主要是采用SmsManager来实现的。

package com.example.activity;import java.util.ArrayList;import android.app.Activity;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 SendSms extends Activity {private EditText numberText;private EditText contentText;private Button send;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.sms);numberText=(EditText)findViewById(R.id.number);contentText=(EditText)findViewById(R.id.content);send=(Button)findViewById(R.id.send);send.setOnClickListener(new ButtonClickListener());}public final class ButtonClickListener implements OnClickListener{@Overridepublic void onClick(View v) {String number=numberText.getText().toString();String content=contentText.getText().toString();SmsManager manager=SmsManager.getDefault();ArrayList<String> texts=manager.divideMessage(content);for(String text:texts){manager.sendTextMessage(number, null, text, null, null);}Toast.makeText(SendSms.this, "发送成功", Toast.LENGTH_SHORT).show();}}}


 

 相对于的界面:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="发送号码"    android:textSize="18dip"    /><EditText     android:id="@+id/number"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    /><TextView     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="发送内容"    android:textSize="18dip"    /><EditText     android:id="@+id/content"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:minLines="3"    /><Button     android:id="@+id/send"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="发送"    /></LinearLayout>