Thursday, February 4th, 2021 android code utils • 172w
For all of you that need to sent a simple preset SMS without copy pasting all the time (covid-era).

For some reason on the play store most of the app that provide this kind of operation instead of sending the SMS they just open the messages app...

Ok, lets do it:

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("phone_number", null, "message", null, null);

And because this android we need to haggle with the permissions:

<uses-permission android:name="android.permission.SEND_SMS" />
if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) 
		!= PackageManager.PERMISSION_GRANTED) {
	ActivityCompat.requestPermissions(this, new String[] {
    		Manifest.permission.SEND_SMS}, 0);
}

And we are done, that's it.



done_
Saturday, January 18th, 2020 android code it is known • 134w
I was writing a confirm dialog on android, and there was it:

    android.R.string.yes

So lets use it, why not. However my extreme imagination, for some reason, had the actual value of it being 'Yes'.
Build, run, test, boom! There was it, on the screen, a 'OK' laughing at my face.

So turns out there is a comment:

    <!-- Preference framework strings. -->
    <string name="yes">OK</string>

And they say comments are overrated...

PS: Just look at this, look deep:
    <string name="yes">OK</string>


done_
Friday, November 13th, 2015 android code utils • 194w
We can save and load values, permanently, using Shared Preferences.

The shared preferences framework provides an easy way to store values, not only preference related but any kind of values. It's a file based so keep that in mind.

To get the SharedPreferences object we need:

private static final String NAME = "...";
public static SharedPreferences getPreferences(Activity a) {
  return a.getSharedPreferences(NAME , Context.MODE_PRIVATE);
}

To save a values we:

public static void save(Activity a) {
  SharedPreferences.Editor sp = getPreferences(a).edit();

  sp.putString("name1", value1);
  sp.putInt("name2", value2);

  sp.commit();
}

And to load a values we:

public static void load(Activity a) {
  SharedPreferences sp = getPreferences(a);

  value1 = sp.getString("name1", default1);
  value2 = sp.getInt("name2", default2);
}

We can also get a single Preferences for each activity with:

getPreferences(Context.MODE_PRIVATE)

Except MODE_PRIVATE we have MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE which are deprecated, so don't use them.


done_