Tuesday 13 September 2011

Cursor management in Honeycomb


If you are targeting your application for honeycomb, then some APIs related to cursor management are deprecated and your application won't work smoothly in honeycomb, even though it will work well in other versions of Android. In Honeycomb, cursor management is more tightly coupled and Some of the APIs that are deprecated are

- startManagingCursor()
- stopManagingCursor()
- managedQuery()
- reQuery()


If you are using any of these methods in HoneyComb, then you will get an exception, like

java.lang.IllegalStateException: trying to requery an already closed cursor

If you are using managedQuery() in lower versions of Android(2.3,2.2) as,

cursor = context.managedQuery(android.provider.Browser.BOOKMARKS_URI, projection, null, null,
null);


then in Honeycomb you need to modify as,

CursorLoader cursorLoader = new CursorLoader(context, android.provider.Browser.BOOKMARKS_URI, projection, null, null,
null);
cursor = cursorLoader .loadInBackground();

Other modification is , you don't need to call reQuery in honeyComb. You have LoaderManager callbacks are there in honeyComb. That will be called if there is any change in the underlying data.

For implementing LoaderManager callbacks

- First implement the interface in your class as LoaderManager.LoaderCallbacks
- in onCreate you need to initialize the loader as
getLoaderManager().initLoader(0, null, this);
- for reQuery, you can use, getLoaderManager().restartLoader(0, null, this);
- Then you need to override three methods in it as,
- onCreateLoader()
- onLoadFinished()
- onLoaderReset()

Static variables in your Android Application

Instead of declaring static variables in an activity you can also do it in your own subclass of the Activity class, which is a singleton.

Within strings.xml I have the application version.


<resources>
   <string name="version_name">2.0.0</string>
</resources

I didn't want to store this value second time as a constant in my java code. And here is the solution.

public class MyApp extends Application
{
   private static String appVersion = "";
   public static void setAppVersion (String version)
   {
      appVersion = version;
   }
 
   public static String getAppVersion ()
   {
      return appVersion;
   }
}
 
 
In my first activity I initialize version variables:
public void onCreate(Bundle savedInstanceState)
{
   ...
   String appVersion = this.getString(R.string.version_name);
   MyApp.setAppVersion(appVersion);
   ...
}
 
Now version value can be read everywhere in your application also in normal java classes:




...
String version = MyApp.getEasyGOVersion();
...
 
Try this :)

Android World: Video streaming using RTSP in android

Android World: Video streaming using RTSP in android: public class VideoViewDemo extends Activity { private static final String TAG = "VideoViewDemo"; private VideoView mVideoView; private E...