Android小部件如何才能从小时和分钟中获取时间?

时间:2013-04-26 09:46:12

标签: android widget alarmmanager

在我的应用程序中,我创建了小部件以模拟时钟格式显示时间。当点击小部件实际显示警报类时,为该小部件设置警报并且它也正常工作。

为此,我使用了这段代码:

当点击小部件时,我在更新中调用此类并从中获取警报类:

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

和报警功能方法:

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;
    }
}

但是我的要求来自这个闹钟功能,我怎样才能在几分钟内获得时间和时间。实际上该功能会自动显示闹钟,但我想知道时间。 有可能吗?帮帮我?

2 个答案:

答案 0 :(得分:4)

您如果需要闹钟时间,可以使用此功能。

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

然后将其转换为日期。

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

答案 1 :(得分:-1)

问题对我来说不是那么清楚。假设您需要从闹钟获取闹钟时间和小时数。

final String tag_alarm = "tag_alarm";
Uri uri = Uri.parse("content://com.android.alarmclock/alarm");
Cursor c = getContentResolver().query(uri, null, null, null, null);
Log.i(tag_alarm, "no of records are " + c.getCount());
Log.i(tag_alarm, "no of columns are " + c.getColumnCount());
if (c != null) {
    String names[] = c.getColumnNames();
    for (String temp : names) {
        System.out.println(temp);
    }
    if (c.moveToFirst()) {
        do {
            for (int j = 0; j < c.getColumnCount(); j++) {
                Log.i(tag_alarm, c.getColumnName(j)
                        + " which has value " + c.getString(j));
            }
        } while (c.moveToNext());
    }
}