如何一个发布依赖Android版本的闹钟

如何一个发布依赖Android版本的闹钟

本文介绍了如何一个发布依赖Android版本的闹钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗口小部件显示时间,如果有人点击它,它启动com.android.alarmclock / .AlarmClock活动与 PendingIntent 。这工作伟大的前Froyo,但与Froyo,我必须启动com.android.deskclock / .AlarmClock。所以我想把代码检查类的存在并启动适当的活动/意图。这是我尝试,但它不工作。

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);

它总是认为它是Eclair,因此在Froyo上失败。这是最好的方法,还是应该检查应用程序级别?我喜欢使用类的存在。

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.

推荐答案

这不会工作,因为这个类不在你的项目中。

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

没有文档支持 Intent 用于启动Android SDK中的闹钟。你的方法在硬件接线包和类名是脆弱的,因为你发现。如果某些设备没有该应用程序(例如,由设备制造商替换为某个设备),则它将无法在某些设备上正常工作。此外,如你所见,它可能会在Android的未来版本中更改。我有足够的时间试图说服设备制造商不打破SDK; 有第三方开发人员会削弱我的

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.

这就是说,一般的方式看看是否有什么东西会响应 Intent 是使用 PackageManager (例如, queryIntentActivities())。

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

这篇关于如何一个发布依赖Android版本的闹钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!