【已解決】Android 如何讓應用在後台運行

來源:互聯網
上載者:User

標籤:android

應用在後台跑,這種說法可能不夠準確,就是說應用沒有finish退出,但也不在前台的狀態,例如應用執行中點擊了home鍵一樣。如何?呢?

要點:

退回後台是執行了home鍵,activity分別執行了onPause和onStop,應用沒有被銷毀,退回後台而已,再次運行應用只要執行onResume就可以了。

 完全退出,執行finishactivity會執行onPause,onStoponDestroy,應用被銷毀,會被系統優先回收,再次運行應用要執行onCreate重新開始。

1.執行home鍵

private ResolveInfo homeInfo;

public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

        PackageManager pm = getPackageManager();

        homeInfo =pm.resolveActivity(newIntent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), 0);

//工作內容

 

//工作內容

    goToIdle();

}

 

 

private void goToIdle(){

        ActivityInfo ai =homeInfo.activityInfo;

        Intent startIntent= new Intent(Intent.ACTION_MAIN);

       startIntent.addCategory(Intent.CATEGORY_LAUNCHER);

       startIntent.setComponent(new ComponentName(ai.packageName,

                ai.name));

       startActivitySafely(startIntent);

    }

  

    voidstartActivitySafely(Intent intent) {

       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        try {

           startActivity(intent);

        } catch(ActivityNotFoundException e) {

           Toast.makeText(this, "work wrongly",

                   Toast.LENGTH_SHORT).show();

        } catch(SecurityException e) {

           Toast.makeText(this, "notsecurity",Toast.LENGTH_SHORT).show();

           Log.e(TAG,"Launcher does not have the permission to launch "

                                    + intent

                                    + ".Make sure to create a MAIN intent-filter for the corresponding activity "

                                    + "oruse the exported attribute for this activity.",

                           e);

        }

    }

 

2.back鍵重寫執行home鍵功能

public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode ==KeyEvent.KEYCODE_BACK) {

            goToIdle();

            return true;

        } else

            returnsuper.onKeyDown(keyCode, event);

    }

 

 

3.重寫finish(),

    @Override

    public void finish() {

       //super.finish();//activity永遠不會自動結束了,而是處於後台。

       moveTaskToBack(true);

    }


補充:

本人在做感應器資料的採集中,當程式介面退出(按back鍵,或者按home鍵)時,資料就不會輸出。所以我嘗試做如下簡單修改,取得理想效果,故在此分享。

未修改前代碼:

protected void onStop() {super.onStop();sm.unregisterListener(this);}

修改後代碼(達到自己想要效果):

protected void onStop() {super.onStop();//sm.unregisterListener(this);  //將該行注釋掉,從而保證在退出時不登出感應器監聽器}



【已解決】Android 如何讓應用在後台運行

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.