How does one launch android version dependent alarm clock? [duplicate]

一世执手 提交于 2019-12-11 17:36:03

问题


Possible Duplicate:
Intent to launch the clock application on android

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.


回答1:


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()).



来源:https://stackoverflow.com/questions/3463317/how-does-one-launch-android-version-dependent-alarm-clock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!