android隔几秒运行一次,【已解决】为何android中实现了Runnable的进程只运行了一次...

【问题】

折腾:

期间,遇到的问题是:

线程只运行了一次,没有重复运行。。

【解决过程】

1.参考:

去加一下对应的权限:@Override

public void run() {

// Moves the current Thread into the background

android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);

看看是否有效果。。。

还是只运行一次。

2.参考:

才明白:

run,就是task的主要的运行的函数:

所以,如果你不写while(1),while(true)之类的代码,

那当然只会执行一次了。。。。

3.所以去改为:@Override

public void run() {

// Moves the current Thread into the background

android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);

//0x0403 / 0x6001: FTDI FT232R UART

final int ft232rUartVid = 0x0403; //1027

final int ft232rUartPid = 0x6001; //24577

boolean bNotFoundUsb = true;

while(bNotFoundUsb)

{

//Toast.makeText(getApplicationContext(), "Begin check usb action", 1000).show();

gLogger.debug("Begin check usb action");

UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

HashMap deviceList = manager.getDeviceList();

Iterator deviceIterator = deviceList.values().iterator();

while(deviceIterator.hasNext()){

UsbDevice device = deviceIterator.next();

int usbVid = device.getVendorId();

int usbPid = device.getProductId();

if((usbVid == ft232rUartVid) && (usbPid ==ft232rUartPid) ){

//Toast.makeText(getApplicationContext(), "Found Usb device: FT232R UART", Toast.LENGTH_LONG).show();

//Toast.makeText(getApplicationContext(), "Now send message USB_ACTION_ATTACH to activity ", Toast.LENGTH_LONG).show();

gLogger.debug("Found Usb device: FT232R UART");

gLogger.debug("Now send message USB_ACTION_ATTACH to activit");

Message foundUsbDeviceAttachMsg =new Message();

foundUsbDeviceAttachMsg.what=usb_action.USB_ACTION_ATTACH.getAction();

mHandler.sendMessage(foundUsbDeviceAttachMsg);

bNotFoundUsb = false;

break;

}

else

{

//Toast.makeText(getApplicationContext(), "Found USB VID="+usbVid+" PID=" + usbPid, Toast.LENGTH_LONG).show();

gLogger.debug("Found USB VID="+usbVid+" PID=" + usbPid);

}

}//while(deviceIterator.hasNext()){

try {

int sleepTimeInMs = 200;

gLogger.debug("Sleep milliseconds: " + sleepTimeInMs);

Thread.sleep(sleepTimeInMs);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

//e.printStackTrace();

}

}//while(bNotFoundUsb)

}//public void run()

然后就可以正常,一直运行了。。。

【总结】

此处的thread的run,就类似于嵌入式系统中的main

如果你不写成死循环:

while(1)

while(true)

那么函数执行完了,则线程也就结束了。

当然就只执行一次了。

想要循环执行,则加上对应的

while(1)

while(true)

即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android平台上,可以使用Lua语言编写一个服务进程实现进程保活。具体实现步骤如下: 1. 在Android项目引入Lua解释器库,可以使用luava库或者tolua库。 2. 编写Lua脚本文件,该脚本文件会被Lua解释器解释执行,实现进程保活的操作。 3. 在Android应用启动一个服务进程,该进程将会加载Lua脚本文件并执行。 4. 在Lua脚本实现进程保活的逻辑,可以使用Java代码调用Android系统API实现进程保活。 下面是一个简单的示例: 1. 在build.gradle文件添加依赖项 ``` dependencies { implementation 'com.android.support:appcompat-v7:29.0.3' implementation 'com.naef.jnlua:jnlua-android:0.9.1' } ``` 2. 编写Lua脚本文件 ``` function keepAlive() while true do -- 实现进程保活的逻辑 os.execute("sleep 60") end end ``` 3. 在Android应用启动服务进程并加载Lua脚本 ``` public class LuaService extends Service { private LuaState luaState; @Override public void onCreate() { super.onCreate(); luaState = LuaStateFactory.newLuaState(); luaState.openLibs(); luaState.LdoFile("keep_alive.lua"); Thread thread = new Thread(new Runnable() { @Override public void run() { LuaFunction function = luaState.getFunction("keepAlive"); function.call(); } }); thread.start(); } @Override public IBinder onBind(Intent intent) { return null; } } ``` 4. 在AndroidManifest.xml文件注册服务 ``` <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication"> <application> <service android:name=".LuaService" /> </application> </manifest> ``` 这样就可以通过启动LuaService服务进程实现进程保活的功能了。需要注意的是,该示例只是一个简单的示例,实际应用需要根据具体情况进行逻辑实现

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值