每5秒和引导在启动机器人环路服务 [英] Android loop service every 5 seconds and boot on startup

查看:205
本文介绍了每5秒和引导在启动机器人环路服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code,我想这个循环每5秒。我曾尝试使用广播接收器和报警管理器,但没有成功。我希望我的服务,以循环5次,之后永久停止。这是第二个问题我张贴。请有人帮助我解决了code,我堆积在我的Mac 2天。任何帮助将AP preciated。

code循环每5秒

 文件密钥=新的文件(Environment.getExternalStorageDirectory()的newfile+的Math.random()+TXT);
尝试{
    key.createNewFile();
}赶上(IOException异常五){
    e.printStackTrace();
}

InfoReceiver.class服务

 公共类InfoReceiver延伸服务{    @覆盖
    公共无效的onCreate()
    {
        AlarmManager alarmMgr =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
        意向意图=新意图(这一点,MyBroadcastReceiver.class);
        的PendingIntent的PendingIntent = PendingIntent.getBroadcast(这一点,0,意向,0);
        日历时间= Calendar.getInstance();
        time.setTimeInMillis(System.currentTimeMillis的());
        time.add(Calendar.SECOND,5);
        alarmMgr.set(AlarmManager.RTC_WAKEUP,5000的PendingIntent);
    }
}

InfoManager.class服务

 公共类InfoManager延伸服务{
    @覆盖
    公共无效的onCreate(){
        文件密钥=新的文件(Environment.getExternalStorageDirectory()的newfile+的Math.random()+TXT);
        尝试{
            key.createNewFile();
        }赶上(IOException异常五){
            e.printStackTrace();
        }
    }
}

MyBroadcastreceiver.class

 公共类MyBroadcastReceiver扩展广播接收器{
    私人语境mContext;
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        意图startServiceIntent =新意图(背景下,InfoManager.class);
        context.startService(startServiceIntent);
    }
}

清单code

 <服务
    机器人:名字=。InfoReceiver
    机器人:启用=真正的>
< /服务>
<服务
    机器人:名字=。InfoManager
    机器人:启用=真正的>
    &所述;意图滤光器>
        <作用机器人:名字=。InfoManager/>
    &所述; /意图滤光器>
< /服务>
<接收
    机器人:名字=。MyBroadcastReceiver
    机器人:工艺=:远程>
    &所述;意图滤光器>
        <作用机器人:名字=android.intent.action.BOOT_COMPLETED/>
    &所述; /意图滤光器>
< /接收器>


解决方案

使用Handler.postDelayed(Runnable的R,长delayMillis)。你不需要定时器或报警经理做你想做的。保持简单。

I have the code below and I want to loop this every 5 seconds. I have tried to use broadcastreceiver and alarm manager but without success . I want my service to loop for 5 times and after that stop permanently .This is the second question I post . Please somebody help me to fix the code, I am stacked on my mac for 2 days. Any help will be appreciated.

Code to loop every 5 seconds

File key = new File(Environment.getExternalStorageDirectory(),"newfile"+Math.random()+".txt");
try {
    key.createNewFile();
} catch (IOException e) {
    e.printStackTrace();
}

InfoReceiver.class Service

public class InfoReceiver extends Service {

    @Override
    public void onCreate()
    {
        AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, MyBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
        Calendar time = Calendar.getInstance();
        time.setTimeInMillis(System.currentTimeMillis());
        time.add(Calendar.SECOND, 5);
        alarmMgr.set(AlarmManager.RTC_WAKEUP, 5000, pendingIntent);
    }
}

InfoManager.class service

public  class InfoManager extends Service {
    @Override
    public void onCreate() {
        File key = new File(Environment.getExternalStorageDirectory(),"newfile"+Math.random()+".txt");
        try {
            key.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

MyBroadcastreceiver.class

public class MyBroadcastReceiver extends BroadcastReceiver {
    private Context mContext;
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, InfoManager.class);
        context.startService(startServiceIntent);
    }
}

Manifest Code

<service
    android:name=".InfoReceiver"
    android:enabled="true" >
</service>
<service
    android:name=".InfoManager"
    android:enabled="true" >
    <intent-filter>
        <action android:name=".InfoManager" />
    </intent-filter>
</service>
<receiver
    android:name=".MyBroadcastReceiver"
    android:process=":remote" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

解决方案

Use Handler.postDelayed(Runnable r, long delayMillis). You don't need timers or the alarm manager to do what you want. Stay simple.

这篇关于每5秒和引导在启动机器人环路服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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