Yugesh
Yugesh

Reputation: 4092

How to run services even after screen off, Lock the screen?

In my application am using services move to another activity after ten minutes even the screen in off,screen locked.

my problem Service is run when screen is in ON condition,but in screen OFF or when lock the screen condition means the service is stopped after i unlock the screen means service start from there.i don't know how to do this thing.Can any one know please help me to solve this problem.

My service time coding

public class Time_out_services extends Service {

Handler mHandler;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //TODO do something useful

    mHandler = new Handler();


    mHandler.postDelayed(first_Task, 4 * 60 * 1000);


    return Service.START_NOT_STICKY;
  }

Runnable first_Task = new Runnable() {
    public void run() {

        mHandler = new Handler();

        Intent i = new Intent();
        i.setClass(Time_out_services.this, Dialog_actvity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);      

        stopSelf();
    }
    }
};

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

Upvotes: 1

Views: 5088

Answers (1)

Anshul
Anshul

Reputation: 106

you have to register a reciever for the screen on off

    private static BroadcastReceiver mReceiver = new ScreenReceiver();

make a method "regScreenReciever()" put this code in that

IntentFilter filter = new IntentFilter();
                    filter.addAction(Intent.ACTION_SCREEN_ON);
                    filter.addAction(Intent.ACTION_SCREEN_OFF);
                    appContext.registerReceiver(mReceiver, filter);

then create a broadcast reciever , override its onRecieve method and then put

if(intent.getAction().equalsIgnoreCase(Intent.ACTION_SCREEN_ON)){
                    Util.disableKeygaurd();
                }
                Intent linkuryIntent = new     Intent(context,UpdateService.class);
                context.startService(linkuryIntent);

your code will run fine

Upvotes: 2

Related Questions