java 在android中创建一个定时服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12188434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a Scheduled service in android
提问by El Sa7eR
I need to create a schedule service in android with java. I have tried some codes , but all time after build the application it doesn't run. My logic is simple , I want to make a service to check the existence of a file in the bluetooth folder path, If this file is there , so this service will run another application , I need this with a schedule which run every 2 minutes.
我需要在 android 中用 java 创建一个调度服务。我已经尝试了一些代码,但在构建应用程序后一直没有运行。我的逻辑很简单,我想做一个服务来检查蓝牙文件夹路径中的文件是否存在,如果这个文件在那里,那么这个服务将运行另一个应用程序,我需要这个计划,每 2 分钟运行一次。
Until now that's great, but now I have an error The method startActivity(Intent) is undefined for the type MyTimerTask
. I have tried this code...
到目前为止,这很好,但现在我有一个错误The method startActivity(Intent) is undefined for the type MyTimerTask
。我试过这个代码...
public class MyTimerTask extends TimerTask {
java.io.File file = new java.io.File("/mnt/sdcard/Bluetooth/1.txt");
public void run(){
if (file.exists()) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity"));
startActivity(intent);
}
}
}
Could someone please help me with this.
有人可以帮我解决这个问题。
回答by Lucifer
There are two ways to achieve your requirement.
有两种方法可以实现您的要求。
- TimerTask
Alarm Manager Class
TimerTask has a method that repeats the activity on the given particular time interval. look at the following sample example.
Timer timer; MyTimerTask timerTask; timer = new Timer(); timerTask = new MyTimerTask(); timer.schedule ( timerTask, startingInterval, repeatingInterval ); private class MyTimerTask extends TimerTask { public void run() { ... // Repetitive Activity goes here } }
AlarmManager
does same thing likeTimerTask
but as it occupies lesser memory to execute tasks.public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { try { Bundle bundle = intent.getExtras(); String message = bundle.getString("alarm_message"); Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); } catch (Exception e) { Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } }
- 定时器任务
报警管理器类
TimerTask 有一个方法可以在给定的特定时间间隔内重复活动。请看以下示例。
Timer timer; MyTimerTask timerTask; timer = new Timer(); timerTask = new MyTimerTask(); timer.schedule ( timerTask, startingInterval, repeatingInterval ); private class MyTimerTask extends TimerTask { public void run() { ... // Repetitive Activity goes here } }
AlarmManager
做同样的事情,TimerTask
但因为它占用较少的内存来执行任务。public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { try { Bundle bundle = intent.getExtras(); String message = bundle.getString("alarm_message"); Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); } catch (Exception e) { Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } }
AlarmClass,
报警类,
private static Intent alarmIntent = null;
private static PendingIntent pendingIntent = null;
private static AlarmManager alarmManager = null;
// OnCreate()
alarmIntent = new Intent ( null, AlarmReceiver.class );
pendingIntent = PendingIntent.getBroadcast( this.getApplicationContext(), 234324243, alarmIntent, 0 );
alarmManager = ( AlarmManager ) getSystemService( ALARM_SERVICE );
alarmManager.setRepeating( AlarmManager.RTC_WAKEUP, ( uploadInterval * 1000 ),( uploadInterval * 1000 ), pendingIntent );