전화에서이 metod 당신의 MainActivity :
private void setNotifyAlarm() {
long _alarm;
Calendar now = Calendar.getInstance();
Calendar wakeupcall = Calendar.getInstance();
wakeupcall.setTimeInMillis(System.currentTimeMillis());
wakeupcall.set(Calendar.HOUR_OF_DAY, 15);
wakeupcall.set(Calendar.MINUTE, 30);
if (wakeupcall.getTimeInMillis() <= now.getTimeInMillis())
_alarm=wakeupcall.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1);
else
_alarm=wakeupcall.getTimeInMillis();
al = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
notif= new Intent(this,NotifyService.class);
fintent = PendingIntent.getService(this,0,notif,0);
al.setRepeating(AlarmManager.RTC_WAKEUP,_alarm,AlarmManager.INTERVAL_DAY, fintent);
}
이 메서드 호출 서비스 15:30 매일 이제 서비스 클래스에서 :.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String time = sharedPrefs.getString("change_time", "");
String[] parts = time.split(":");
int chosenHour = Integer.parseInt(parts[0]);
int chosenMinute = Integer.parseInt(parts[1]);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, chosenHour);
cal.set(Calendar.MINUTE, chosenMinute);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Intent i = new Intent(this, SmsService.class);
PendingIntent pIntent = PendingIntent.getService(this, 0, i, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 86400000, pIntent);
return START_STICKY;
}
01 23,516,
이 당신의 매니페스트 파일에 ADD :
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<service android:name=".NotifyService" android:exported="true" android:enabled="true"/>
Service.StartService을()? 그리고 어디에 넣어야합니까? –
예, API입니다. "어디에서 그것을 넣을 까?" => 필요에 따라 예 : 서비스를 즉시 시작하려면'Application # onCreate()'에 넣거나, 어떤 이벤트/액션에 기반하여 트리거하고 싶다면, 이벤트/액션이 발생할 때 메소드를 호출하십시오. 그러나 이전에 말했듯이, 코드를 애플리케이션 내부가 아닌'Application # onCreate()'에 넣어야합니다. –