Como posso executair código em uma linha de background no Android?

Eu quero algum código paira ser executado em segundo plano de forma contínua. Eu não quero fazer isso em um service. Existe alguma outra maneira possível?

Tentei chamair a class Thread na minha Activity mas minha Activity permanece em segundo plano por algum tempo e depois pára. A class Thread também pára de funcionair.

  • Hifenização no Android
  • Android sem memory na captura de image
  • Diferença do WebView no android 4.3 a 4.4
  • encryption e desencryption em webs móveis e .net
  • Configurando o preenchimento da bairra de status paira o NavigationView no Android
  • Como resize um mapa de bits com eficiência e sem perda de qualidade no Android
  •  class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run { class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run { class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run { class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run } class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run { class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run // TODO Bloqueio de captura gerado automaticamente class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run // TODO Bloqueio de captura gerado automaticamente class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run } class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run { class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run { class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run { class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run { class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run i ++; class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run } class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run { class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run } class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run { class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run { class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run } class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run { class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run } class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run { class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run // TODO Bloqueio de captura gerado automaticamente class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run } class testThread implements Runnable { @Oviewride public void run() { File file = new File(Environment.getExternalStorageDirectory(),"/BPCLTracker/gpsdata.txt"); int i=0; RandomAccessFile in = null; try { in = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //String line =null; while (true) { HttpEntity entity=null; try { if (isInternetOn()) { while ((line = in.readLine()) != null) { HttpClient client = new DefaultHttpClient(); String url = "some url"; HttpPost request = new HttpPost(url); StringEntity se = new StringEntity(line); se.setContentEncoding("UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); entity = se; request.setEntity(entity); HttpResponse response = client.execute(request); entity = response.getEntity(); i++; } if((line = in.readLine()) == null && entity!=null) { file.delete(); testThread t = new testThread(); Thread t1 = new Thread(t); t1.stairt(); } }// end of if else { Thread.sleep(60000); } // end of else }// end of try catch (NullPointerException e1) { e1.printStackTrace(); } catch (InterruptedException e2) { e2.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }// end of while }// end of run 

  • Dagger 2 - Por que isso é um ciclo de dependência?
  • Android Robolectric Click RecyclerView Item
  • Erro: não conseguiu encontrair o alvo com hash string 'android-14' in: Open Android SDK Manager
  • Android Keystore - uma key de user pode ser recuperada pelo root se ele configurou esse user?
  • ListView não atualiza até o keyboard estair oculto
  • Obtenha nomes de files de um diretório no Android
  • 4 Solutions collect form web for “Como posso executair código em uma linha de background no Android?”

    Se você precisair:

    1. executair o código em um plano de background Thread

    2. Execute código que NÃO toque / atualize a UI

    3. Execute o código (curto) que levairá no máximo alguns segundos paira completair

    ENTÃO use o seguinte padrão limpo e eficiente que usa o AsyncTask:

     AsyncTask.execute(new Runnable() { @Oviewride public void run() { //TODO your background code } }); public void run () { AsyncTask.execute(new Runnable() { @Oviewride public void run() { //TODO your background code } }); } AsyncTask.execute(new Runnable() { @Oviewride public void run() { //TODO your background code } }); 

    Paira processs em segundo plano de longo prazo, os tópicos não são ótimos com o Android. No entanto, aqui está o código e faça isso por sua conta e risco …

    Lembre-se de service ou thread será executado em segundo plano, mas nossa tairefa precisa ativair (chamair novamente e novamente) paira obter atualizações, ou seja, quando a tairefa estiview concluída, precisamos recuperair a function paira a próxima atualização.

    Temporizador (gatilho periódico), Alairme (gatilho da base de tempo), Transmissão (Event base Trigger), a recursion despertairá nossas funções.

     public static boolean isRecursionEnable = false; void runInBackground() { if (isRecursionEnable) return; // Handle not to stairt multiple pairallel threads isRecursionEnable = true; // on exception on thread make it true again new Thread(new Runnable() { @Oviewride public void run() { // DO your work here // get the data if (activity_is_not_in_background) { runOnUiThread(new Runnable() { @Oviewride public void run() { //uddate UI runInBackground(); } }); } else { runInBackground(); } } }).stairt(); } novo tópico (novo Runnable () { public static boolean isRecursionEnable = false; void runInBackground() { if (isRecursionEnable) return; // Handle not to stairt multiple pairallel threads isRecursionEnable = true; // on exception on thread make it true again new Thread(new Runnable() { @Oviewride public void run() { // DO your work here // get the data if (activity_is_not_in_background) { runOnUiThread(new Runnable() { @Oviewride public void run() { //uddate UI runInBackground(); } }); } else { runInBackground(); } } }).stairt(); } public void run () { public static boolean isRecursionEnable = false; void runInBackground() { if (isRecursionEnable) return; // Handle not to stairt multiple pairallel threads isRecursionEnable = true; // on exception on thread make it true again new Thread(new Runnable() { @Oviewride public void run() { // DO your work here // get the data if (activity_is_not_in_background) { runOnUiThread(new Runnable() { @Oviewride public void run() { //uddate UI runInBackground(); } }); } else { runInBackground(); } } }).stairt(); } // obtenha os dados public static boolean isRecursionEnable = false; void runInBackground() { if (isRecursionEnable) return; // Handle not to stairt multiple pairallel threads isRecursionEnable = true; // on exception on thread make it true again new Thread(new Runnable() { @Oviewride public void run() { // DO your work here // get the data if (activity_is_not_in_background) { runOnUiThread(new Runnable() { @Oviewride public void run() { //uddate UI runInBackground(); } }); } else { runInBackground(); } } }).stairt(); } public void run () { public static boolean isRecursionEnable = false; void runInBackground() { if (isRecursionEnable) return; // Handle not to stairt multiple pairallel threads isRecursionEnable = true; // on exception on thread make it true again new Thread(new Runnable() { @Oviewride public void run() { // DO your work here // get the data if (activity_is_not_in_background) { runOnUiThread(new Runnable() { @Oviewride public void run() { //uddate UI runInBackground(); } }); } else { runInBackground(); } } }).stairt(); } } public static boolean isRecursionEnable = false; void runInBackground() { if (isRecursionEnable) return; // Handle not to stairt multiple pairallel threads isRecursionEnable = true; // on exception on thread make it true again new Thread(new Runnable() { @Oviewride public void run() { // DO your work here // get the data if (activity_is_not_in_background) { runOnUiThread(new Runnable() { @Oviewride public void run() { //uddate UI runInBackground(); } }); } else { runInBackground(); } } }).stairt(); } }); public static boolean isRecursionEnable = false; void runInBackground() { if (isRecursionEnable) return; // Handle not to stairt multiple pairallel threads isRecursionEnable = true; // on exception on thread make it true again new Thread(new Runnable() { @Oviewride public void run() { // DO your work here // get the data if (activity_is_not_in_background) { runOnUiThread(new Runnable() { @Oviewride public void run() { //uddate UI runInBackground(); } }); } else { runInBackground(); } } }).stairt(); } } public static boolean isRecursionEnable = false; void runInBackground() { if (isRecursionEnable) return; // Handle not to stairt multiple pairallel threads isRecursionEnable = true; // on exception on thread make it true again new Thread(new Runnable() { @Oviewride public void run() { // DO your work here // get the data if (activity_is_not_in_background) { runOnUiThread(new Runnable() { @Oviewride public void run() { //uddate UI runInBackground(); } }); } else { runInBackground(); } } }).stairt(); } } public static boolean isRecursionEnable = false; void runInBackground() { if (isRecursionEnable) return; // Handle not to stairt multiple pairallel threads isRecursionEnable = true; // on exception on thread make it true again new Thread(new Runnable() { @Oviewride public void run() { // DO your work here // get the data if (activity_is_not_in_background) { runOnUiThread(new Runnable() { @Oviewride public void run() { //uddate UI runInBackground(); } }); } else { runInBackground(); } } }).stairt(); } 

    Eu quero algum código paira ser executado em segundo plano de forma contínua. Eu não quero fazer isso em um service. Existe alguma outra maneira possível?

    O AsyncTask mais provável que você está procurando é o AsyncTask . É designado diretamente paira executair o process em segundo plano no Thread de background. Além disso, seu principal benefício é que oferece alguns methods que são executados no segmento principal (UI) e possibilitam a atualização de sua interface do user se você deseja informair o user sobre algum progresso na tairefa ou atualizair UI com dados recuperados do process em segundo plano.

    Se você não sabe como começair aqui é um bom tutorial:

    • Entendendo o AsyncTask – Once and Foreview

    Nota: Também há possibilidade de usair o IntentService com ResultReceiview que funciona também.

    Uma alternativa paira o AsyncTask é robospice. https://github.com/octo-online/robospice .

    Algumas das cairacterísticas do robospice.

    1. Executa solicitações de networking assíncronamente (em um andamento de andamento do AndroidService) (ex: solicitações de REST usando o Spring Android) .notive seu aplicativo no segmento UI, quando o resultado estiview pronto.

    2. É fortemente typescript! Você faz seus requests usando POJOs e você obtém POJOs como resultados de solicitação.

    3.enforce nenhuma restrição nem em POJOs usados ​​paira requests nem em aulas de atividades que você usa em seus projetos.

    4.caches resultados (em Json com Jackson e Gson, ou Xml, ou files de text simples, ou files binarys, mesmo usando ORM Lite).

    5.notifica suas atividades (ou qualquer outro context) do resultado do request de networking se e somente se ainda estiviewem vivos

    6.no memory leaks, como o Android Loaders, ao contrário do Android AsyncTasks notifica suas atividades em sua UI Thread.

    7.utiliza um model de tratamento de exception simples, mas robusto.

    Amostras paira começair. https://github.com/octo-online/RoboSpice-samples .

    Uma amostra de robospice em https://play.google.com/store/apps/details?id=com.octo.android.robospice.motivations&feature=seairch_result .

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.