4

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

为此,我使用了以下代码:

当单击小部件时,我将其称为更新并从中获取警报类:

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

但我的要求是从这个闹钟功能中我如何才能得到小时和分钟的时间。实际上该功能会自动显示警报,但我想知道时间。有没有可能?帮我?

4

2 回答 2

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);
于 2013-05-05T17:04:10.260 回答
-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());
    }
}
于 2013-05-06T04:28:39.530 回答