getting method not found setTime(long) in AlarmManager when tried to set time and date in android system time

旧巷老猫 提交于 2019-12-24 18:13:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!