Android Tip: Preferences - Easier Than Using a Database - With Code Sample!

Attached File(s): 

If you have done any programming for an Android phone, you've probably already used (or at least heard of) Android's built-in database option of SQLite.  SQLite is a fine system, but it can be cumbersome to work with, especially if you only need to store a few values. 

samsung-omnia-hd-android.jpgEnter your new best friend: Preferences.

Think of "Preferences" as a way to save data (like Strings) outside of your application.  It's like a database in that way, but it is much easier to put data in and get data out.  If you build a Preferences Activity at some point, then it will actually be using this very system-- but that's another blog post.

I have included my own class for working with Preferences.  It is linked at the top of the post.  To use it, instantiate a Prefs object like so:

Prefs prefs = new Prefs(context);
// Prefs prefs = new Prefs(this) will work 
// if you are within an Activity

Now, to store a String value, all you have to do is this:

String mUserNameString = "rpeacock";
prefs.setPreference("username", mUserNameString);

To retrieve that value later, even after the app has closed and restarted, all you have to do is this:

String temp = prefs.getPreference("username");

Isn't that easy?  More to the point, isn't that much easier than creating an SQLite object, constructing a query, executing the query, then closing the connection?  That's why I use Preferences everywhere I can.  It's also great for accessing values between Activities, services, etc.

The code I attached only does Strings, but you can easily extend it to work with any value.  Enjoy!

 


Software License Fine Print

Creative Commons LicenseThis code is licensed as-is, with no warranty or guarantee, under a Creative Commons Attribution 3.0 Unported License.

You may use this code however you want, even in commercial products, but only if you include attribution to me, Richard Peacock, as the original author of this small portion of code, but not in a way that implies I endorse your project.

For example, on a Help/Credits screen:  Portions of this project were based on work freely obtained from these developers: Richard Peacock (http://richardpeacock.com), NAME HERE, etc.  These outside developers neither endorse nor support this software.

If you use this code, feel free to email me to let me know!  I'll include a link to your project here.

Top Web Sites Running Linux

Last year, I posted an article detailing the OS of choice for the top 10 web sites, finding that most of them were running some flavor of Linux. 

Linux.jpgWell, since a year has passed, I thought it might be nice to check again, not just the top 10, but some other well-known sites.

Here are my results, retrieved April 3, 2011:

Ranking from http://www.alexa.com/topsites
Results from http://uptime.netcraft.com/up/

Ranking Site OS
1 Google Linux
2 Facebook Linux
3 YouTube

Linux

4 Yahoo! Linux
5 Blogger Linux
6 Baidu unknown, running apache
7 Windows Live Windows-based
8 Wikipedia Linux
9 Twitter Linux
10 QQ Linux

 As you can see, 8 of the top 10 are running some flavor of Linux, which speaks volumes about the reliability of the OS.  Many of the top 11+ sites are just foreign variations on these sites.  For example, Google India is #14, Yahoo.jp is #12. 

So, I have decided to select several other sites which English-speakers are more likely to have used... 

Is Your Laptop Overheating?

Recently, my laptop started running hotter and hotter, to the point that it would shut itself down out of protection.  Speedfan reported my core temperature (under load) above 100C (the temperature of boiling water)!  It idled around 60 - 70C.  I almost went out a bought a new laptop, assuming my processor or power supply or something must be breaking down.

laptopfire.jpgWell, I'm glad I didn't, because I fixed it with one simple trick after some googling.  All you need to do is break out your vacuum cleaner, and clear the dust from the laptop's cooling system.

On most laptops, that means two points:  an intake vent, and an exhaust vent.  The idea is that air is sucked into the laptop through the intake vent, passes over your hot components, and then blows out of the exhaust vent, cooling everything down in the process.  This is all done with 1 or 2 fans, which can get very dusty over time.

It's that dust that causes the laptop to overheat.  It chokes the fans and creates a blanket over your components.

So, just take your vacuum cleaner's hose attachment and go over both vents really well.  The intake vent is probably on the bottom of the laptop, and the exhaust is probably on the side or back. On some laptops, to really get rid of all the dust, you might have to take the cover off and blow out the fans/vents with compressed air.  I didn't have to with mine, though.

After doing this, my laptop idles around 52-54C, and under load it gets to around 60-65C.  It's still a little hotter than I want it to be, but that's nothing compared to 100C!

So if you've got a dangerously overheating laptop (use Speedfan to check) then give this tip a try.  If it doesn't work, it probably means you have a more serious-- and more expensive-- problem.

 

Dropbox and Programming - A Match Made in Heaven

For those that don't know, Dropbox is a free service which lets you share a folder across multiple computers, even with different OSes like Mac, Windows, and Linux.  It's great for keeping all your personal files with you on both your work and home PC (without the need for a USB drive).  But where it really shines for me is when it comes to programming.

Dropbox-Review-2.jpgProgramming from multiple computers is always a bit of a pain.  What I normally do is I keep all my files on a USB drive, and just have to remember to carry it with me every where I go.  I'm always worried about breaking or losing it, so I have to back it up like crazy.

Another option is to use a version control system like Subversion or Git.  They work (usually), but you've got to remember to commit changes before moving to another computer.  For newbies they can be complicated to set up, and might be overkill for smaller one-man projects. Plus my IDE for Android development, Eclipse, just doesn't want to play nice with Subversion.

Enter Dropbox, my new hero.

Using Synergy With a Null Modem Cable

If you've never heard of Synergy, check out there website here.  In a nutshell, it's a program you run on two computers, which lets you use the keyboard and mouse of one to control the other.  Think of it a little bit like a KVM switch in software.  The connection is made over your network or the Internet.  To switch machines you just slide your mouse off the side of one screen and onto another, as if you just had a second monitor set up.

null-modem-cable.jpgRecently at my day job, I used Synergy to connect my main work computer to a spare computer I set on my desk but did not want to make room for another keyboard and mouse.  I also did not want to connect our company network to the spare computer, for security reasons.  Now, I could have just used a cross-over ethernet cable, but did not have a spare ethernet port on my main computer.

Enter my savior: an ancient serial Null Modem cable.

In the good old days, these little serial cables were used to connect two computers together at a relatively low speed for file transfers and the like.  And if you happen to have one laying around (possible with an extra "null modem" block attached, possibly not), then this guide will help you set up two Windows computers to use it to work with Synergy.

DVDStyler - So Easy My Mom Can Use It

I don't normally go out of my way to recommend software, but I feel like I must for a program called DVDStyler (http://www.dvdstyler.org/).

ffm.jpgA little backstory:  I am giving my parents a new digital camcorder for Christmas, and I wanted them to be able to create their own DVDs from the videos they shoot.  A little more backstory: my parents are terrified of technology.  There is no way I could have given them Roxio, Ulead, AVS, or any of the other solutions out there-- just too complicated.  Not to mention the fact that they can be costly.

I spent literally hours going through demos of all the popular solutions, trying to find something I deemed was easy enough for them to use (with a custom printed guide from me).  Then I found DVDStyler.

Here's the top things to know about it:

  • It's free/open source.
  • It's very simple to use, but also has some cool advanced features for more experienced users.
  • It can handle lots of different video formats (cameras might use MP4, MOV, or AVI).
  • It also burns the disc when finished.

 It also works for Unix, too.  So if this sounds like something you'd like to use, check it out.

Tips: Creating Web Sites for Mobile Phones

I've recently learned a few technical tips you should use when creating web sites for mobile devices (like the iPhone, Android, etc) and though I'd share them all in one place.

9658-bbcskyfirecopy.jpg#1: Size matters

This should be obvious, I guess, but you're dealing with a smaller screen.  Unfortunately, you really have no idea what the screen size is, so you have to low-ball it.  Various sources on the Internet agree that you shouldn't have images, tables, divs, etc. any wider than 240 pixelsMaybe 320, but you're pushing it, mister.  Of course, that could change as time goes on, but at the time of this writing, 240px seems to be the most widely accepted width that fits on the most screens.

Also, don't forget to place this at the top of your page:

<meta name='viewport' 
   content='initial-scale=1.0, user-scalable=no' />

This will cause the device to be zoomed-in on your content, and not let the user zoom out any more.  Just try it out and you'll see what I mean.

IE Can Only Handle 31 Stylesheets -- Thanks IE!

Yes, our old friend Internet Explorer is at it again to make our lives as web developers miserable.  Not many know this, but in IE, a single web page can only reference at most 31 <link>'d CSS stylesheets. For hand-coded pages, this probably isn't a problem.  But when you start using larger CMS systems and applications, like Drupal, you can very quickly run into the 31 stylesheet limit.  This happens in currently all versions of IE, including IE 8.

ie_sucks_sm-300x225.pngMost of the time this problem presents itself, the web developer believes he or she is going insane.  Your site looks beautiful in FireFox and Chrome, but in IE everything is off: some things are positioned weird, fonts are the wrong size, etc.  And IE doesn't warn you or let you know this is happening in any way, so you are left to go silently mad trying to figure out what the problem is.

There are a couple possible solutions:

  • Combine stylesheets into single, large files.  This may not always be practical or very organized, but hey, you've got to do what you've got to do.
  • Use @import instead of <link>
    • For example:
      <style>
      @import url('a.css');
      </style>
    • IE seems to be okay with this, and will allow more than 31.  However, @import isn't as great, performance-wise, as <link>.  Also, it may cause stylesheets to be loaded out of order, which can cause JavaScript problems.
  • If you are using Drupal, you can try the modules http://drupal.org/project/ie_css_optimizer or http://drupal.org/project/unlimited_css to get around the IE problem.

 Let's hope that the next version of IE lifts this bizzare restriction (but I'm not holding my breath).

Safer Login - Drupal 6 Module

Note!  This project is now an official Drupal project.  You may download it by going here: http://drupal.org/project/safer_login

network-security-lock.jpgWe all love Drupal, but unless you have an SSL certificate on your site, logins are not secure.  Usernames and passwords are submitted "in the clear" (meaning, not encrypted in any way).  This may not seem like such a big deal, but if you or your site visitors use the same usernames and passwords for everything-- say, Facebook, Bank accounts, Email, etc-- then a stolen password from your site can be a huge security risk.  The threat of a hacker sitting in the back of a StarBucks or computer lab with a laptop and a free packet sniffer is very real and very easy to pull off.

So, I created the Drupal 6 module "Safer Login."  It uses JavaScript to encrypt your visitor's passwords before they are ever submitted through the browser.  If they have JavaScript disabled, then they will log into Drupal the regular way with no warnings or error messages.

The module actually uses a salted approach to hash your visitor's password, and the salt changes with every log in attempt, so a "replay attack" is not possible.

If this module sounds useful to you, please use the download link at the top of this post to grab it.  Enjoy!

Oracle_query() and Other Oracle Convenience Functions

Attached File(s): 

If you're used to using MySQL with PHP, you already know of the simple and easy to use mysql_query(), mysql_fetch_object(), mysql_affected_rows() and so forth functions.  Well, if you've ever dipped your toes into the tepid waters of Oracle, you know that it lacks the friendly PHP functions that MySQL has.  So, I created a set of convenience functions designed to mirror the MySQL ones.

Just download the attached file at the top, and use it like so:

<?php
  
  include_once("oracle_common_functions.php");

  oracle_connect($username, $pass, $host, "TEST");
  $res = oracle_query(" SELECT * FROM $table_name ") or echo(oracle_error());
  while ($cur = oracle_fetch_array($res)) {
    var_dump($cur);
  }  
  
  oracle_close();
  
?>

Just remember that to use any of this, you also need PHP to be compiled with the necessary oci libraries.

Complete list of functions in the file:

oracle_connect()
oracle_query()
oracle_fetch_array()
oracle_fetch_object()
oracle_affected_rows()
oracle_close()
oracle_error()
oracle_errno()
oracle_escape_string()

Enjoy!

 


Software License Fine Print

Creative Commons LicenseThis code is licensed as-is, with no warranty or guarantee, under a Creative Commons Attribution 3.0 Unported License.

You may use this code however you want, even in commercial products, but only if you include attribution to me, Richard Peacock, as the original author of this small portion of code, but not in a way that implies I endorse your project.

For example, on a Help/Credits screen:  Portions of this project were based on work freely obtained from these developers: Richard Peacock (http://richardpeacock.com), NAME HERE, etc.  These outside developers neither endorse nor support this software.

If you use this code, feel free to email me to let me know!  I'll include a link to your project here.

Syndicate content