Example usage for android.telephony SmsManager getDefault

List of usage examples for android.telephony SmsManager getDefault

Introduction

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

Prototype

public static SmsManager getDefault() 

Source Link

Document

Get the SmsManager associated with the default subscription id.

Usage

From source file:com.demo.android.smsapp.activities.SMSChatActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);//from   w  w  w.  ja v a  2 s. co m

    number = getIntent().getStringExtra("number");
    smsModels = new ArrayList<>();
    smsModelsSearch = new ArrayList<>();

    actionBar = getSupportActionBar();
    actionBar.setTitle(number);

    smsManager = SmsManager.getDefault();

    etMsg = (EditText) findViewById(R.id.et_msg);
    btnSend = (Button) findViewById(R.id.btn_send);
    btnSend.setOnClickListener(this);
    recyclerView = (RecyclerView) findViewById(R.id.recycler_chats);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    getChatWithNumber();

    smsChatsAdapter = new SMSChatsAdapter(this, smsModels);
    recyclerView.setAdapter(smsChatsAdapter);
}

From source file:capstone.se491_phm.Alarm.java

public static void sendSMS(Context context) {
    String phoneNumber = PreferenceManager.getDefaultSharedPreferences(context)
            .getString(Constants.EMERGENCY_CONTACT, "");
    if (!"".equals(phoneNumber)) {
        String message = "Help I have fallen! Last know location: ";

        LocationMgr locMgr = new LocationMgr(context);
        message += locMgr.getLocationUrl();

        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, null, null);

        broadcast(alertBroadcastIntent, "SMS Sent");
    } else {//from  ww w  . j av a  2 s.  co  m
        broadcast(alertBroadcastIntent, "Unable to send SMS. Missing number.");
    }
}

From source file:me.myatminsoe.myansms.SMSdroid.java

@Override
public void onCreate() {
    try {//  w ww.j av  a  2  s  . c o m
        Class.forName("android.os.AsyncTask");
    } catch (Throwable ignore) {
    }

    super.onCreate();
    Log.i(TAG, "init SMSdroid v" + BuildConfig.VERSION_NAME + " (" + BuildConfig.VERSION_CODE + ")");

    // check for default app only when READ_SMS was granted
    // this may need a second launch on Android 6.0 though
    if (hasPermission(this, Manifest.permission.READ_SMS)) {
        final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
        int state = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
        if (p.getBoolean(PreferencesActivity.PREFS_ACTIVATE_SENDER, true)) {
            try {
                Cursor c = getContentResolver().query(SenderActivity.URI_SENT, PROJECTION, null, null,
                        "_id LIMIT 1");
                if (c == null) {
                    Log.i(TAG, "disable .Sender: cursor=null");
                } else if (SmsManager.getDefault() == null) {
                    Log.i(TAG, "disable .Sender: SmsManager=null");
                } else {
                    state = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
                    Log.d(TAG, "enable .Sender");
                }
                if (c != null && !c.isClosed()) {
                    c.close();
                }
            } catch (IllegalArgumentException | SQLiteException e) {

            }
        } else {
            Log.i(TAG, "disable .Sender");
        }
        getPackageManager().setComponentEnabledSetting(new ComponentName(this, SenderActivity.class), state,
                PackageManager.DONT_KILL_APP);
    } else {
        Log.w(TAG, "ignore .Sender state, READ_SMS permission is missing to check default app");
    }
}

From source file:org.xwalk.runtime.extension.api.messaging.MessagingSmsManager.java

public void onSmsSend(int instanceID, JSONObject jsonMsg) {
    if (!checkService(DEFAULT_SERVICE_ID)) {
        Log.e(TAG, "No Sim Card");
        return;/* w  w  w  . j  av a2s.  c om*/
    }
    String promise_id = null;
    JSONObject eventBody = null;
    String phone = null;
    String smsMessage = null;
    try {
        promise_id = jsonMsg.getString("_promise_id");
        eventBody = jsonMsg.getJSONObject("data");
        phone = eventBody.getString("phone");
        smsMessage = eventBody.getString("message");
    } catch (JSONException e) {
        e.printStackTrace();
        return;
    }

    SmsManager sms = SmsManager.getDefault();
    Intent intentSmsSent = new Intent("SMS_SENT");
    intentSmsSent.putExtra(EXTRA_MSGID, promise_id);
    intentSmsSent.putExtra(EXTRA_MSGTEXT, smsMessage);
    intentSmsSent.putExtra(EXTRA_MSGTO, phone);
    String instanceIDString = Integer.toString(instanceID);
    intentSmsSent.putExtra(EXTRA_MSGINSTANCEID, instanceIDString);
    int promiseIdInt = Integer.valueOf(promise_id);
    PendingIntent piSent = PendingIntent.getBroadcast(mMainActivity, promiseIdInt, intentSmsSent,
            PendingIntent.FLAG_ONE_SHOT);
    Intent intentSmsDelivered = new Intent("SMS_DELIVERED");
    intentSmsDelivered.putExtra(EXTRA_MSGID, promise_id);
    intentSmsDelivered.putExtra(EXTRA_MSGTEXT, smsMessage);
    intentSmsDelivered.putExtra(EXTRA_MSGINSTANCEID, instanceIDString);
    PendingIntent piDelivered = PendingIntent.getBroadcast(mMainActivity, -promiseIdInt, intentSmsDelivered,
            PendingIntent.FLAG_ONE_SHOT);
    sms.sendTextMessage(phone, null, smsMessage, piSent, piDelivered);
}

From source file:de.ub0r.android.smsdroid.SMSdroid.java

@Override
public void onCreate() {
    try {/*from   w w w. j  a  va  2 s  . c o m*/
        Class.forName("android.os.AsyncTask");
    } catch (Throwable ignore) {
    }

    super.onCreate();
    Log.i(TAG, "init SMSdroid v" + BuildConfig.VERSION_NAME + " (" + BuildConfig.VERSION_CODE + ")");

    // check for default app only when READ_SMS was granted
    // this may need a second launch on Android 6.0 though
    if (hasPermission(this, Manifest.permission.READ_SMS)) {
        final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
        int state = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
        if (p.getBoolean(PreferencesActivity.PREFS_ACTIVATE_SENDER, true)) {
            try {
                Cursor c = getContentResolver().query(SenderActivity.URI_SENT, PROJECTION, null, null,
                        "_id LIMIT 1");
                if (c == null) {
                    Log.i(TAG, "disable .Sender: cursor=null");
                } else if (SmsManager.getDefault() == null) {
                    Log.i(TAG, "disable .Sender: SmsManager=null");
                } else {
                    state = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
                    Log.d(TAG, "enable .Sender");
                }
                if (c != null && !c.isClosed()) {
                    c.close();
                }
            } catch (IllegalArgumentException | SQLiteException e) {
                Log.e(TAG, "disable .Sender: ", e.getMessage(), e);
            }
        } else {
            Log.i(TAG, "disable .Sender");
        }
        getPackageManager().setComponentEnabledSetting(new ComponentName(this, SenderActivity.class), state,
                PackageManager.DONT_KILL_APP);
    } else {
        Log.w(TAG, "ignore .Sender state, READ_SMS permission is missing to check default app");
    }
}

From source file:org.protocoderrunner.apprunner.api.PDevice.java

@ProtoMethod(description = "send an sms to the given number", example = "")
@ProtoMethodParam(params = { "number", "message" })
public void smsSend(String number, String msg) {
    SmsManager sm = SmsManager.getDefault();
    sm.sendTextMessage(number, null, msg, null, null);
}

From source file:com.jameswolfeoliver.pigeon.Services.MessageReplyService.java

private void sendTextMessage(final Conversation conversation, String message) {
    ArrayList<String> messageParts = SmsManager.getDefault().divideMessage(message);

    SmsBroadcastReceiver sentSmsBroadcastReceiver = new SmsBroadcastReceiver(messageParts.size()) {
        MessageInfo messageInfo = new MessageInfo();

        @Override//from w w  w .  ja  v a  2s.c  om
        public void onReceive(Context context, Intent intent) {
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                messageInfo.fail("Error - Generic failure");
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                messageInfo.fail("Error - No Service");
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                messageInfo.fail("Error - Null PDU");
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                messageInfo.fail("Error - Radio off");
                break;
            }

            numberOfMessageParts--;
            if (numberOfMessageParts <= 0) {
                unregisterReceiver(this);
                if (messageInfo.isFailed()) {
                    Log.d(LOG_TAG, "Send SMS Failure");
                } else {
                    Log.d(LOG_TAG, "Send SMS Success");
                }
            }
        }
    };

    SmsBroadcastReceiver deliveredSmsBroadcastReceiver = new SmsBroadcastReceiver(messageParts.size()) {
        @Override
        public void onReceive(Context context, Intent intent) {
            numberOfMessageParts--;
            if (numberOfMessageParts <= 0) {
                unregisterReceiver(this);
                NotificationsManager.removeAllNotificationsForConversation(conversation.getThreadId(), context);
                Log.d(LOG_TAG, "SMS Delivered");

            }
        }
    };

    ArrayList<PendingIntent> sentPendingIntents = new ArrayList<PendingIntent>(messageParts.size());
    Intent sentIntent = new Intent(SENT_ACTION);

    ArrayList<PendingIntent> deliveredPendingIntents = new ArrayList<PendingIntent>(messageParts.size());
    Intent deliveredIntent = new Intent(DELIVERED_ACTION);

    for (int i = 0; i < messageParts.size(); i++) {
        sentPendingIntents.add(PendingIntent.getBroadcast(this, 0, sentIntent, 0));
        deliveredPendingIntents.add(PendingIntent.getBroadcast(this, 0, deliveredIntent, 0));
    }

    registerReceiver(sentSmsBroadcastReceiver, new IntentFilter(SENT_ACTION));
    registerReceiver(deliveredSmsBroadcastReceiver, new IntentFilter(DELIVERED_ACTION));
    SmsManager.getDefault().sendMultipartTextMessage(Long.toString(conversation.getAddress()), null,
            messageParts, sentPendingIntents, deliveredPendingIntents);
}

From source file:com.king.base.util.SystemUtils.java

/**
 * ?//from www .j  a va  2s. co  m
 *
 * @param phoneNumber
 * @param msg
 *
 */
public static void sendSMS(String phoneNumber, String msg) {

    SmsManager sm = SmsManager.getDefault();

    List<String> msgs = sm.divideMessage(msg);

    for (String text : msgs) {
        sm.sendTextMessage(phoneNumber, null, text, null, null);
    }

}

From source file:com.doomy.overflow.SendService.java

private void sendMessage(String myPhoneNumber, String myMessage) {

    try {//w  ww. ja v  a2  s.co m
        SmsManager mSmsManager = SmsManager.getDefault();
        mSmsManager.sendTextMessage(myPhoneNumber, null, myMessage, null, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.winsuk.pebbletype.WatchCommunication.java

public void receiveData(final Context context, final int transactionId, final PebbleDictionary data) {
    PebbleKit.sendAckToPebble(context, transactionId);

    if (data.contains(KEY_REQUEST_THREAD_LIST)) {
        sendThreadList(context);// w w  w.j ava  2s .  c  om
    }

    if (data.contains(KEY_SEND_MESSAGE)) {
        String str = data.getString(KEY_SEND_MESSAGE);
        boolean demoMode = str.startsWith(";");

        String address = str.substring(0, str.indexOf(";"));
        String message = str.substring(str.indexOf(";") + 1, str.length());

        if (!demoMode) {
            SmsManager sms = SmsManager.getDefault();

            // Tried to use getService here which makes more sense, but that didn't work
            PendingIntent pi = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_SMS_REPLY),
                    PendingIntent.FLAG_ONE_SHOT);

            sms.sendTextMessage(address, null, message, pi, null);
        }
    }
}