1

I am starting and stopping a service from an activity calling startSertice()/stopService() (when user select/deselect a check box and service is not bounded). Every thing is working fine even though the activity that starts the service is closed. In "Running apps" I'm able to see 1 processes, 1 service running. But when I kill the application, using Task manager kind of application, the process is getting killed and service is not working though the running apps showing 0 processes, 1 service. How to make the service working in such situations? I observed the same in some other security applications like Avast with 0 processes, 1 service, while service working properly. Please help me out on this.

Following is the activity on click method

@Override
    public void onClick(View arg0) {
        boolean value = checkBox.isChecked();

        if(value){
            // start the service
            startService(new Intent(this, MyService.class));
            Toast.makeText(this, "Background service started", Toast.LENGTH_SHORT).show();
        } else {
            stopService(new Intent(this, MyService.class));
            Toast.makeText(this, "Background service stopped", Toast.LENGTH_SHORT).show();
        }
    }  

Following is the service class:

public class MyService extends Service{

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate(){
    super.onCreate();
    Log.d("@@@@@@Service","Service created successfully");
}

@Override
public int onStartCommand(Intent intent, int flags, int stardId){
    Log.d("@@@@@@Service","Service started successfully");
    IntentFilter powerButtonIntentFilter = new IntentFilter();
    powerButtonIntentFilter.addAction("android.intent.action.SCREEN_ON");
            this.registerReceiver(pbReceiver, powerButtonIntentFilter);
    Log.d("#######","Power button register registered");
    return START_STICKY;
}

@Override
public void onDestroy(){
    Log.d("@@@@@@Service","Service destroyed successfully");
    this.unregisterReceiver(pbReceiver);
    Log.d("#######","Power button register un-registered");
    super.onDestroy();
}
}

Everything is working fine in ideal case. SCREEN ON action is being listened by the broadcast receiver properly even when the activity that starts the service is closed. I am able to see the app running in settings. But when I force kill the process using Task Manager kind of applications, processes is getting killed and in running apps I am able to see 0 process, 1 service running. Though the service is running after force killing the app from Task manager, broadcast receiver is not listening to the SCREEN ON action. Please help me out on this.

Thanks, JK

Jagan
  • 63
  • 1
  • 7
  • First you need to expand on 'not working' that is very broad. Second, I suggest you to post the code from your service class – Merlevede Mar 03 '14 at 07:12

0 Answers0