打算在 android 上启动时钟应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3590955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Intent to launch the clock application on android
提问by John
I am facing a problem with a clock widget i made. I want the user to touch the clock and launch the clock app on the phone. this is the code:
我制作的时钟小部件出现问题。我希望用户触摸时钟并在手机上启动时钟应用程序。这是代码:
//this worked on my nexus 2.1
if(VERSION.SDK.equals("7")){
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent AlarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setComponent(new ComponentName("com.android.deskclock", "com.android.deskclock.DeskClock"));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, AlarmClockIntent, 0);
views.setOnClickPendingIntent(R.id.Widget, pendingIntent);
AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);
}
//this worked on my nexus +froyo2.2
else if(VERSION.SDK.equals("8")){
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent AlarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setComponent(new ComponentName("com.google.android.deskclock", "com.android.deskclock.DeskClock"));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, AlarmClockIntent, 0);
views.setOnClickPendingIntent(R.id.Widget, pendingIntent);
AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);
}
//this worked on my htc magic with 1.5 and 1.6
else{
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent AlarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setComponent(new ComponentName("com.android.alarmclock", "com.android.alarmclock.AlarmClock"));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, AlarmClockIntent, 0);
views.setOnClickPendingIntent(R.id.Widget, pendingIntent);
AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);
}
I made the above so the when i touch the clock opens the alarm settings. But is not universal. I found out that on droids with 2.2 does not work. There must be a better solution than creating an if statement for every android phone flavor i the world. Plus i don not know the package names for all. Anyone knows how to overcome this please help.
我做了上述操作,所以当我触摸时钟时会打开闹钟设置。但不是万能的。我发现在 2.2 的机器人上不起作用。必须有比为世界上每个 Android 手机风格创建 if 语句更好的解决方案。另外我不知道所有的包名。任何人都知道如何克服这个请帮助。
回答by frusso
This code works for my clock widget.
此代码适用于我的时钟小部件。
PackageManager packageManager = context.getPackageManager();
Intent alarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
// Verify clock implementation
String clockImpls[][] = {
{"HTC Alarm Clock", "com.htc.android.worldclock", "com.htc.android.worldclock.WorldClockTabControl" },
{"Standar Alarm Clock", "com.android.deskclock", "com.android.deskclock.AlarmClock"},
{"Froyo Nexus Alarm Clock", "com.google.android.deskclock", "com.android.deskclock.DeskClock"},
{"Moto Blur Alarm Clock", "com.motorola.blur.alarmclock", "com.motorola.blur.alarmclock.AlarmClock"},
{"Samsung Galaxy Clock", "com.sec.android.app.clockpackage","com.sec.android.app.clockpackage.ClockPackage"} ,
{"Sony Ericsson Xperia Z", "com.sonyericsson.organizer", "com.sonyericsson.organizer.Organizer_WorldClock" },
{"ASUS Tablets", "com.asus.deskclock", "com.asus.deskclock.DeskClock"}
};
boolean foundClockImpl = false;
for(int i=0; i<clockImpls.length; i++) {
String vendor = clockImpls[i][0];
String packageName = clockImpls[i][1];
String className = clockImpls[i][2];
try {
ComponentName cn = new ComponentName(packageName, className);
ActivityInfo aInfo = packageManager.getActivityInfo(cn, PackageManager.GET_META_DATA);
alarmClockIntent.setComponent(cn);
debug("Found " + vendor + " --> " + packageName + "/" + className);
foundClockImpl = true;
} catch (NameNotFoundException e) {
debug(vendor + " does not exists");
}
}
if (foundClockImpl) {
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, alarmClockIntent, 0);
// add pending intent to your component
// ....
}
In this way, I can run a default clock manager.
通过这种方式,我可以运行一个默认的时钟管理器。
回答by resnbl
To follow up to Mayra's answer, the class android.provider.AlarmClock
has the proper Intent
for this, but it is available only at API level 9 and above. (At least someone at Google listens to requests like this!)
为了跟进 Mayra 的回答,该类android.provider.AlarmClock
具有Intent
此功能,但仅适用于 API level 9 及更高级别。(至少谷歌有人会听这样的请求!)
To launch the clock app from your widget the following code works:
要从您的小部件启动时钟应用程序,请使用以下代码:
Intent openClockIntent = new Intent(AlarmClock.ACTION_SET_ALARM);
openClockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(openClockIntent);
You also need this permission:<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
您还需要此权限:<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
回答by Cheryl Simon
The correct way to start an activity outside of your application is to use a Implicit intent. With an implicit intent you do not provide a specific class to start, instead you specify an action that other apps can choose to act upon.
在应用程序之外启动 Activity 的正确方法是使用隐式意图。使用隐式意图,您不会提供特定的类来启动,而是指定其他应用程序可以选择执行的操作。
This is to prevent breakage in your app when the creator of the other app decides to rename their class, or some such. Also, it allows multiple apps to respond to the same action.
这是为了防止当另一个应用程序的创建者决定重命名他们的类时,您的应用程序被破坏,或类似的。此外,它还允许多个应用程序响应相同的操作。
See intents and intent filtersfor more details on implicit intents.
有关隐式意图的更多详细信息,请参阅意图和意图过滤器。
Unfortunately, you need to app developers to agree to all implement a particular action in order for this to work. If the clock apps don't have this, then there is no good way to accomplish what you want.
不幸的是,您需要应用程序开发人员同意所有实施特定操作才能使其正常工作。如果时钟应用程序没有这个,那么就没有好的方法来完成你想要的。
I would suggest against trying to create a huge if statement like you have, since the app could be different not only with different sdk versions, but with different phones since the phone manufacturers can choose to replace apps. Thus, your app is going to be constantly breaking on new phones, if you rely on being able to open the clock app.
我建议不要尝试像您那样创建一个巨大的 if 语句,因为该应用程序不仅可能与不同的 sdk 版本不同,而且与不同的手机不同,因为手机制造商可以选择替换应用程序。因此,如果您依赖能够打开时钟应用程序,那么您的应用程序将在新手机上不断更新。
回答by iGio90
From api level 19 you can use this to show the alarm list on the default clock app:
从 api 级别 19 开始,您可以使用它在默认时钟应用程序上显示闹钟列表:
Intent mClockIntent = new Intent(AlarmClock.ACTION_SHOW_ALARMS);
mClockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mClockIntent);
回答by Ahamadullah Saikat
Code:
代码:
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
i.putExtra(AlarmClock.EXTRA_MESSAGE, "Alarm Title");
ArrayList<Integer> alarmDays = new ArrayList<>();
alarmDays.add(Calendar.SATURDAY);
alarmDays.add(Calendar.SUNDAY);
alarmDays.add(Calendar.MONDAY);
alarmDays.add(Calendar.TUESDAY);
alarmDays.add(Calendar.WEDNESDAY);
alarmDays.add(Calendar.THURSDAY);
alarmDays.add(Calendar.FRIDAY);
i.putExtra(AlarmClock.EXTRA_DAYS, alarmDays);
i.putExtra(AlarmClock.EXTRA_VIBRATE, true);
i.putExtra(AlarmClock.EXTRA_HOUR, 10);
i.putExtra(AlarmClock.EXTRA_MINUTES, 21);
startActivity(i);
Permission:
允许:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
回答by pjv
@Mayra is right. That is the way. However the native clock application does not have a registered intent and is not using an intent filter. I don't think there is a native application, other than the system preferences, that opens it.
@Mayra 是对的。这就是方法。然而,本机时钟应用程序没有注册意图,也没有使用意图过滤器。我认为除了系统首选项之外,没有其他本机应用程序可以打开它。
Also the native clock application is not available in the same form, or not even available at all, on different phones. This is even beyond the SDK versioning. So there really is no (good) way for you to switch on either.
此外,本地时钟应用程序在不同的手机上无法以相同的形式使用,甚至根本不可用。这甚至超出了 SDK 版本控制。因此,您确实没有(好的)方法可以开启。
Your best way out is to submit a patch on source.android.com that gives the clock application an intent filter and then register the intent on http://www.openintents.org/en/intentstable. At least then your app works on a subset of devices and fails cleanly on others (saying no such application can be found to handle the intent).
最好的方法是在 source.android.com 上提交一个补丁,为时钟应用程序提供一个意图过滤器,然后在http://www.openintents.org/en/intentstable上注册意图。至少你的应用程序可以在一部分设备上运行,而在其他设备上完全失败(说找不到这样的应用程序来处理意图)。
回答by iBog
Small addition for frussoanswer. To launch Alarm Clock on Google Galaxy Nexus phones:
frusso答案的小补充。在 Google Galaxy Nexus 手机上启动闹钟:
{"Standar Alarm Clock2", "com.google.android.deskclock", "com.android.deskclock.AlarmClock"},
in other cases will be launched Clock application instead of Alarm Clock
在其他情况下将启动时钟应用程序而不是闹钟
回答by Victor Laskin
You can add
你可以加
{ "Sony Ericsson Xperia Z", "com.sonyericsson.organizer", "com.sonyericsson.organizer.Organizer_WorldClock" }
to frusso answer to support new Xperia phones.
frusso 回答以支持新的 Xperia 手机。