import android.app.Activity;import android.app.PendingIntent;import android.content.BroadcastReceiver;import android.content.ContentValues;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.net.Uri;import android.telephony.SmsManager;import android.util.Log;public class SMSLibrary {    public static void sendSMS(final Context context, final String phoneNumber, final String message) {        String SENT = "SMS_SENT";        String DELIVERED = "SMS_DELIVERED";        final PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent(SENT), 0);        final PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, new Intent(DELIVERED), 0);        //---when the SMS has been sent---        context.registerReceiver(new BroadcastReceiver() {            @Override            public void onReceive(Context arg0, Intent arg1) {                switch (getResultCode()) {                    case Activity.RESULT_OK:                        Log.w("LOG", "SMS sent");                        break;                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:                        Log.w("LOG", "Generic failure");                        break;                    case SmsManager.RESULT_ERROR_NO_SERVICE:                        Log.w("LOG", "No service");                        break;                    case SmsManager.RESULT_ERROR_NULL_PDU:                        Log.w("LOG", "Null PDU");                        break;                    case SmsManager.RESULT_ERROR_RADIO_OFF:                        Log.w("LOG", "Radio off");                        break;                }            }        }, new IntentFilter(SENT));        //---when the SMS has been delivered---        context.registerReceiver(new BroadcastReceiver() {            @Override            public void onReceive(Context arg0, Intent arg1) {                switch (getResultCode()) {                    case Activity.RESULT_OK:                        Log.w("LOG", "SMS delivered");                        break;                    case Activity.RESULT_CANCELED:                        Log.w("LOG", "SMS not delivered");                        break;                }            }        }, new IntentFilter(DELIVERED));        SmsManager sms = SmsManager.getDefault();        try {            sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);        /* To save in Sent items */            ContentValues values = new ContentValues();            values.put("address", phoneNumber);            values.put("body", message);            context.getContentResolver().insert(Uri.parse("content://sms/sent"), values);        } catch (IllegalArgumentException e) {            // TODO: handle exception            Log.d("LOG", "" + e.getMessage());        }    }}



更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. android字符串 优化(一)
  2. Android动态关闭mainfest中的receiver
  3. android 图片处理
  4. DPCM.txt
  5. Android中Message参数传递
  6. Android(安卓)后台线程调用前台线程的几
  7. Android——实现在纯Java代码里写布局
  8. android cts and junit
  9. Android(安卓)kernel printk打印级别
  10. Android(安卓)Studio - Session 'app': E