[英]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.