问题
I'm trying to set the system time and date on an Android device.
I thought the following code would work, but it produces the error 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());
回答1:
It is not possible for an application to change the date or time on an (non-rooted) Android device.
From the documentation for AlarmManager.setTime()
:
Requires the permission android.permission.SET_TIME.
See http://developer.android.com/reference/android/app/AlarmManager.html
回答2:
This may Helpful ------------------Broad Cast Class----------------------------
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();
}
}
----------------Write this code in 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);
Note- Opent your manifest file and click on option>Add>Reciever>brows>addclassname(here is TimeReceiver)
来源:https://stackoverflow.com/questions/12104213/getting-method-not-found-settimelong-in-alarmmanager-when-tried-to-set-time-an