后台进程; android中的计时设置

时间:2013-09-26 08:41:06

标签: android

我在android中制作一个应用程序。这总是在后台运行,然后5分钟打印一个代码。那个时间应用程序没有显示用户,并且所有进程都在后台进行。请帮我。 如何解决这个问题。

package com.finalapp;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MainActivity.this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub

            for(int i=0 ;i<=2000000;i++)
            {
                try {
                    Thread.sleep(1000);
                    Log.d("final timer",""+ i);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    });



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}


Please tell me how to make this application. For working in background.

Always print this number in background.


Please help me.

1 个答案:

答案 0 :(得分:-1)

将你的ui线程更新逻辑写在runnable中,如下所示:

YourActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
//paste your ui thread logic here

                    }
                });