1

I have created an analog clock widget for Android 4. Now I want to mimic the same behavior as the native analog clock widget, which is to show the alarm settings window on tapping the clock.

After searching I found this topic: Intent to launch the clock application on android

Which launches the desktop clock and not the alarm settings. I think I have to do something with android.provider.AlarmClock but I can not get it to launch the alarms window. Does anyone know how to achieve this?

Community
  • 1
  • 1
Bas van Dijk
  • 9,933
  • 10
  • 55
  • 91

1 Answers1

0

In my AppWidgetProvider class, the following works for me on 4.1.2 (provided you only want it to work on 4.x - otherwise you will need conditional logic to select the right package and class names for the appropriate version, like in the post you linked to):

public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    PendingIntent pendingIntent;
    if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.main);
        pendingIntent = PendingIntent.getActivity(context, 0,
                getAlarmPackage(context), 0);
        views.setOnClickPendingIntent(R.id.analogClock1, pendingIntent);

        AppWidgetManager
                .getInstance(context)
                .updateAppWidget(
                        intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS),
                        views);
    }
}

private Intent getAlarmPackage(Context context) {
    PackageManager manager = context.getPackageManager();
    Intent intent = new Intent(Intent.ACTION_MAIN)
            .addCategory(Intent.CATEGORY_LAUNCHER);

    try {
        ComponentName c = new ComponentName("com.google.android.deskclock",
                "com.android.deskclock.AlarmClock");
        manager.getActivityInfo(c, PackageManager.GET_META_DATA);
        intent.setComponent(c);
    } catch (NameNotFoundException nf) {
        Log.d("XXXX", "Caught name not found exception!");
    }

    return intent;
}

Basically, using com.android.deskclock.AlarmClock (with com.google.android.deskclock) instead of com.android.deskclock.DeskClock produces the Alarms list instead of the Alarm clock screen.

Iskar Jarak
  • 5,136
  • 4
  • 38
  • 60