Android小程序之自动发送短信
2019-03-16 17:09
351 查看
Android中有一个用于发送短信的类:android.telephony.SmsManager。通过使用该类就能实现自动发送短信的功能。
小程序界面如下:
在程序中能够输入手机号和短信内容,点击发送按钮即可发送短信。具体步骤如下:
1.在AndroidManifest.xml文件中声明发送短信权限:
[code]<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
2.设计UI界面,在页面布局里加入两个EditText控件和一个Button控件:
[code] <EditText android:id="@+id/numberEdit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="请输入手机号"/> <EditText android:id="@+id/contentEdit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="请输入短信内容"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="发送短信"/>
3.构建两个广播接收器,分别用来判断短信是否成功发送以及短信是否被成功接收(当发送短信后,会接收到两个常量,分别表示发送者发送状态和接收者接收状态,并且回调到这两个广播接收器内进行处理):
[code]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()方法中进行初始化以及发送短信操作:
[code]@Override protected 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()方法内解绑广播接收器:
[code]@Override protected void onDestroy(){ unregisterReceiver(mSmsSendStatusReceiver); unregisterReceiver(mSmsDeliveryStatusReceiver); }
分析:发送短信功能主要通过调用SmsManager类的sendTextMessage()方法,该方法有5个参数:第一个是接收方手机号,第二个是短信中心号码,代码内设为null,第三个是短信内容,第四个和第五个是两个延迟意图,当接收到短信发送和接收状态的时候,分别执行延迟意图内的逻辑操作:发送两个广播,分别启动两个广播接收器,在广播接收器内对状态进行判断。
相关文章推荐
- Android调用发送短信程序
- Android基础教程之----SMS简单发送短信程序(两个模拟器之间的通信)!
- Android自动发送短信
- Android手机拨打电话、手动发送短信与自动拨打电话、自动发送短信(代码很简单哦)
- Android基础教程之----SMS简单发送短信程序(两个模拟器之间的通信)!
- android发送短信,超过70个中文,自动分拆短信内容
- Android发送短信的病毒程序
- Android自动发送短信
- Android手机自动发送短信
- Android自制发送短信程序
- Android程序崩溃异常处理之自动发送邮件
- android之接收和发送广播的开机可自动运行程序
- Android调用拨号程序,短信发送
- Android之路——第二个Android小程序(Android短信发送)
- Android自制发送短信程序
- Android 发送短信小程序
- Android短信发送程序
- android发送短信验证码并自动获取验证码填充文本框
- Android自动发送短信
- Android小程序:实现发送短信