杀死应用程序后继续运行计时器

杀死应用程序后继续运行计时器

本文介绍了杀死应用程序后继续运行计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following timer in my application:

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

每秒都会出现一条消息这是一个测试!"显示,但是当我关闭应用程序时,它也会停止计时器.当我关闭应用程序时,有没有办法让这个计时器保持运行?

Each second the message "This is a test!" is displayed, but when I close the application it stops the timer also.Is there any way to keep this timer running when I close the application?

我试图:

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

但它不起作用...

推荐答案

每分钟杀死应用程序后,使用广播接收器保持运行计时器:

Use a broadcast receiver to keep running the timer after killing application every minute:

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 方法中执行您想要的任何操作:

And in the class below, do whatever you want in onHandleIntent method that is called every minute from TimerReceiverSyncInterval class:

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

在清单文件中创建一个条目:

Make a entry in the manifest file:

<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 注册广播接收器:

And finally register the broadcast receiver from MainActivity like this:

TimerReceiverSyncInterval.scheduleAlarms(this);

这篇关于杀死应用程序后继续运行计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:03