2011-10-17 17 views
0

簡單代碼:在onStop之後如何不殺死進程?

public class ZSEEActivity extends TabActivity { 
private WebView webview ; 
private WebView webviewtwo; 
private TabHost mTabHost; 
private int a; 


protected void onStart() { 
    super.onStart(); 
    // The activity is about to become visible. 
} 
protected void onStop() { 
    super.onStop(); 
    // The activity is about to become visible. 
} 
protected void onRestart() { 
    super.onRestart(); 
} 
protected void onDestroy(){ 
    super.onDestroy(); 
} 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    final Activity activity = this; 


    mTabHost = getTabHost(); 

    mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Zastępstwa").setContent(R.id.tab1)); 
    mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Plan Lekcji").setContent(R.id.tab2)); 
    mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("O programie").setContent(R.id.tab3)); 



    webview = (WebView) findViewById(R.id.webView1); 
    webviewtwo = (WebView) findViewById(R.id.webView2); 
    final WebSettings webviewtwoSettings = webviewtwo.getSettings(); 
     if (savedInstanceState != null){ 
      webview.restoreState(savedInstanceState.getBundle("stateone")); 
      webviewtwo.restoreState(savedInstanceState.getBundle("statetwo")); 
      webviewtwoSettings.setTextSize(TextSize.LARGER); 
      mTabHost.setCurrentTab(savedInstanceState.getInt("CURRENT_TAB")); 

     } 
     else{ 
      webview.loadUrl("http://zsee.bytom.pl/ogloszenia.php"); 
      webviewtwo.loadUrl("http://zsee.bytom.pl/plannew/index.html"); 
      webviewtwoSettings.setTextSize(TextSize.LARGER); 
      mTabHost.setCurrentTab(0); 
     } 


    webview.setWebViewClient(new WebViewClient() { 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       String summary = "<html><body><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" ><center>Coś się zepsuło :(</center></body></html>"; 
       webview.loadData(summary, "text/html","utf-8"); 
      Toast.makeText(activity, "O nie! " + description, Toast.LENGTH_SHORT).show(); 
      } 
     }); 

    webviewtwo.setWebViewClient(new WebViewClient() { 
     public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
      String summary = "<html><body><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" ><center>Coś się zepsuło :(</center></body></html>"; 
      webviewtwo.loadData(summary, "text/html","utf-8"); 
      webviewtwoSettings.setTextSize(TextSize.NORMAL); 
     Toast.makeText(activity, "O nie! " + description, Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    } 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.main, menu); 
    return true; 
} 

protected void onSaveInstanceState(Bundle outState) { 
    Bundle outStateone = new Bundle(); 
    Bundle outStatetwo = new Bundle(); 
    webview.saveState(outStateone); 
    webviewtwo.saveState(outStatetwo); 


    outState.putBundle("stateone", outStateone); 
    outState.putBundle("statetwo", outStatetwo); 
    outState.putInt("CURRENT_TAB", mTabHost.getCurrentTab()); 
} 



public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle item selection 
    switch (item.getItemId()) { 
    case R.id.item1: 
     final AlertDialog alertdialog= new AlertDialog.Builder(this).create(); 
     alertdialog.setTitle("O Programie"); 
     alertdialog.setMessage("Zmiany w 1.0.1: \n-Obsługa planu z dnia 17.10.2011\n-Drobne Poprawki"); 
     alertdialog.setButton("Fajno", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       alertdialog.cancel(); 
      } 
     }); 
     alertdialog.show(); 
     return true; 
    case R.id.item2: 
     finish(); 
    case R.id.item3: 
     if(mTabHost.getCurrentTab() == 0){ 
     webview.loadUrl("http://zsee.bytom.pl/ogloszenia.php"); 
     } 
     else if(mTabHost.getCurrentTab() == 1) 
     { 
     webviewtwo.loadUrl("http://zsee.bytom.pl/plannew/index.html"); 
     } 
    default: 
     return super.onOptionsItemSelected(item); 
    } 
} 
} 

現在我的問題。在我按下返回按鈕onStop()代碼執行並onDestroy。我怎麼能不殺應用程序?我想在後臺的這個應用程序。現在,當我按回按鈕並打開應用程序,所有數據再次下載並加載到webview。那麼如何讓這個過程在後臺工作?

對不起,我的英語haotic :)

Sierran

回答

2

使用Android service在背景中執行某些操作,而非Activity。

並使用Broadcast receiver來調用您的來自服務的活動。當你的後臺工作完成時。

Android - Service

如果你想要做一些小然後就override onKeyDown()

@Override  
public boolean onKeyDown(int keyCode, KeyEvent event) 
    {   
    if (keyCode == KeyEvent.KEYCODE_BACK) 
     {    
    // put your stuff here or just block the back button for perticular activity    
      return true;   
     }   
    return super.onKeyDown(keyCode, event); 
    } 
+0

我不想完全地禁用後退按鈕..只是改變像爲homeButton他的活動。像歌劇迷。正常的應用程序在背景中,但是當android沒有ram時,那麼歌劇過程就會被殺死。 是的..我不能英語;> – Sierran

+0

所以你需要實現一個服務。:-) – user370305