意图发动对Android的时钟应用程序 [英] Intent to launch the clock application on android
问题描述
我现在面临一个问题,一个时钟控件我做。我希望用户能够接触到时钟并启动时钟应用程序在手机上。这是code:
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);
}
我作出了上述所以,当我接触到的时钟打开报警设置。但是,并不具有普遍性。 我发现,在机器人2.2不起作用。必须有比每一个Android手机的味道,我的世界创造一个if语句更好的解决方案。另外我不知道所有的包名。 任何人都知道如何克服这种请帮助。
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.
推荐答案
这code适合我的时钟小工具。
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.
这篇关于意图发动对Android的时钟应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!