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