Intent to launch the clock application on android
Asked Answered
M

8

20

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.

Marabelle answered 28/8, 2010 at 13:45 Comment(1)
I am doing something similar HERE!! https://mcmap.net/q/584973/-android-alarm-clock-uiBalaton
O
42

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.

Oxygen answered 25/11, 2010 at 22:22 Comment(3)
In "Froyo Nexus Alarm Clock" line correct className would be: "com.android.deskclock.AlarmClock". In each other case intent takes user directly to setting alarms, but in this one it takes user to so-called deskClock, from where one additional click is required to land in same place as in other cases. I don't understand why my edit was rejected... Even second most useful answer mentions it (@iBog used different vendor name, but it's relevant only while logging).Avocet
@Oxygen In this we set alarm time but it can automatically shows the alarm with own functions. is there any way to find the time in hours and minutes?? plz help...Stannwood
Seems like this code doesn't support Sony Xperia Z1.Thedrick
S
18

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

Slip answered 4/3, 2011 at 15:54 Comment(0)
D
7

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);
Dispassionate answered 13/3, 2015 at 14:57 Comment(1)
works but remember to set <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>Neuro
M
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.

Menides answered 28/8, 2010 at 17:2 Comment(4)
Mayra, Could you provide the code for the implicit intent to launch the clock application please, so i could understand better.ThanksMarabelle
There probably isn't an implicit intent for that. You're not supposed to be launching external apps in android unless the app provides a broadcast receiverHendrik
If the clock app supported a particular action, you would do something like: Intent clockIntent = new Intent(); clockIntent.setAction(android.clock.Intent.CLOCK_ACTION); startActivity(clockIntent); However, as Falmarri pointed out, there probably isn't one for the clock, so there is no good way to do what you want.Menides
This answer is more of a comment to a previous answer. Basically, this answer tells you that there is no way of firing the clock app.Mccarthy
T
2

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" />
Therese answered 11/7, 2017 at 10:24 Comment(0)
S
1

@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).

Sevastopol answered 26/9, 2010 at 18:7 Comment(0)
T
1

Small addition for frusso answer. To launch Alarm Clock on Google Galaxy Nexus phones:

{"Standar Alarm Clock2", "com.google.android.deskclock", "com.android.deskclock.AlarmClock"},

in other cases will be launched Clock application instead of Alarm Clock

Tenon answered 26/8, 2012 at 14:3 Comment(2)
To find this answer I've been cruising through OASP for many hours. And once I found it, I returned here to post errata, but it's already there! Just my luck :). Thanks anyway!Avocet
This should be a comment (or an edition), not an answer.Covington
D
-1

You can add

{ "Sony Ericsson Xperia Z", "com.sonyericsson.organizer", "com.sonyericsson.organizer.Organizer_WorldClock" }

to frusso answer to support new Xperia phones.

Denys answered 23/11, 2013 at 21:15 Comment(1)
This should be a comment (or an edition), not an answer.Covington

© 2022 - 2024 — McMap. All rights reserved.