android - Locked and Unlocked detection in background -


i'm working on android app in need detect when user locked , unlocked, not screen on/off. so, can perform action according locked , unlocked. i'm unable achieve goal.

i declare broadcast inside service, register service , receiver inside manifest , actions.

it's working fine, when app open. app goes in background it's stop working.

public class useservice extends service {  @nullable vibrator vibr; mediaplayer audi;  //this service class @override public ibinder onbind(intent intent) {      return null;     }     public void oncreate() {      super.oncreate();           intentfilter filter = new intentfilter();      filter.addaction(intent.action_screen_off);     filter.addaction(intent.action_screen_on);     filter.addaction("android.intent.action.screen_on");     registerreceiver(receiver, filter); }  //broadcast receiver  private final broadcastreceiver receiver = new broadcastreceiver() {     @override     public void onreceive(context context, intent intent) {         string action = intent.getaction();         keyguardmanager mykm = (keyguardmanager) context.getsystemservice(context.keyguard_service);          if (action.equals(intent.action_screen_off) && mykm.inkeyguardrestrictedinputmode())         {              vibr.vibrate(500);               if( action.equals(intent.action_screen_on) &&  !mykm.inkeyguardrestrictedinputmode()  )             {                  vibr.vibrate(5000);              }          }     } };    //************************started************* @override public int onstartcommand(intent intent, int flags, int startid) {      audi=mediaplayer.create(useservice.this, r.raw.start);     toast.maketext(useservice.this, "the service has been started",toast.length_short).show();      return start_sticky; }   //*******************ondestroy**************** @override public void ondestroy() {     toast.maketext(useservice.this, "the service destroyed",toast.length_short).show();     unregisterreceiver(receiver);   }   } 

main activity

public class mainactivity extends appcompatactivity {   vibrator vibr; button btns;  //this mainactivity   @override protected void oncreate(bundle savedinstancestate) {      super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      btns=(button)findviewbyid(r.id.btn);     vibr=(vibrator) getsystemservice(vibrator_service);       btns.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             intent intent=new intent(getapplicationcontext(), useservice.class);             startservice(intent);          }     });  }  @override protected void onpause() {     super.onpause();  } } 


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -