2016-02-26 62 views
1

後運行定時器我在我的應用程序下面的計時器:繼續殺戮應用

public class MainScreen extends ApplicationActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_screen); 

     Timer timer = new Timer(); 
     timer.scheduleAtFixedRate(new ScheduleSomething(), 1000, 1000); 
    } 

    public static class ScheduleSomething extends TimerTask { 
     @Override 
     public void run() { 
      System.out.println("This is a test!"); 
     } 
    } 
} 

每個第二消息!「這是一個測試」顯示,但是當我關閉應用程序時,它也會停止計時器。 當我關閉應用程序時,有沒有辦法讓這個定時器運行?

我想:

public void onStop(Bundle savedInstanceState) { 
    super.onStop(savedInstanceState); 

    Timer timer = new Timer(); 
    timer.scheduleAtFixedRate(new ScheduleSomething(), 1000, 1000); 
} 

public void onDestroy(Bundle savedInstanceState) { 
    super.onDestroy(savedInstanceState); 

    Timer timer = new Timer(); 
    timer.scheduleAtFixedRate(new ScheduleSomething(), 1000, 1000); 
} 

但它不工作...

+0

使用的服務。你不應該在一個活動中做到這一點 –

+0

從來沒有使用過服務,對不起。如果我使用服務,我仍然可以從應用程序中調用方法或類。 – Ravers

+0

在發佈到stackoverflow之前請google。 http://developer.android.com/guide/components/services.html –

回答

1

使用廣播接收器殺死應用每分鐘後繼續運行計時器:

public class TimerReceiverSyncInterval extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     scheduleAlarms(context); 
     context.startService(new Intent(context, NotificationServiceSyncInterval.class)); 
     Log.d("TAG", "Sync OnReceive"); 
    } 

    public static void scheduleAlarms(Context paramContext) { 
     Calendar calendar = Calendar.getInstance(); 
     AlarmManager localAlarmManager = (AlarmManager) paramContext.getSystemService(Context.ALARM_SERVICE); 
     PendingIntent localPendingIntent = PendingIntent.getService(paramContext, 0, 
      new Intent(paramContext, NotificationServiceSyncInterval.class), PendingIntent.FLAG_UPDATE_CURRENT); 

     localAlarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 
       (1 * 60000), localPendingIntent); 
    } 
} 

並且在下面的類中,根據TimerReceiverSyncInterval類中每分鐘調用的onHandleIntent方法執行所需操作:

public class NotificationServiceSyncInterval extends IntentService { 

    public NotificationServiceSyncInterval() { 
     super("Tracker"); 
    } 

    public NotificationServiceSyncInterval(String paramString) { 
     super(paramString); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     //ToDo: put what you want to do here 
     Log.d("TAG", "Handler call"); 
    } 
} 

請在清單文件中的條目:

<receiver 
    android:name="com.yourpackage.TimerReceiverSyncInterval" 
    android:enabled="true" > 
    <intent-filter android:priority="999" > 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     <action android:name="android.intent.action.QUICKBOOT_POWERON" /> 
    </intent-filter> 
</receiver> 

<service android:name="com.yourpackage.NotificationServiceSyncInterval" /> 

最後從註冊MainActivity這樣的廣播接收器:

TimerReceiverSyncInterval.scheduleAlarms(this);