怎样才能推出Android版本的依赖闹钟? [英] How does one launch android version dependent alarm clock?

查看:219
本文介绍了怎样才能推出Android版本的依赖闹钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/3590955/intent-to-launch-the-clock-application-on-android\">Intent在Android 启动时钟应用程序


我有一个显示时间的窗口小部件,如果一个就可以了水龙头,它会启动与的PendingIntent 的com.android.alarmclock / .AlarmClock活动。这个伟大的工程前,升级Froyo,但与升级Froyo,我不得不启动com.android.deskclock / .AlarmClock。所以,我希望把code,它会检查是否存在类和推出相应的活动/意图。以下是我试过,但它不能正常工作。

 意图alarmIntent =新意图();
尝试{
    如果(的Class.forName(com.android.deskclock.AlarmClock)!= NULL){
    Log.i(TAG,设置报警DeskClock中 - 必须升级Froyo!);
    alarmIntent.setClassName(com.android.deskclock
        com.android.deskclock.AlarmClock);
    }
}赶上(ClassNotFoundException的E){
    Log.i(TAG,设置闹钟闹铃 - 必须埃克莱尔!);
    alarmIntent.setClassName(com.android.alarmclock
        com.android.alarmclock.AlarmClock);
}
的PendingIntent的PendingIntent = PendingIntent.getActivity(背景下,REQUEST_UPDATE_TIME_NOW,
    alarmIntent,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.text_timeofday,的PendingIntent);

它总是认为这是巧克力慕斯蛋糕,因此失败的升级Froyo。这是最好的办法,还是应该检查应用程序级?我preFER与类存在工作。


解决方案

  

如果(的Class.forName(com.android.deskclock.AlarmClock)!= NULL)


这是行不通的,因为那类不在您的项目。至多,它可能是在该装置上的一些其他项目

有是没有形成文件支持意图在Android SDK中启动一个闹钟。你的包和类的名称硬接线的方法是脆弱的,因为你发现。它不会在某些设备上工作,如果它们不具有该应用程序(例如,从设备制造商所取代一个)。此外,正如你所看到的,它可能会在Android的未来版本的变化。我有一个粗略的足够的时间试图说服设备制造商不打破SDK; 有第三方开发者做削弱了我的情况

话虽这么说,一般的方法,如果事情会回应一个意图是使用软件包管理系统(例如, queryIntentActivities())。

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.

解决方案

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

这篇关于怎样才能推出Android版本的依赖闹钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆