[英]Android - executing different functions with multiple threads
我对Android编程很陌生,但是在其他语言方面也有一些经验。 我想创建一个原理如此的APP。
APP是一个进程,每隔10秒询问我的Web /数据库服务器是否有事件要执行。
Web /数据库服务器用事件ID甚至函数名称回答。
我的问题是:
如何使用返回值执行函数? 例如
InputStream in = response.getEntity().getContent(); //Get the data in the entity
public in(void) { // execute a function which got´s the same name as the variable "in" }
结果应该是:一个线程每10秒询问我的Web- /数据库服务器是否有事件。 该事件在线程内执行,并且正在并行工作(同时没有崩溃或卡住)。
这些线程的示例代码将不胜感激。
我的代码至今:
公共类服务扩展了Service {private static final String TAG =“ MyService”;
@Override public IBinder onBind(Intent intent) { return null; } public void onDestroy() { Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); Log.d(TAG, "onDestroy"); } @Override public int onStartCommand(Intent intent, int flags, int startid) { Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); Log.d(TAG, "onStart"); Thread thread = new Thread(new Runnable() { @Override public void run() { new Timer().scheduleAtFixedRate(new TimerTask() { @Override public void run() { System.out.println("Send GO!"); Device dev = new Device(); dev.getDeviceId(getBaseContext()); dev.getEmail(getBaseContext()); dev.sendDeviceItems(); } }, 0, 10000);//put here time 1000 milliseconds=1 second } }); thread.start(); return Service.START_STICKY; } }
是在进程中限制为2个线程,还是每次为要执行的功能创建新线程时都可以打开? 也许因为其他功能仍在运行?
如何使用返回值执行函数? 例如
InputStream in = response.getEntity()。getContent(); //获取实体中的数据
public in(void){//执行一个与变量“ in”同名的函数}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.