Friday 18 February 2011

Start the youtube player in a simple way

Don't spend time on reinventing the wheel. If your app needs to play a youtube video, just start the youtube player like this:

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM")));

Thursday 3 February 2011

Reloading a WebView every x seconds

If you load a page using WebView and want to reload that page to show the latest updates etc. java has an easy solution for that:
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); exec.scheduleAtFixedRate(new Runnable() { @Override public void run() { Shout.loadUrl("http://www.bengaard.com/OB/shoutstart.php?konto="+konto); } }, 10, 20, TimeUnit.SECONDS);


So this piece of code simply reloads the page every 20 seconds with a 20 second initial delay which is very ideal for updating my shoutbox. Of course, you can change the WebView with whatever code you need to execute between fixed intervals.

Tuesday 1 February 2011

Login using your Google account

Since 99,99% of all android users have a gmail account it can sometimes be useful to use that email address as a login or username for the users of your app. Here's how to do it:

protected AccountManager accountManager; accountManager = AccountManager.get(getApplicationContext()); Account[] accounts = accountManager.getAccountsByType("com.google");
The variable accounts will now hold an array of all the google accounts ever used on the phone. So to get the first account/email address registered on the phone try something like

konto = accounts[0].name;
You can then use the account name/email address for login, shoutbox name, etc. etc.