SMSServices.java :  » SMS » smsshow » com » cpan » android » sms » util » Android Open Source

Android Open Source » SMS » smsshow 
smsshow » com » cpan » android » sms » util » SMSServices.java
package com.cpan.android.sms.util;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.provider.Telephony.Sms;
import android.telephony.gsm.SmsManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

public class SMSServices {
  private final static String TAG = "SMSServices";
  private final static boolean debug = true;
  private static SMSServices service;
  private Context mContext;

  private SMSServices(Context ctx) {
    mContext = ctx;
  }

  public static SMSServices getInstance(Context ctx) {
    if (null == service) {
      service = new SMSServices(ctx);
    }
    return service;
  }

  /**
   * 
   * 
   * @param smsId
   */
  public void markSmsAsReadBySmsId(String smsId) {
    if (TextUtils.isEmpty(smsId)) {
      return;
    }
    Uri smsInboxUri = ContentUris.withAppendedId(Sms.CONTENT_URI, Long
        .parseLong(smsId));
    ContentValues values = new ContentValues(1);
    values.put("read", 1);
    String where = "read = 0";
    mContext.getContentResolver().update(smsInboxUri, values, where, null);
  }

  /**
   * 
   * 
   * @param phoneNo
   * @param messages
   */
  public void sendSMS(String phoneNo, String messages) {
    if (debug)
      Log.e(TAG, "sendSMS");
    int result = 999;
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0,
        new Intent(SENT), 0);

    PendingIntent deliveredPI = PendingIntent.getBroadcast(mContext, 0,
        new Intent(DELIVERED), 0);

    mContext.registerReceiver(new BroadcastReceiver() {
      public void onReceive(Context context, Intent intent) {
        switch (getResultCode()) {
        case Activity.RESULT_OK:
          Toast.makeText(mContext, "SMS sent", Toast.LENGTH_SHORT)
              .show();
          break;
        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
          Toast.makeText(mContext, "Generic failure",
              Toast.LENGTH_SHORT).show();
          break;
        case SmsManager.RESULT_ERROR_NO_SERVICE:
          Toast.makeText(mContext, "No service", Toast.LENGTH_SHORT)
              .show();
          break;
        case SmsManager.RESULT_ERROR_NULL_PDU:
          Toast.makeText(mContext, "Null PDU", Toast.LENGTH_SHORT)
              .show();
          break;
        case SmsManager.RESULT_ERROR_RADIO_OFF:
          Toast.makeText(mContext, "Radio off", Toast.LENGTH_SHORT)
              .show();
          break;
        }
      }
    }, new IntentFilter(SENT));

    mContext.registerReceiver(new BroadcastReceiver() {
      public void onReceive(Context context, Intent intent) {
        switch (getResultCode()) {
        case Activity.RESULT_OK:
          Toast.makeText(mContext, "SMS delivered",
              Toast.LENGTH_SHORT).show();
          break;
        case Activity.RESULT_CANCELED:
          Toast.makeText(mContext, "SMS not delivered",
              Toast.LENGTH_SHORT).show();
          break;
        }
      }
    }, new IntentFilter(DELIVERED));

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNo, null, messages, sentPI, deliveredPI);
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.