views:

82

answers:

1

I have a widget that displays the time and if one taps on it, it launches the com.android.alarmclock/.AlarmClock activity with an PendingIntent. This works great before-Froyo, but with Froyo, I have to launch the com.android.deskclock/.AlarmClock. So I want to put in code that checks for the class existence and launch the appropriate activity/intent. Here is what I tried, but it does not work.

Intent alarmIntent = new Intent();
try {
    if (Class.forName("com.android.deskclock.AlarmClock") != null) {
    Log.i(TAG, "setting deskclock alarm -- must be Froyo!");
    alarmIntent.setClassName("com.android.deskclock",
        "com.android.deskclock.AlarmClock");
    }
} catch (ClassNotFoundException e) {
    Log.i(TAG, "setting alarmclock alarm -- must be Eclair!");
    alarmIntent.setClassName("com.android.alarmclock",
        "com.android.alarmclock.AlarmClock");
}
PendingIntent pendingIntent = PendingIntent.getActivity(context, REQUEST_UPDATE_TIME_NOW,
    alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.text_timeofday, pendingIntent);

It always thinks it is "Eclair" and therefore fails on Froyo. Is this the best approach, or should I check the application-level? I prefer to work with the class existence.

+2  A: 

if (Class.forName("com.android.deskclock.AlarmClock") != null)

That won't work, because that class is not in your project. At most, it might be in some other project on the device.

There is no documented supported Intent for launching an alarm clock in the Android SDK. Your approach of hard-wiring in package and class names is fragile, as you're discovering. It won't work on some devices, if they do not have that application (e.g., replaced by one from the device manufacturer). Plus, as you've seen, it may change in future versions of Android. I am having a rough enough time trying to convince device manufacturers not to break the SDK; having third-party developers do it weakens my case.

That being said, the general way to see if something will respond to an Intent is to use PackageManager (e.g., queryIntentActivities()).

CommonsWare
I totally agree with your assessment of the fragility. I was getting ready to pose the question about packaging by carriers. Thanks for putting me straight on this approach.
mobibob