Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,807,088 Members 39,613 Now Online
XDA Developers Android and Mobile Development Forum

[Q]Service doesn't stop even after calling stopService

Tip us?
 
arpitkh96
Old
(Last edited by arpitkh96; 29th May 2014 at 04:17 PM.)
#1  
arpitkh96's Avatar
Senior Member - OP
Thanks Meter 384
Posts: 1,093
Join Date: Feb 2012
Location: panipat
Default [Q]Service doesn't stop even after calling stopService

Here is the service
Code:
public class SearchService extends IntentService {

public SearchService() {
super("SearchService");

// TODO Auto-generated constructor stub
}
   
// Binder given to clients

  

    /**
     * Class used for the client Binder.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with IPC.
     */
  

 @override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub


String FILENAME=intent.getStringExtra("name");
String FILEPATH=intent.getStringExtra("path");


ArrayList a=getSearchResult(new File(FILEPATH),FILENAME);
Toast.makeText(this, "Search Completed", Toast.LENGTH_LONG).show();
publishResults(a);
this.stopSelf();
}
 private void publishResults(ArrayList<File> outputPath) {
    Intent intent = new Intent("notify");
    ArrayList<String> a=new ArrayList<String>(); 
    for(int i=0;i<outputPath.size();i++){a.add(outputPath.get(i).getPath());}
    intent.putStringArrayListExtra("path", a);
sendBroadcast(intent);
  } private void publishResults(String a) {
    Intent intent = new Intent("current");
                  intent.putExtra("name", a);
        sendBroadcast(intent);

  }}
 
arpitkh96
Old
#2  
arpitkh96's Avatar
Senior Member - OP
Thanks Meter 384
Posts: 1,093
Join Date: Feb 2012
Location: panipat
I am using it like this
Code:
 final Intent intent = new Intent(getActivity(), SearchService.class);
            intent.putExtra("path",fpath);
            intent.putExtra("name",a);
        p=new ProgressDialog(getActivity());
    p.setCancelable(false);
    p.setTitle("Searching Files");
    p.setMessage("Please Wait");
        p.getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON );
p.setButton("Cancel", new DialogInterface.OnClickListener(){

public void onClick(DialogInterface p1, int p2)
{
getActivity().stopService(new Intent(getActivity(),SearchService.class));
// TODO: Implement this method
}
});
    p.show();
    
            getActivity().startService(intent);
but even after pressing cancel button,broadcast is received in activity

Sent from my GT-S5570 using XDA Premium 4 mobile app
 
sak-venom1997
Old
#3  
sak-venom1997's Avatar
Senior Member
Thanks Meter 404
Posts: 920
Join Date: Feb 2013
Location: Lucknow

 
DONATE TO ME
A service to display a toast and brodcast the data it recieved looks like a design flaw

anyways you are extending the intent service i guess it does not implement stopService() rather it stops automatically when it has nothing to do[not sure with it please check documentation for IntentService never actually used one of those ]
I guess you need to extend the Service class from package android.app

Sent from my GT-S5302 using Tapatalk 2
 
arpitkh96
Old
#4  
arpitkh96's Avatar
Senior Member - OP
Thanks Meter 384
Posts: 1,093
Join Date: Feb 2012
Location: panipat
Quote:
Originally Posted by sak-venom1997 View Post
A service to display a toast and brodcast the data it recieved looks like a design flaw

anyways you are extending the intent service i guess it does not implement stopService() rather it stops automatically when it has nothing to do[not sure with it please check documentation for IntentService never actually used one of those ]
I guess you need to extend the Service class from package android.app

Sent from my GT-S5302 using Tapatalk 2
Actually I am using toast just for debugging.I am learning services. so I might be wrong at places.I made this service to search for files while an indeterminate progress dialog shows in activity till the broadcast of result is received.
I used intentservice because it was supposed to do one work at a time.please suggest me exact ways to use service in my case.I also want to make sure that if activity is paused(minimized) then, when task is completed activity is also started

Sent from my GT-S5570 using XDA Premium 4 mobile app
 
SimplicityApks
Old
#5  
SimplicityApks's Avatar
Senior Member
Thanks Meter 327
Posts: 323
Join Date: May 2013
Location: Aachen
Quote:
Originally Posted by arpitkh96 View Post
Actually I am using toast just for debugging.I am learning services. so I might be wrong at places. I made this service to search for files while an indeterminate progress dialog shows in activity till the broadcast of result is received.
I used intentservice because it was supposed to do one work at a time.please suggest me exact ways to use service in my case.I also want to make sure that if activity is paused(minimized) then, when task is completed activity is also started
You can still use an IntentService to do that. To stop it just pass an Intent to it with a boolean extra indicating that you don't want to do anything. You'll need only one more if clause in the onHandleIntent of the service.
 
arpitkh96
Old
#6  
arpitkh96's Avatar
Senior Member - OP
Thanks Meter 384
Posts: 1,093
Join Date: Feb 2012
Location: panipat
Quote:
Originally Posted by SimplicityApks View Post
You can still use an IntentService to do that. To stop it just pass an Intent to it with a boolean extra indicating that you don't want to do anything. You'll need only one more if clause in the onHandleIntent of the service.
That didnt worked I used it like this.
Code:
 @Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub


String FILENAME=intent.getStringExtra("name");
String FILEPATH=intent.getStringExtra("path");
        boolean b=intent.getBooleanExtra("run",false);
while(b){
ArrayList<File> a=getSearchResult(new File(FILEPATH),FILENAME);

publishResults(a);
this.stopSelf();}
}
Code:
 final Intent intent = new Intent(getActivity(), SearchService.class);
            intent.putExtra("path",fpath);
            intent.putExtra("name",a);
intent.putExtra("run",true);
        p=new ProgressDialog(getActivity());
    p.setCancelable(false);
    p.setTitle("Searching Files");
    p.setMessage("Please Wait");
        p.getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON );
p.setButton("Cancel", new DialogInterface.OnClickListener(){

public void onClick(DialogInterface p1, int p2)
{
Intent j=new Intent(getActivity(),SearchService.class);
j.putExtra("run",false);
getActivity().stopService(j);
// TODO: Implement this method
}
});
    p.show();
    
            getActivity().startService(intent);
Sent from my GT-S5570 using XDA Premium 4 mobile app
 
SimplicityApks
Old
#7  
SimplicityApks's Avatar
Senior Member
Thanks Meter 327
Posts: 323
Join Date: May 2013
Location: Aachen
Quote:
Originally Posted by arpitkh96 View Post
That didnt worked I used it like this.
Code:
 @Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub


String FILENAME=intent.getStringExtra("name");
String FILEPATH=intent.getStringExtra("path");
        boolean b=intent.getBooleanExtra("run",false);
while(b){
ArrayList<File> a=getSearchResult(new File(FILEPATH),FILENAME);

publishResults(a);
this.stopSelf();}
}
Code:
 final Intent intent = new Intent(getActivity(), SearchService.class);
            intent.putExtra("path",fpath);
            intent.putExtra("name",a);
intent.putExtra("run",true);
        p=new ProgressDialog(getActivity());
    p.setCancelable(false);
    p.setTitle("Searching Files");
    p.setMessage("Please Wait");
        p.getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON );
p.setButton("Cancel", new DialogInterface.OnClickListener(){

public void onClick(DialogInterface p1, int p2)
{
Intent j=new Intent(getActivity(),SearchService.class);
j.putExtra("run",false);
getActivity().stopService(j);
// TODO: Implement this method
}
});
    p.show();
    
            getActivity().startService(intent);
First, if you look in the Dokumentation for IntentService, it says that you should not call stopSelf because it is already implemented to do that when there are no intents left. So It really should be easier to use a Service if you want to stop it like that.

If you want to keep using the intent service, I'd instead use a boolean instance variable which is checked in the publishResults method so just let the service do its work, but before it is published in the UI thread check if the dialog was canceled or not. Otherwise because you have two threads you can't be sure when the other thread receives the boolean change.

To me it seems like you could also use an AsyncTask to handle the threading and that class is easily cancelable .
 
arpitkh96
Old
#8  
arpitkh96's Avatar
Senior Member - OP
Thanks Meter 384
Posts: 1,093
Join Date: Feb 2012
Location: panipat
Quote:
Originally Posted by SimplicityApks View Post
First, if you look in the Dokumentation for IntentService, it says that you should not call stopSelf because it is already implemented to do that when there are no intents left. So It really should be easier to use a Service if you want to stop it like that.

If you want to keep using the intent service, I'd instead use a boolean instance variable which is checked in the publishResults method so just let the service do its work, but before it is published in the UI thread check if the dialog was canceled or not. Otherwise because you have two threads you can't be sure when the other thread receives the boolean change.

To me it seems like you could also use an AsyncTask to handle the threading and that class is easily cancelable .
I cannot use Asynctask ,as operation could be long.checking the boolean before publish is good idea.I will try this

Sent from my GT-S5570 using XDA Premium 4 mobile app

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes