이렇게하려면 services
과 AlarmManager
을 쓸 수 있습니다. 간단히 서비스에서 응용 프로그램을 등록하고 서비스의 onStart()
방법으로 수행 할 서버 측이나 다른 작업의 코드를 시작 alarmMangaer.setRepeat()
메소드를 호출
public class MyService extends Service{
Calendar cur_cal = Calendar.getInstance();
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Intent intent = new Intent(this, MyService.class);
PendingIntent pintent = PendingIntent.getService(getApplicationContext(),
0, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
cur_cal.setTimeInMillis(System.currentTimeMillis());
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cur_cal.getTimeInMillis(),
60 * 1000*3, pintent);
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
// your code for background process
}
}
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service
android:name="com.yourpackage.MyService"
android:enabled="true" />
<receiver android:name=".BootCompletedIntentReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
의 AndroidManifest.xml이 추가 편집 : BootCompletedIntentReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootCompletedIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context, MyService.class);
context.startService(pushIntent);
}
}
}
사용 서비스 : HT tp : //developer.android.com/guide/components/services.html –
인 텐트 서비스 또는 서비스 사용 ... http://developer.android.com/reference/android/app/IntentService.html http : // developer .android.com/reference/android/app/Service.html –