2015-02-05 20 views
-1

我不知道從哪裏開始,但我已經爲我的應用做了GUI,並且需要根據我存儲在SharedPreferences中的變量這是基於一週的時間和日期,我只想知道一些基於日期和時間可以使事情發生在背景中的最佳方式,謝謝。需要使事情發生在基於星期和時間的背景

+0

您可能想查看AlarmManager。 –

回答

1
  1. 要在後臺運行的東西,你需要一個Service

    public class MyService extends Service { 
    
        @Override 
        public int onStartCommand(final Intent intent, final int flags, 
             final int startId) { 
         // parse the intent and run your things 
         return START_NOT_STICKY; 
        } 
    } 
    
  2. 要的東西在特定的時間自動,你需要AlarmManager

    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context 
         .ALARM_SERVICE); 
    Intent alarmIntent = new Intent(context, BlockchainService.class); 
    // customize your intent 
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, 
         alarmIntent, 0); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, your_specific_time, pendingIntent); 
    
  3. 啓動服務,並防止睡眠

    相關ans wer AlarmManager not working in sleep mode

+0

即使包含阿拉姆馬槽的應用程序未打開,AlarmManager也會啓動服務嗎? – endlesschaos

+0

@endlesschaos是 – songchenwen

+0

還有一個問題,即使設備被鎖定,我的報警管理器是否會啓動服務? – endlesschaos

相關問題