簡體   English   中英

當我調用stopService(intent)時

[英]When I call stopService(intent)

就生命周期而言,當我調用stopService(intent)時,是否會立即調用服務的onDestroy()? 我問,因為我有問題,調用onDestroy()來殺死服務生成的這個線程。

提前致謝!

button.setOnClickListener(new OnClickListener(){
        public void onClick(View v){

            Log.d(Tag, "in button's onClick");


            Log.d(Tag, field.getText().toString());
            String busNo = field.getText().toString();
            field.setText("");
            field.setAdapter(clearAdapter);
            String url = makeURL(busNo);
            Log.d(Tag, "after makeURL(), url is now "+url);
            Intent intent = new Intent(JSoupTest9Activity.this, ServiceTest.class);
            Log.d(Tag, "intent created...");
            intent.putExtra("URL", url);
            Log.d(Tag, "intent's extra put");
            Log.d(Tag, "stopping intent service");
            stopService(intent);
            Log.d(Tag, "starting intent service");

            startService(intent);
            Log.d(Tag, "service started");

            //pendingIntent = PendingIntent.getService(JSoupTest8Activity.this, 0, intent,  0);
            //alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 30*1000, pendingIntent);

        }
    });

如您所見,我調用stopService()來終止任何先前的服務(查詢線程)以啟動新服務(新查詢線程)。

它不會僅通過靜態calliong stopService來調用。 正如活動的生命周期圖中所指出的,如果vm需要資源,它將被銷毀

當我有疑問時,我只需使用日志工具並在LogCat上檢查它們的輸出。 如果您重寫OnDestroy方法,如下所示:

protected void onDestroy() {
Log.d("event","onDestroy was Called!");
super.onDestroy();
}

您可以在調用時驗證自己。

如果你想破壞你在Background中的一個活動,你可以使用一個活動經理:

ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses("<packagename>");

如果它不在您的應用程序中,您當然需要在清單上設置權限。 憑借我有限的經驗,我沒有找到時間來摧毀任何過程; 但我確實找到了很多文件告訴我不要, 例如

進一步檢查,我認為這個問題已被涵蓋

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM