Coding for Mobile Platforms

Random thoughts, sometimes relevant to mobile programming.

  • Mobile Coder

  • Subscribe to Mobile Coder and receive notifications of new posts by email.

    Join 185 other followers

Archive for February, 2010

Global Wakelocks for dummies (like me)

Posted by error454 on 02/15/2010


For those that are trying to implement a global variable of any kind.  Here is how I was able to successfully do so.

Step 1: Create a class for your global variables that extends Application

package com.company.application;

import android.app.Application;
import android.os.PowerManager;

public class MyGlobals extends Application {
   private static PowerManager.WakeLock wl=null;

   public PowerManager.WakeLock getWakeLock(){
      return wl;
   }

   public void setWakeLock(PowerManager.WakeLock lock){
      wl = lock;
   }

   @Override
   public void onCreate()
   {
      super.onCreate();
   }
}

Step 2: Initialize globals if necessary in the onCreate method of your starting Activity

public class myApp extends Activity {

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      //Initialize the wakelock
      PowerManager pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
      ((MyGlobals) this.getApplication()).setWakeLock(pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PushPinService"));
   }
}

Step 3: Modify your manifest file to load your class on startup.  This means modifying your existing <application> tag to include the android:name field

<application android:name="MyGlobals" android:icon="@drawable/icon" android:label="@string/app_name">

Posted in Android | Tagged: , , | Leave a Comment »

Android ListView LongClick

Posted by error454 on 02/12/2010


I found myself very confused today while attempting to determine the currently selected menu item from a ListView object on a LongClick event.

As it turns out, the ListView.getSelectedItem() will always return NULL for your LongClick events because nothing is actually selected.

So how to do this seemingly simple task?  Well first of all, it makes more sense to use a Context Menu to handle a long click, so this example will be based around that construct.

The short version of the story is to use the AdapterContextMenuInfo class to grab the juicy stuff returned by MenuItem’s getMenu.  So when you implement your context menu, in the onContextItemSelected override:

@Override
public boolean onContextItemSelected(MenuItem item) {
   final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
   . . .
}

info.id is the row ID for the selected entry.  You can now reference the array or database that backs your ListView with this index.

Posted in Android | Tagged: , , | Leave a Comment »

SimpleCursorAdapter

Posted by error454 on 02/11/2010


SimpleCursorAdapters are used to take a cursor (data from a SQL query) and map it to a View (a widget in your UI like a ListView).

I found several parameters very confuzzling the first, second and third time I used SimpleCursorAdapters.  Lets break-down the odd parameter list:

SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to)

Context context – If you aren’t sure what this is, go google it.

int layout – Notice this is an integer.  This field tells the adapter  the id of the layout you will be using to display your results.  This works in conjunction with the to parameter.

Cursor c – Your cursor object

String[] from – The name of the columns from your database that you want to display junk from.

int[] to – The id of the view(s) that you want your junk displayed on

So this seems fairly simple, and my definition of these terms really doesn’t deviate far from the built-in help viewer.  But in every example that uses SimpleCursorAdapter you will find that they set the layout parameter to android.R.layout.simple_list_item_1.  The problem/confusion comes in here, how do we know what Views are contained in android.R.layout.simple_list_item_1 so that we can properly set the to parameter?!?

Well to save us all trouble, someone out there had the android source and posted the contents of these items:

simple_list_item_1:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        style="android:attr/listItemFirstLineStyle"
        android:paddingTop="2dip"
        android:paddingBottom="3dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
/>

simple_list_item_2:

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
               android:paddingTop="2dip"
               android:paddingBottom="2dip"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
>
<TextView android:id="@android:id/text1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          style="android:attr/listItemFirstLineStyle"
/>
<TextView android:id="@android:id/text2"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_below="@android:id/text1"
          style="android:attr/listItemSecondLineStyle"
/>
</TwoLineListItem>

So, to make the rubber meet the road.  If you are using simple_list_item_1 then you need your to parameter to be android.R.id.text1.

Your code might look like this, note I’m using a database helper class to perform my SQL query and get my results.

DbAdapter.KEY_TITLE = &quot;name&quot;:

Cursor c = mDbHelper.getAllEntries();
startManagingCursor(c);

ListAdapter adapter =
new SimpleCursorAdapter(mContext, android.R.layout.simple_list_item_1, c, new String[] {DbAdapter.KEY_TITLE}, new int[] {android.R.id.text1});
listView.setAdapter(adapter);

Posted in Android | Tagged: , , | 2 Comments »

FileNotFoundException

Posted by error454 on 02/09/2010


I had previously saved a file in my Android application.  Why can’t I open the file with this code?

FileReader myFile = new FileReader("PreferencesForCoolPeople");

For those that remember Android 101, this type of file access will only succeed if you are reading/writing from the SD Card.  In order to get access to your application’s private files you need to use the openFileInput/openFileOutput through your application’s context.  Note, I make a habit of storing context away in a variable named mContext in my class, I’m not sure if this is a bad habit, but it makes referencing your context a lot easier.

mContext.openFileInput("PreferencesForCoolPeople");

But we want to store our filename in our strings.xml file and we want a BufferedReader.  So that looks like this.

BufferedReader reader = new BufferedReader(
                                 new InputStreamReader(
                                    mContext.openFileInput(
                                    getString(R.string.PreferencesForCoolPeople)
)));

Posted in Android | Tagged: , , | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.

Join 185 other followers

%d bloggers like this: