我正在尝试在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());
答案 0 :(得分:1)
应用程序无法更改(非根)Android设备上的日期或时间。
来自AlarmManager.setTime()
的文档:
需要权限android.permission.SET_TIME。
请参阅http://developer.android.com/reference/android/app/AlarmManager.html
答案 1 :(得分:-1)
这可能有帮助 ------------------ Broad Cast Class ---------------------------- < / p>
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 Class中编写此代码------------------------- -
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);
注意 - 打开您的清单文件并单击选项&gt;添加&gt;收件人&gt;眉毛&gt; addclassname(此处为TimeReceiver)