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.droid.app.fotobot.FotoBot.java

public void sendSMS(String phoneNumber, String message) {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

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

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

    //---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver() {
        @Override/*from ww  w.  j av a  2 s .  c o m*/
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
                break;
            }
        }
    }, new IntentFilter(SENT));

    //---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_SHORT).show();
                break;
            case Activity.RESULT_CANCELED:
                Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
                break;
            }
        }
    }, new IntentFilter(DELIVERED));

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

From source file:com.yangtsaosoftware.pebblemessenger.services.PebbleCenter.java

public void doSendSMSTo(String phoneNumber, String message) {
    if (PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)) {
        SmsManager smsmanager = SmsManager.getDefault();
        ArrayList<String> smsList = smsmanager.divideMessage(message);
        for (String sms : smsList) {
            smsmanager.sendTextMessage(phoneNumber, null, sms, null, null);
        }//  w w  w  .j a  va 2 s  . c  om
        Constants.log("sendsms", String.format("send:[%s] to number:%s", message, phoneNumber));
    }
}

From source file:com.codename1.impl.android.AndroidImplementation.java

/**
 * @inheritDoc/*from  w  ww  . j av  a  2  s  .c o  m*/
 */
public void sendSMS(final String phoneNumber, final String message, boolean i) throws IOException {
    if (!checkForPermission(Manifest.permission.SEND_SMS, "This is required to send a SMS")) {
        return;
    }
    if (!checkForPermission(Manifest.permission.READ_PHONE_STATE, "This is required to send a SMS")) {
        return;
    }
    if (i) {
        Intent smsIntent = null;
        if (android.os.Build.VERSION.SDK_INT < 19) {
            smsIntent = new Intent(Intent.ACTION_VIEW);
            smsIntent.setType("vnd.android-dir/mms-sms");
            smsIntent.putExtra("address", phoneNumber);
            smsIntent.putExtra("sms_body", message);
        } else {
            smsIntent = new Intent(Intent.ACTION_SENDTO);
            smsIntent.setData(Uri.parse("smsto:" + Uri.encode(phoneNumber)));
            smsIntent.putExtra("sms_body", message);
        }
        getContext().startActivity(smsIntent);

    } else {
        SmsManager sms = SmsManager.getDefault();
        ArrayList<String> parts = sms.divideMessage(message);
        sms.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
    }
}

From source file:org.telegram.ui.PassportActivity.java

private void startPhoneVerification(boolean checkPermissions, final String phone, Runnable finishRunnable,
        ErrorRunnable errorRunnable, final PassportActivityDelegate delegate) {
    TelephonyManager tm = (TelephonyManager) ApplicationLoader.applicationContext
            .getSystemService(Context.TELEPHONY_SERVICE);
    boolean simcardAvailable = tm.getSimState() != TelephonyManager.SIM_STATE_ABSENT
            && tm.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE;
    boolean allowCall = true;
    if (getParentActivity() != null && Build.VERSION.SDK_INT >= 23 && simcardAvailable) {
        allowCall = getParentActivity()/*from ww  w  .  j a va  2  s. c o m*/
                .checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED;
        if (checkPermissions) {
            permissionsItems.clear();
            if (!allowCall) {
                permissionsItems.add(Manifest.permission.READ_PHONE_STATE);
            }
            if (!permissionsItems.isEmpty()) {
                if (getParentActivity()
                        .shouldShowRequestPermissionRationale(Manifest.permission.READ_PHONE_STATE)) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
                    builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
                    builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), null);
                    builder.setMessage(LocaleController.getString("AllowReadCall", R.string.AllowReadCall));
                    permissionsDialog = showDialog(builder.create());
                } else {
                    getParentActivity().requestPermissions(
                            permissionsItems.toArray(new String[permissionsItems.size()]), 6);
                }
                pendingPhone = phone;
                pendingErrorRunnable = errorRunnable;
                pendingFinishRunnable = finishRunnable;
                pendingDelegate = delegate;
                return;
            }
        }
    }
    final TLRPC.TL_account_sendVerifyPhoneCode req = new TLRPC.TL_account_sendVerifyPhoneCode();
    req.phone_number = phone;
    req.settings = new TLRPC.TL_codeSettings();
    req.settings.allow_flashcall = simcardAvailable && allowCall;
    if (Build.VERSION.SDK_INT >= 26) {
        try {
            req.settings.app_hash = SmsManager.getDefault()
                    .createAppSpecificSmsToken(PendingIntent.getBroadcast(ApplicationLoader.applicationContext,
                            0, new Intent(ApplicationLoader.applicationContext, SmsReceiver.class),
                            PendingIntent.FLAG_UPDATE_CURRENT));
        } catch (Throwable e) {
            FileLog.e(e);
        }
    } else {
        req.settings.app_hash = BuildVars.SMS_HASH;
        req.settings.app_hash_persistent = true;
    }
    SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig",
            Activity.MODE_PRIVATE);
    if (!TextUtils.isEmpty(req.settings.app_hash)) {
        req.settings.flags |= 8;
        preferences.edit().putString("sms_hash", req.settings.app_hash).commit();
    } else {
        preferences.edit().remove("sms_hash").commit();
    }
    if (req.settings.allow_flashcall) {
        try {
            @SuppressLint("HardwareIds")
            String number = tm.getLine1Number();
            if (!TextUtils.isEmpty(number)) {
                req.settings.current_number = phone.contains(number) || number.contains(phone);
                if (!req.settings.current_number) {
                    req.settings.allow_flashcall = false;
                }
            } else {
                req.settings.current_number = false;
            }
        } catch (Exception e) {
            req.settings.allow_flashcall = false;
            FileLog.e(e);
        }
    }

    ConnectionsManager.getInstance(currentAccount).sendRequest(req,
            (response, error) -> AndroidUtilities.runOnUIThread(() -> {
                if (error == null) {
                    HashMap<String, String> values = new HashMap<>();
                    values.put("phone", phone);
                    PassportActivity activity = new PassportActivity(TYPE_PHONE_VERIFICATION, currentForm,
                            currentPassword, currentType, null, null, null, values, null);
                    activity.currentAccount = currentAccount;
                    activity.saltedPassword = saltedPassword;
                    activity.secureSecret = secureSecret;
                    activity.delegate = delegate;
                    activity.currentPhoneVerification = (TLRPC.TL_auth_sentCode) response;
                    presentFragment(activity, true);
                } else {
                    AlertsCreator.processError(currentAccount, error, PassportActivity.this, req, phone);
                }
            }), ConnectionsManager.RequestFlagFailOnServerErrors);
}