在尝试在android系统时间设置时间和日期时,在AlarmManager中找不到setTime(long)方法

时间:2012-08-24 06:29:00

标签: android alarmmanager

我正在尝试在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());

2 个答案:

答案 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)