android - open app when screen on -
i'm trying app started when screen on. lockscreen should show everytime screen goes on. following code wrote far , i'm not sure wrong. there no error message. application runs won't start when screen goes on.
this receiver:
package com.example.screenlocker; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; public class startmyserviceatbootreceiver extends broadcastreceiver { public static boolean screenoff = true; @override public void onreceive(context context, intent intent) { if (intent.getaction().equals(intent.action_screen_on)) { screenoff = true; intent = new intent(context,lockservice.class); i.putextra("screen_state", screenoff); context.startservice(i); } else if (intent.getaction().equals(intent.action_screen_off)) { screenoff = false; } }
and service:
package com.example.screenlocker; import android.app.service; import android.content.broadcastreceiver; import android.content.intent; import android.content.intentfilter; import android.os.ibinder; public class lockservice extends service { public void oncreate(){ intentfilter filter=new intentfilter(intent.action_screen_on); filter.addaction(intent.action_screen_off); broadcastreceiver locker=new startmyserviceatbootreceiver(); registerreceiver(locker, filter); } @override public void onstart(intent intent, int startid) { boolean screenon = intent.getbooleanextra("screen_state", false); if(screenon){ startactivity(new intent(this, mainactivity.class)); } } @override public ibinder onbind(intent arg0) { // todo auto-generated method stub return null; } }
maybe know wrong.
you gave if expression wrong if see correctly.
if (intent.getaction().equals(intent.action_screen_on)) { screenoff = true; intent = new intent(context,lockservice.class); i.putextra("screen_state", screenoff); context.startservice(i); }
you set screenoff = true when comes on. switch false , bellow in action_screen_off
true.
Comments
Post a Comment