2013-09-26 11 views
-1

我正在android中製作一個應用程序。這總是在後臺運行,5分鐘後打印一個代碼。那個時候應用程序不會顯示用戶,並且所有進程都在後臺運行。請幫幫我。 如何解決這個問題。在android中的背景過程和時間安排

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. 
+0

發佈您的代碼。 –

+0

http://www.vogella.com/articles/AndroidServices/article.html檢查了這個 – Ashok

+0

請檢查http://www.heypasteit.com/clip/0Z4G –

回答

-1

寫您的UI線程更新邏輯可運行內部,如圖:

YourActivity.this.runOnUiThread(new Runnable() { 

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

        } 
       }); 
+0

這是工作,但歷史清楚,停止此代碼...並感謝... –

+0

http://www.heypasteit.com/clip/ 0Z4G –

+0

@ user2749218 - 請不要惹別人快速接受你的答案,特別是當這是一個低質量的答案,只解決了一小部分問題。 –