Intent to launch the clock application on android

Solution 1:

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.

Solution 2:

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!)

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"/>

Solution 3:

From api level 19 you can use this to show the alarm list on the default clock app:

    Intent mClockIntent = new Intent(AlarmClock.ACTION_SHOW_ALARMS);
    mClockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(mClockIntent);

Solution 4:

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.

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 filters for 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.

Solution 5:

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" />