200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > Android小程序之自动发送短信

Android小程序之自动发送短信

时间:2020-03-05 23:21:54

相关推荐

Android小程序之自动发送短信

Android中有一个用于发送短信的类:android.telephony.SmsManager。通过使用该类就能实现自动发送短信的功能。

小程序界面如下:

在程序中能够输入手机号和短信内容,点击发送按钮即可发送短信。具体步骤如下:

1.在AndroidManifest.xml文件中声明发送短信权限:

<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

2.设计UI界面,在页面布局里加入两个EditText控件和一个Button控件:

<EditTextandroid:id="@+id/numberEdit"android:layout_width="wrap_content"android:layout_height="wrap_content"android:hint="请输入手机号"/><EditTextandroid:id="@+id/contentEdit"android:layout_width="wrap_content"android:layout_height="wrap_content"android:hint="请输入短信内容"/><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:hint="发送短信"/>

3.构建两个广播接收器,分别用来判断短信是否成功发送以及短信是否被成功接收(当发送短信后,会接收到两个常量,分别表示发送者发送状态和接收者接收状态,并且回调到这两个广播接收器内进行处理):

public class SmsSendStatusReceiver extends BroadcastReceiver {//用于判断短信是否成功发送@Override public void onReceive(Context context, Intent intent) {Log.d(TAG, "SmsStatusReceiver onReceive.");switch (getResultCode()) {case Activity.RESULT_OK:Log.d("SendStatus", "Activity.RESULT_OK");break;case SmsManager.RESULT_ERROR_GENERIC_FAILURE:Log.d("SendStatus", "RESULT_ERROR_GENERIC_FAILURE");break;case SmsManager.RESULT_ERROR_NO_SERVICE:Log.d("SendStatus", "RESULT_ERROR_NO_SERVICE");break;case SmsManager.RESULT_ERROR_NULL_PDU:Log.d("SendStatus", "RESULT_ERROR_NULL_PDU");break;case SmsManager.RESULT_ERROR_RADIO_OFF:Log.d("SendStatus", "RESULT_ERROR_RADIO_OFF");break;}}}public class SmsDeliveryStatusReceiver extends BroadcastReceiver {//用于判断短信是否成功被接收private static final String TAG = "SmsDeliveryStatusReceiver";@Override public void onReceive(Context context, Intent intent) {Log.d(TAG, "SmsDeliveryStatusReceiver onReceive.");switch (getResultCode()) {case Activity.RESULT_OK:Log.i(TAG, "RESULT_OK");break;case Activity.RESULT_CANCELED:Log.i(TAG, "RESULT_CANCELED");break;}}}

4.在onCreate()方法中进行初始化以及发送短信操作:

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);text1 = (EditText) findViewById(R.id.EditText01);//获取控件实例text2 = (EditText) findViewById(R.id.EditText02);send = (Button) findViewById(R.id.Button01);sendIntentFilter = new IntentFilter();//注册两个广播接收器sendIntentFilter.addAction("SMS_SEND");registerReceiver(mSmsSendStatusReceiver,sendIntentFilter);deliveryIntentFilter = new IntentFilter();deliveryIntentFilter.addAction("SMS_DELIVERYED");registerReceiver(mSmsDeliveryStatusReceiver,deliveryIntentFilter);send.setOnClickListener(new View.OnClickListener() {public void onClick(View arg0) {String phoneNo = text1.getText().toString();//从两个文本控件中获取手机号和短信内容String message = text2.getText().toString();if (phoneNo.length()>0 && message.length()>0){//构造两个延迟意图,当收到短信发送和接收状态时,回调到两个广播接收器内进行下一步操作。PendingIntent sendIntent = PendingIntent.getBroadcast(MainActivity.this,1,new Intent("SMS_SEND"),0);PendingIntent deliveryIntent = PendingIntent.getBroadcast(MainActivity.this,1,new Intent("SMS_DELIVERYED"),0);SmsManager manager = SmsManager.getDefault();//调用sendTextMessage方法发送短信,参数是手机号,短信内容,以及两个延迟意图。manager.sendTextMessage(phoneNo, null, message, sendIntent, deliveryIntent);}else {Toast.makeText(MainActivity.this, "请重新输入电话号码和短信内容", Toast.LENGTH_LONG).show();}}});}

5.在onDestroy()方法内解绑广播接收器:

@Overrideprotected void onDestroy(){unregisterReceiver(mSmsSendStatusReceiver);unregisterReceiver(mSmsDeliveryStatusReceiver);}

分析:发送短信功能主要通过调用SmsManager类的sendTextMessage()方法,该方法有5个参数:第一个是接收方手机号,第二个是短信中心号码,代码内设为null,第三个是短信内容,第四个和第五个是两个延迟意图,当接收到短信发送和接收状态的时候,分别执行延迟意图内的逻辑操作:发送两个广播,分别启动两个广播接收器,在广播接收器内对状态进行判断。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。