Example usage for android.telephony SmsManager sendTextMessage

List of usage examples for android.telephony SmsManager sendTextMessage

Introduction

In this page you can find the example usage for android.telephony SmsManager sendTextMessage.

Prototype

public void sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent,
        PendingIntent deliveryIntent) 

Source Link

Document

Send a text based SMS.

Usage

From source file:com.godowondev.MyGcmListenerService.java

private void sendSMS(String phoneNumber, String message) {
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, null, null);
}

From source file:mx.com.neus.juanitovision.GeofenceTransitionsIntentService.java

public void sendSMS(String phoneNo, String msg) {
    try {//www .  j  ava2 s. co  m
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNo, null, msg, null, null);
        Toast.makeText(getApplicationContext(), "Message Sent", Toast.LENGTH_LONG).show();
    } catch (Exception ex) {
        Toast.makeText(getApplicationContext(), ex.getMessage().toString(), Toast.LENGTH_LONG).show();
        ex.printStackTrace();
    }
}

From source file:com.example.hackinghealthclinification.app.ClinificationApp.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_clinification_app);

    initializeLaunchButtons();/*from  ww w .j  ava 2s .  co  m*/
    mIsRegistered = false; // check if BroadcastReceiver registered
    IntentFilter filterSmsReceived = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");

    final SMSConfirmDialog.ConfirmDialogListener mListener = new SMSConfirmDialog.ConfirmDialogListener() {
        @Override
        public void onDialogPositiveClick(SMSConfirmDialog c) {
            //                mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("yes"
            //                        + mPhoneNumber)));
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(mServerNumber, null, "yes", null, null);
        }

        @Override
        public void onDialogNegativeClick(SMSConfirmDialog c) {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(mServerNumber, null, "no", null, null);
        }
    };

    mReceiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {
            mContext = context;
            Bundle pudsBundle = intent.getExtras();
            Object[] pdus = (Object[]) pudsBundle.get("pdus");
            SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdus[0]);
            Log.i("Error with SMS ::: ", messages.getMessageBody());
            if (messages.getMessageBody().contains(SMS_TRIGGER_KEY)) {
                mPhoneNumber = messages.getOriginatingAddress();
                SMSConfirmDialog dialog = new SMSConfirmDialog(mListener,
                        "Will you accept the new appointment: " + "\n" + messages.getMessageBody(), "Yes",
                        "No");
                dialog.show(getSupportFragmentManager(), null);
            }
        }
    };

    this.registerReceiver(mReceiver, filterSmsReceived);
    mIsRegistered = true;

}

From source file:com.packpublishing.asynchronousandroid.chapter6.SMSDispatcherIntentService.java

@Override
protected void onHandleIntent(Intent intent) {
    try {//from w ww  .  j a  v  a2s .com
        String to = intent.getStringExtra(TO_KEY);
        String text = intent.getStringExtra(TEXT_KEY);
        Log.i("SMS Dispatcher", "Delivering message to " + text);
        SmsManager sms = SmsManager.getDefault();
        Intent deliveredIntent = new Intent("sms_delivered");
        deliveredIntent.putExtra(SMSDispatcher.TO_KEY, to);
        deliveredIntent.putExtra(SMSDispatcher.TEXT_KEY, text);
        sms.sendTextMessage(to, null, text, null, null);
    } finally {
        WakefulBroadcastReceiver.completeWakefulIntent(intent);
    }
}

From source file:dk.spaceblog.thenicobomb.MainActivity.java

/** Called when the user clicks the Send button */
public void sendSMSMessage() {
    //Intent sentIntent = new Intent("SMS_SENT");
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(this.phoneNumber, null, this.textMessage, null, null);
}

From source file:com.packpublishing.asynchronousandroid.chapter6.SMSDispatcherAsync.java

void processDispatch(Context context, Intent intent) {
    String to = intent.getStringExtra(TO_KEY);
    String text = intent.getStringExtra(TEXT_KEY);
    Log.i("SMS Dispatcher", "Delivering message to " + text);
    SmsManager sms = SmsManager.getDefault();
    Intent deliveredIntent = new Intent("sms_delivered");
    deliveredIntent.putExtra(SMSDispatcher.TO_KEY, to);
    deliveredIntent.putExtra(SMSDispatcher.TEXT_KEY, text);
    sms.sendTextMessage(to, null, text,
            PendingIntent.getBroadcast(context, DISPATCH_ACTION.hashCode(), deliveredIntent, 0), null);
}

From source file:com.mattprecious.locnotifier.LocationService.java

private void approachingDestination() {
    Log.d(getClass().getSimpleName(), "Within distance to destination");

    locationManager.removeUpdates(locationListener);

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

    String notifTitle = getString(R.string.notification_alert_title);
    String notifText = getString(R.string.notification_alert_text);

    runningNotification = null;/*from ww w . j av a  2  s .c om*/

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setSmallIcon(R.drawable.notification_alert).setContentTitle(notifTitle)
            .setContentText(notifText).setContentIntent(contentIntent).setAutoCancel(true);

    String tone = preferences.getString("tone", null);
    Uri toneUri = (tone == null) ? RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
            : Uri.parse(tone);

    notificationBuilder.setSound(toneUri);

    if (preferences.getBoolean("vibrate", false)) {
        int shortVib = 150;
        int shortPause = 150;

        // vibrate 3 short times
        long[] pattern = { 0, shortVib, shortPause, shortVib, shortPause, shortVib, };

        notificationBuilder.setVibrate(pattern);
    }

    notificationBuilder.setLights(0xffff0000, 500, 500);

    Notification notification = notificationBuilder.getNotification();
    if (preferences.getBoolean("insistent", false)) {
        notification.flags |= Notification.FLAG_INSISTENT;
    }

    notificationManager.notify(0, notification);

    // send sms
    if (preferences.getBoolean("sms_enabled", false)) {
        String number = preferences.getString("sms_contact", null);
        String message = preferences.getString("sms_message", null);

        if (number != null && message != null) {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(number, null, message, null, null);
        }
    }

    stopSelf();
}

From source file:MainActivity.java

public void send(View view) {
    String phoneNumber = ((EditText) findViewById(R.id.editTextNumber)).getText().toString();
    String msg = ((EditText) findViewById(R.id.editTextMsg)).getText().toString();

    if (phoneNumber == null || phoneNumber.length() == 0 || msg == null || msg.length() == 0) {
        return;// ww w . jav  a 2  s.c  o  m
    }

    if (checkPermission(Manifest.permission.SEND_SMS)) {
        SmsManager smsManager = SmsManager.getDefault();
        if (msg.length() > 160) {
            ArrayList<String> messages = smsManager.divideMessage(msg);
            smsManager.sendMultipartTextMessage(phoneNumber, null, messages, null, null);
        } else {
            smsManager.sendTextMessage(phoneNumber, null, msg, null, null);
        }
    } else {
        Toast.makeText(MainActivity.this, "No Permission", Toast.LENGTH_SHORT).show();
    }
}

From source file:com.packpublishing.asynchronousandroid.chapter6.SMSDispatcher.java

void processDispatch(Context context, Intent intent) {
    String to = intent.getStringExtra(TO_KEY);
    String text = intent.getStringExtra(TEXT_KEY);
    Log.i("SMS Dispatcher", "Delivering message to " + text);
    SmsManager sms = SmsManager.getDefault();
    Intent deliveredIntent = new Intent("sms_delivered");
    deliveredIntent.putExtra(SMSDispatcher.TO_KEY, to);
    deliveredIntent.putExtra(SMSDispatcher.TEXT_KEY, text);

    PendingIntent pi = PendingIntent.getBroadcast(context, DISPATCH_ACTION.hashCode(), deliveredIntent, 0);

    sms.sendTextMessage(to, null, text, pi, null);
}

From source file:com.hari.autotasx.GeofenceTransitionsIntentService.java

@Override
protected void onHandleIntent(Intent intent) {

    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        String errorMessage = GeofenceErrorMessages.getErrorString(this, geofencingEvent.getErrorCode());
        // Log.e(TAG, errorMessage);
        return;//from   w  w  w .j  a  v a  2 s  .c  o m
    }

    int geofenceTransition = geofencingEvent.getGeofenceTransition();
    int smsDb = 0;
    int wifiDb = 0;
    int silDb = 0;
    int RemDb = 0;
    int blueDb = 0;
    int carDb = 0;
    String remMsgDb = null;
    int RemDb_carPark = 0;
    String remMsgDb_carPark = null;
    //Creating database object to fetch values
    ManageDB autodb = new ManageDB(this);
    autodb.open();

    if (!CarParkService.car_flag_static) {
        Cursor cursor = autodb.getEntry(ActionFragment.geoFence);
        cursor.moveToFirst();

        //System.out.println("cursor count"+cursor.getCount());

        //Log.i("logcat",cursor.getString(0)+", "+cursor.getString(1)+" , "+cursor.getString(2));
        smsDb = cursor.getInt(5);
        wifiDb = cursor.getInt(6);
        silDb = cursor.getInt(7);
        RemDb = cursor.getInt(8);
        remMsgDb = cursor.getString(9);
        blueDb = cursor.getInt(10);
        carDb = cursor.getInt(11);

        cursor.close();

    } else {
        Cursor cursor_carpark = autodb.getEntryCarPark(CarParkService.geofence_carpark);
        cursor_carpark.moveToFirst();

        carDb = cursor_carpark.getInt(11);
        remMsgDb_carPark = cursor_carpark.getString(9);

        cursor_carpark.close();
    }
    autodb.close();

    //Checking Enter/Exit transitions
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
        List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
        String geofenceTransitionDetails = getGeofenceTransitionDetails(this, geofenceTransition,
                triggeringGeofences);
        System.out.println("smsdb" + smsDb);
        if (RemDb == 1) {
            sendNotification(geofenceTransitionDetails, remMsgDb);
        }
        if (carDb == 1) {
            sendNotification(geofenceTransitionDetails, remMsgDb_carPark);
        }
        //Perform actions if set on Actions set
        if (silDb == 1) {
            Toast.makeText(this, "silent", Toast.LENGTH_SHORT).show();
            AudioManager am = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
            am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

        }

        if (wifiDb == 1) {
            WifiManager wifiManager = (WifiManager) getBaseContext().getSystemService(Context.WIFI_SERVICE);
            wifiManager.setWifiEnabled(true);
        }

        if (blueDb == 1) {
            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (!mBluetoothAdapter.isEnabled()) {
                mBluetoothAdapter.enable();
            }
        }
        if (smsDb == 1) {
            Toast.makeText(this, "inside sendsms()", Toast.LENGTH_SHORT).show();
            SmsManager sm = SmsManager.getDefault();
            System.out.printf("sm", sm);
            sm.sendTextMessage(ActionFragment.smsNo, null,
                    "Hi, I am in " + ActionFragment.geoFence.getNameLoc() + " now!!!!", null, null);
        }
        if (carDb == 1) {
            Intent mapIntent = new Intent(this, CarParkMap.class);
            mapIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

    } else if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
        List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
        String geofenceTransitionDetails = getGeofenceTransitionDetails(this, geofenceTransition,
                triggeringGeofences);
        if (silDb == 1) {
            Toast.makeText(this, "general", Toast.LENGTH_SHORT).show();
            AudioManager am1 = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
            am1.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        }

        if (wifiDb == 1) {
            WifiManager wifiManager1 = (WifiManager) getBaseContext().getSystemService(Context.WIFI_SERVICE);
            wifiManager1.setWifiEnabled(false);
        }
        if (blueDb == 1) {
            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (mBluetoothAdapter.isEnabled()) {
                mBluetoothAdapter.disable();
            }
        }
        if (smsDb == 1) {
            Toast.makeText(this, "inside sendsms()", Toast.LENGTH_SHORT).show();
            SmsManager sm = SmsManager.getDefault();
            System.out.printf("smsNo :" + ActionFragment.smsNo);
            //fetch
            sm.sendTextMessage(ActionFragment.smsNo, null,
                    "Hi, I am outside " + ActionFragment.geoFence.getNameLoc() + " now !!!", null, null);
        }
        if (RemDb == 1) {
            sendNotification(geofenceTransitionDetails, remMsgDb);
        }

    }

}