我怎么能运行在Android上后台线程code? [英] How can I run code on a background thread on Android?

查看:144
本文介绍了我怎么能运行在Android上后台线程code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一些code在后台持续运行。我不想做一个服务。有没有其他可能的方式?

我已经打过电话了类在我的活动但我的活动一直在后台运行了一段时间,然后停止。该类也将停止工作。

 类testThread实现Runnable
    {
        @覆盖
        公共无效的run()
        {
            档案文件=新的文件(Environment.getExternalStorageDirectory()/ BPCLTracker / gpsdata.txt);
            INT I = 0;

        RandomAccessFile的在= NULL;

        尝试
        {
            在=新RandomAccessFile的(文件,RW);


        }
        赶上(FileNotFoundException异常E)
        {
            // TODO自动生成的catch块
            e.printStackTrace();
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }
        //串线= NULL;
        而(真)
        {
            HttpEntity实体= NULL;
            尝试
            {
                如果(isInternetOn())
                {
                        而((行= in.readLine())!= NULL)
                        {

                            HttpClient的客户端=新DefaultHttpClient();
                            字符串URL =某些URL;
                            HttpPost请求=新HttpPost(URL);
                            StringEntity本身=新StringEntity(线);
                            se.setContentEncoding(UTF-8);
                            se.setContentEncoding(新BasicHeader(HTTP.CONTENT_TYPE,应用/ JSON));
                            实体= SE;
                            request.setEntity(实体);
                            HTT presponse响应= client.execute(要求);
                            实体= response.getEntity();
                            我++;
                        }
                        如果((行= in.readLine())== NULL和放大器;&安培;实体!= NULL)
                        {
                            file.delete();
                            testThread T =新testThread();
                            线T1 =新主题(T);
                            t1.start();
                        }


                }如果//结束
                其他
                {
                    视频下载(60000);

                的其他} //结束
            试试} //结束
            赶上(NullPointerException异常E1)
            {
                e1.printStackTrace();
            }
            赶上(InterruptedException的E2)
            {
                e2.printStackTrace();
            }
            赶上(IOException异常E1)
            {
                // TODO自动生成的catch块
                e1.printStackTrace();
            }
        ,而} //结束
    运行} //结束
 

解决方案

如果您需要:

  1. 执行code在后台线程

  2. 执行code不触及/更新UI

  3. 执行(短期)code这将需要至多几秒钟即可完成

然后用下面的清洁和高效的模式,它使用的AsyncTask:

  AsyncTask.execute(新的Runnable(){
   @覆盖
   公共无效的run(){
      // TODO你的背景code
   }
});
 

I want some code to run in the background continuously. I don't want to do it in a service. Is there any other way possible?

I have tried calling the Thread class in my Activity but my Activity remains in the background for sometime and then it stops. The Thread class also stops working.

class testThread implements Runnable 
    {
        @Override
        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.start();
                        }


                }// 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

解决方案

IF you need to:

  1. execute code on a background Thread

  2. execute code that DOES NOT touch/update the UI

  3. execute (short) code which will take at most a few seconds to complete

THEN use the following clean and efficient pattern which uses AsyncTask:

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

这篇关于我怎么能运行在Android上后台线程code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆