java - 终止应用程序后继续运行计时器

标签 java android timer

我的应用程序中有以下计时器:

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);
}

但是这不起作用...

最佳答案

每分钟终止应用程序后,使用广播接收器继续运行计时器:

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");
   }
}

在 list 文件中创建一个条目:

<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);

关于java - 终止应用程序后继续运行计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35653908/

相关文章:

java - 使用 @ElementCollection : org. hibernate.MappingException : Could not determine type for: java. util.Set 时出错,在表中:对于列

java - Switch 语句不显示任何结果

android - 在 Fragment 中显示确认对话框

flutter - 如何在 flutter 中实现超时按钮

java - 在 Recycler View 的情况下,等效的 listview.setSelection 是什么

Java - Arraylist/Hash 映射 问题

java - Spring Rabbit消息消费,payload格式错误

java - android.support.v7.widget.AppCompatEditText错误

ios - 如何在给定的时间内显示 NSArray

javascript - 带有 setTimeout 的 JS 计时器得到 "Uncaught SyntaxError: Unexpected token ("