0

i have two checked box in my activity. first i want to send some parameter to my service and second if user unchecked the box the service stops. here is my code snipped

here i am passing parameter from my MainActivity but i am not getting it in the service

Intent serviceIntent=new Intent(MainActivity.this,MyService.class);
serviceIntent.putExtra("name", "image");
startService(serviceIntent);

Service Class

public class MyService extends Service {

String selectedAudioPath = "";
private MyThread myythread;
public Intent intent;
public boolean isRunning = false;
long interval=30000;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

    myythread  = new MyThread(interval);

}

@Override
public synchronized void onDestroy() {
    super.onDestroy();
    if(!isRunning){
        myythread.interrupt();
        myythread.stop();
    }
}

@Override
public synchronized void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

    if(!isRunning){
        this.intent = intent;

        myythread.start();
        isRunning = true;
    }
}

class MyThread extends Thread{
    long interval;
    public MyThread(long interval){
        this.interval=interval;
    }
    @Override
    public void run(){
        while(isRunning){
            System.out.println("Service running");
            try {

                Bundle extras = intent.getExtras(); 
                if(extras == null)
                    Log.d("Service","null"); // i am getting null here 
                else
                {
                    Log.d("Service","not null");
                    String from = (String) extras.get("name");
                    if(from.equalsIgnoreCase("image")){
                        uploadImages();
                    Thread.sleep(interval);
                }else if(from.equalsIgnoreCase("audio")){

                    uploadAudio();
                    Thread.sleep(interval);
                } }



            } catch (InterruptedException e) {
                isRunning = false;
                e.printStackTrace();
            }
        }
    }

and second thing i want to stop the service if during process user unchecked the box. i followed this question How to pass a parameter from an activity to a service...when the user stop the service i followed every step but i got stuck when i write this line

registerReceiver(new UserStopServiceReceiver(),  newIntentFilter(USER_STOP_SERVICE_REQUEST));

giving me an error on newIntentFilter . i thing it has to be declare somewhere

Community
  • 1
  • 1
hellosheikh
  • 2,929
  • 8
  • 49
  • 115

1 Answers1

0

First, in your intent, you didn't specify any bundle, just a string. So, replace this

Bundle extras = intent.getExtras();

with this

String myString = intent.getStringExtra("name");

For the second question:

CheckBox repeatChkBx = ( CheckBox ) findViewById( R.id.repeat_checkbox );
repeatChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if ( isChecked )
        {
            // perform logic
        }

    }
});
Merlevede
  • 8,140
  • 1
  • 24
  • 39
  • thankyou for your answer. your first solution works for me. ok can you tell me by using your checkbox code the service get stop if it was running ? and second think if it was so easy to stop a service then why the guy who answered the question in my link which i posted above has given a so much long explanation – hellosheikh Feb 23 '14 at 19:03
  • The link you specify is not related to what you ask. To stop a service you use stopService function. – Merlevede Feb 23 '14 at 19:09
  • i want to stop the service while it is running .. for example i checked the check box now i want to uncheck it so means stop the process. because by using your code my service is not stopping . can you please guide me where this "newIntentFilter" comes. where should i declare this – hellosheikh Feb 23 '14 at 19:12
  • sorry it was a wrong link. sorry for that. can you please look it again – hellosheikh Feb 23 '14 at 19:27
  • Use `stopService` EXACTLY the way you use `startService`. e.g. `stopService(new Intent(MainActivity.this,MyService.class));` – Merlevede Feb 23 '14 at 19:52
  • i already did it but the service is not stopping when i unchecked it – hellosheikh Feb 23 '14 at 19:53
  • I guess you need to check your threading code. From what I can see, your variable `isRunning` is never set to false, to your thread never ends. – Merlevede Feb 23 '14 at 19:56
  • Merlevede so can you please tell me what to do then ? – hellosheikh Feb 23 '14 at 19:58
  • Unfortunately I can't solve it for you, because I would need to spend a lot of time on this. But I hope I pointed you in the right direction. – Merlevede Feb 23 '14 at 20:01
  • OK, a quick one: set your `isRunning` flag to false in onDestroy(). Also the logic is weird with the (!isRunning)... i think the exclamation mark should go away. – Merlevede Feb 23 '14 at 20:04
  • @Mervelde nope still not stopping – hellosheikh Feb 23 '14 at 20:07