创建于Android的一个预定服务 [英] Create a Scheduled service in android

查看:130
本文介绍了创建于Android的一个预定服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Android中使用Java创建一个日程安排服务。我曾尝试C $ CS一些$,但毕竟时间打造它不运行应用程序。我的逻辑很简单,我想打一个服务检查,在蓝牙文件夹路径中的文件是否存在,如果该文件是存在的,所以这项服务将运行其他应用程序,我需要这个与运行每2分钟一个时间表。

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.

到现在为止这是伟大的,但现在我有一个错误的方法startActivity(意向)是未定义的类型MyTimerTask 。我曾尝试code ...

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...

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.

推荐答案

有两种方法可以实现您的要求。

There are two ways to achieve your requirement.

  • 的TimerTask
  • 报警管理类

  • TimerTask
  • Alarm Manager Class

的TimerTask具有重复给定的特定时间间隔中的活性的方法。看看下面的示例的例子。

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 做同样的事情,比如的TimerTask 但它占用较少的内存来执行任务。

AlarmManager does same thing like TimerTask 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();
        }
   }
}

AlarmClass报警,

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

这篇关于创建于Android的一个预定服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆