0

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());
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778

2 Answers2

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

David Wasser
  • 93,459
  • 16
  • 209
  • 274
-1

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)

Sher Ali
  • 5,513
  • 2
  • 27
  • 29
  • This has nothing to do with the question. The poster wants to know how he can change the date and time on the phone programatically. Not how to wake up his phone at a particular time. – David Wasser Aug 25 '12 at 07:53