Android Open Source - android-sms-gateway Sms Intent Service






From Project

Back to project page android-sms-gateway.

License

The source code is released under:

Apache License

If you think the Android project android-sms-gateway listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.anjlab.android.smsgateway.gcm;
//ww w  . ja va2 s. c  o  m
import com.bugsense.trace.BugSenseHandler;
import com.google.android.gms.gcm.GoogleCloudMessaging;

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.telephony.SmsManager;
import android.util.Log;


public class SmsIntentService extends IntentService {
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;

    public SmsIntentService() {
        super("GcmIntentService");
    }
    public static final String TAG = "SMS Gateway";

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {
            if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
              String number = extras.getString("number");
              String message = extras.getString("message");
              if (number != null && number.length() > 0 && message != null && message.length() > 0) {
                try {
                  SmsManager smsManager = SmsManager.getDefault();
                  smsManager.sendTextMessage(number, null, message, null, null);

                  String result = number + ": " + message;
                  Log.i(TAG, result);

                  sendNotification(result);

                  ContentValues values = new ContentValues();
                  values.put("address", number);
                  values.put("body", message); 
                  getApplicationContext().getContentResolver().insert(Uri.parse("content://sms/sent"), values);
                }
                catch (Exception ex) {
                  Log.e(TAG, ex.toString());
                  BugSenseHandler.initAndStartSession(this, "f502e669");
                  BugSenseHandler.sendException(ex);
                  BugSenseHandler.flush(this);
                  BugSenseHandler.closeSession(this);
                }
              }
            }
        }
        SmsBroadcastReceiver.completeWakefulIntent(intent);
    }

    private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_stat_gcm)
        .setContentTitle(getText(R.string.app_name))
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg).setAutoCancel(true);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}




Java Source Code List

com.anjlab.android.smsgateway.gcm.GatewayApplication.java
com.anjlab.android.smsgateway.gcm.MainActivity.java
com.anjlab.android.smsgateway.gcm.SmsBroadcastReceiver.java
com.anjlab.android.smsgateway.gcm.SmsIntentService.java