Android 5.0.2 Fixes 2012 Nexus 7! Sony’s Making a Google Glass Competitor? – XDA TV

Android 5.0.2 Lollipop is available for the … more

Early Lollipop Ports for Micromax A116 and A117

It feels like it was only yesterday when Google announced the mysterious Android L.After … more

Now Gesture Tweaks Replaces Google Now with Custom Action

On most modern Android devices with software navigation keys, swiping up from the … more

ToqAN Fixes Android 5.0 Notification Bug on Qualcomm Toq

The Qualcomm Toq is probably one of the lesser known smartwatches on the market … more

Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

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

OP arpitkh96

29th May 2014, 05:12 PM   |  #1  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 553
 
1,126 posts
Join Date:Joined: Feb 2012
Donate to Me
More
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);

  }}
Last edited by arpitkh96; 29th May 2014 at 05:17 PM.
29th May 2014, 05:19 PM   |  #2  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 553
 
1,126 posts
Join Date:Joined: Feb 2012
Donate to Me
More
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
30th May 2014, 08:48 PM   |  #3  
sak-venom1997's Avatar
Senior Member
Flag Lucknow
Thanks Meter: 409
 
921 posts
Join Date:Joined: Feb 2013
Donate to Me
More
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
31st May 2014, 01:16 PM   |  #4  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 553
 
1,126 posts
Join Date:Joined: Feb 2012
Donate to Me
More
Quote:
Originally Posted by sak-venom1997

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
1st June 2014, 12:26 AM   |  #5  
SimplicityApks's Avatar
Senior Member
Flag Aachen
Thanks Meter: 337
 
347 posts
Join Date:Joined: May 2013
More
Quote:
Originally Posted by arpitkh96

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.
1st June 2014, 06:53 AM   |  #6  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 553
 
1,126 posts
Join Date:Joined: Feb 2012
Donate to Me
More
Quote:
Originally Posted by SimplicityApks

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
1st June 2014, 10:11 AM   |  #7  
SimplicityApks's Avatar
Senior Member
Flag Aachen
Thanks Meter: 337
 
347 posts
Join Date:Joined: May 2013
More
Quote:
Originally Posted by arpitkh96

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 .
1st June 2014, 08:12 PM   |  #8  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 553
 
1,126 posts
Join Date:Joined: Feb 2012
Donate to Me
More
Quote:
Originally Posted by SimplicityApks

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

Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes