Search code examples
androidwidgetalarmmanager

Android widget how can get time in hour and minute from this?


In my App, I created widgets to show the time in analog clock format. When click on widget actually showing alarm class to set the alarm for that widget and its also working fine.

For that I used this code:

when click on widget I call this one in update and getting alarm class from this:

PendingIntent pendingIntent = PendingIntent.getActivity(
    context, 0, getAlarmPackage(context), 0);
rv_ana.setOnClickPendingIntent(R.id.clock, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, rv_ana);

and alarm function method:

public Intent getAlarmPackage(Context context) {
    PackageManager packageManager = context.getPackageManager();
    Intent AlarmClockIntent = new Intent(Intent.ACTION_MAIN)
            .addCategory(Intent.CATEGORY_LAUNCHER);

    String clockImpls[][] = {
            { "Standard Alarm", "com.android.alarmclock",
                    "com.android.alarmclock.AlarmClock" },
            { "HTC Alarm ClockDT", "com.htc.android.worldclock",
                    "com.htc.android.worldclock.WorldClockTabControl" },
            { "Standard Alarm ClockDT", "com.android.deskclock",
                    "com.android.deskclock.AlarmClock" },
            { "Froyo Nexus Alarm ClockDT", "com.google.android.deskclock",
                    "com.android.deskclock.DeskClock" },
            { "Moto Blur Alarm ClockDT", "com.motorola.blur.alarmclock",
                    "com.motorola.blur.alarmclock.AlarmClock" },
            { "Samsung Galaxy S", "com.sec.android.app.clockpackage",
                    "com.sec.android.app.clockpackage.ClockPackage" } 
    };

    boolean foundClockImpl = false;

    for (int i = 0; i < clockImpls.length; i++) {
        String packageName = clockImpls[i][1];
        String className = clockImpls[i][2];
        try {
            ComponentName cn = new ComponentName(packageName, className);
            packageManager.getActivityInfo(
                        cn, PackageManager.GET_META_DATA);
            AlarmClockIntent.setComponent(cn);    
            foundClockImpl = true;
        } catch (NameNotFoundException nf) {
        }
    }
    if (foundClockImpl) {
        return AlarmClockIntent;
    } else {
        return null;
    }
}

But my requirement is from this alarm function how can I get time in hours and time in minutes. Actually that function automatically shows the alarm but I want to know the time. Is it possible or not? Help me?


Solution

  • You If you want the alarm time you can use this.

    String nextAlarm = Settings.System.getString(getContentResolver(),
    Settings.System.NEXT_ALARM_FORMATTED);
    

    then convert it to Date.

    String string = nextAlarm;//Tue 8:30"
    Date date = new SimpleDateFormat("EEE HH:mm", Locale.ENGLISH).parse(string);
    System.out.println(date);