簡體   English   中英

stopService(intent)不停止服務嗎?

[英]stopService(intent) is not stopping service?

這是解決我的問題的一些代碼:

@Override
public void onDestroy() {
    Log.e("onDestroy------------->", "onDestroy called");
    Toast.makeText(this, " onDestroy Started", Toast.LENGTH_LONG).show();
    super.onDestroy();
}

b2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            //startServiceManually();
            stopService(i);

        }
    });
try { 
stopService(new Intent(YOUR ACTIVITY NAME.this, YOUR SERVICE NAME.class));                                      
} catch (Exception e) {                                         e.printStackTrace();            
}

試試這個...

  Intent i = new Intent(this, THE_SERVICE_NAME.class);
  stopService(i);

如果這樣不起作用,請確保您在服務中具有onDestroy;如果使用通知,請確保inonDestroy中是

 stopForeground(true);

或者只是復制...

 @Override
 public void onDestroy(){
  super.onDestroy();
  stopForeground(true);
}

暫無
暫無

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

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