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.
From api level 19 you can use this to show the alarm list on the default clock app:
To follow up to Mayra's answer, the class
android.provider.AlarmClock
has the properIntent
for this, but it is available only at API level 9 and above. (At least someone at Google listens to requests like this!)To launch the clock app from your widget the following code works:
You also need this permission:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
You can add
to frusso answer to support new Xperia phones.
Small addition for frusso answer. To launch Alarm Clock on Google Galaxy Nexus phones:
in other cases will be launched Clock application instead of Alarm Clock
This code works for my clock widget.
In this way, I can run a default clock manager.
@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.
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.
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).