0

我正在尝试在 Android 设备上设置系统时间和日期。

我认为下面的代码可以工作,但它会产生错误method not found: setTime(long)

Calendar c = Calendar.getInstance();
c.set(2010, 1, 1, 12, 00, 00);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setTime(c.getTimeInMillis());
4

2 回答 2

1

应用程序无法更改(非根)Android 设备上的日期或时间。

从文档中AlarmManager.setTime()

需要权限 android.permission.SET_TIME。

请参阅http://developer.android.com/reference/android/app/AlarmManager.html

于 2012-08-24T07:52:35.660 回答
-1

这可能会有所帮助-------广泛的演员班------------------------- --

public class TimeReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context ctx, Intent intent) {

    Toast.makeText(ctx, "It's time to WAKE UP!!!!", Toast.LENGTH_SHORT).show();
}

}

----------------在Activity类中写这段代码-------------------------

PendingIntent sender = null;
    Intent intent = null;
    Calendar systemtime = Calendar.getInstance();
    systemtime.setTime(new Date());

    Calendar timeToTriggerAlarm = Calendar.getInstance();
    timeToTriggerAlarm.set(Calendar.HOUR_OF_DAY, systemtime.get(Calendar.HOUR_OF_DAY));
    timeToTriggerAlarm.set(Calendar.MINUTE, systemtime.get(Calendar.MINUTE));
    timeToTriggerAlarm.set(Calendar.SECOND, 00);

    intent = new Intent(this,TimeReceiver.class);

    sender = PendingIntent.getBroadcast(this, 0, intent,PendingIntent.FLAG_ONE_SHOT);

    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, timeToTriggerAlarm.getTimeInMillis(), sender);

注意-打开您的清单文件并单击选项>添加>接收器>浏览>添加类名(这里是 TimeReceiver)

于 2012-08-24T16:31:05.267 回答