当前位置: 代码迷 >> Android >> android Service一直运行一个程序解决办法
  详细解决方案

android Service一直运行一个程序解决办法

热度:21   发布时间:2016-05-01 21:25:41.0
android Service一直运行一个程序
public int onStartCommand(Intent intent, int flags, int startId) {


while (1>0) {
try {
Thread.sleep(4000);
Log.i("log", "onstart");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


return super.onStartCommand(intent, flags, startId);
}

请问我想让一个后台servie 一值运行这个 那我在 这个方法里写这个 但是他报错。。。说return super.onStartCommand(intent, flags, startId);
代码一直不会运行 ,我像问我如何 实现一致运行 这个程序的目的呢

------解决方案--------------------
楼主是这个意思。。。?
Java code
public int onStartCommand(Intent intent, int flags, int startId) {        if(true){ //加个这个,能编过。。。            while (1>0) {                try {                    Thread.sleep(4000);                    Log.i("log", "onstart");                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }        return super.onStartCommand(intent, flags, startId);    }
------解决方案--------------------
主线程回调必须5秒内返回,另建一个线程处理这种东西。

另外Android上sleep貌似靠不住,长时间运行恐怕得用Alarm
------解决方案--------------------
service也不能做超时的操作。。。你这个可以用定时器干。timer和timertask
------解决方案--------------------
探讨

是这个意思。。。有什么问题吗
难道要新线程? 怎么用新线程呀我小白白

------解决方案--------------------
我的demo是BroadcastReceiver+Service,不用电源管理,但是实现了锁屏时依然运行的功能,如果用电源管理禁止锁屏的话,你的项目一运行,半天手机就没电了
------解决方案--------------------
探讨
我的demo是BroadcastReceiver+Service,不用电源管理,但是实现了锁屏时依然运行的功能,如果用电源管理禁止锁屏的话,你的项目一运行,半天手机就没电了

------解决方案--------------------
你的邮箱是:[email protected]
------解决方案--------------------
是要开线程去做的。service里不能直接放while(true)
------解决方案--------------------
探讨

引用:
我的demo是BroadcastReceiver+Service,不用电源管理,但是实现了锁屏时依然运行的功能,如果用电源管理禁止锁屏的话,你的项目一运行,半天手机就没电了
求demo,[email protected]
  相关解决方案