android - Start an activity when screen on -


i have designed lockscreen adnroid. i'm trying use broadcast receiver start lockscreen activity when user pushes power button unlock.

public class receiver extends broadcastreceiver {  @override public void onreceive(context context, intent intent) {      if(intent.getaction().equals(intent.action_screen_on)){         log.w("bam", "screen went on");     }      else if(intent.getaction().equals(intent.action_screen_off)){         log.w("bam","screen went off");     } } 

}

android manifest:`

<uses-sdk     android:minsdkversion="7"     android:targetsdkversion="21" />  <application     android:allowbackup="true"     android:icon="@drawable/ic_launcher"     android:label="@string/app_name"     android:theme="@style/apptheme" >     <activity         android:name=".mylockscreenactivity"         android:label="@string/app_name" >         <intent-filter>             <action android:name="android.intent.action.main" />              <category android:name="android.intent.category.launcher" />         </intent-filter>     </activity>     <receiver         android:name="myadminreceiver"         android:permission="android.permission.bind_device_admin">         <meta-data             android:name="android.app.device_admin"             android:resource="@xml/admin"/>          <intent-filter>             <action android:name="android.app.action.device_admin_enabled" />         </intent-filter>     </receiver>     <receiver android:name=".receiver">       <intent-filter>         <action android:name="android.intent.action.user_present" />         <action android:name="android.intent.action.action_shutdown" />      </intent-filter>     </receiver>     <activity android:name=".lockscreen"></activity> </application> 

`

private actionbar actionbar; private viewpager viewer;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     getwindow().setflags(windowmanager.layoutparams.flag_fullscreen,             windowmanager.layoutparams.flag_fullscreen);     setcontentview(r.layout.lockscreen);     actionbar = getsupportactionbar();     actionbar.hide();      intentfilter filter = new intentfilter(intent.action_screen_on);     filter.addaction(intent.action_screen_off);     broadcastreceiver mreceiver = new receiver();     registerreceiver(mreceiver, filter);      viewer = (viewpager) findviewbyid(r.id.viewr);     viewer.setadapter(new myadapter(getsupportfragmentmanager())); } 

but when user pushes power button lockscreen activity starts after 1-3 seconds. appropriate start activity when user pushes power button first screen off? how can it?

thank advice. (sorry bad english!)

first, unlike other broad casted intents, intent.action_screen_off , intent.action_screen_on cannot declare them in android manifest! i’m not sure why, must registered in intentfilter in java code

use:

edit again

public class youractivity extends activity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.yourlayout);          intentfilter filter = new intentfilter(intent.action_screen_on);         filter.addaction(intent.action_screen_off);         broadcastreceiver mreceiver = new screenreceiver();         registerreceiver(mreceiver, filter);     }      public class screenreceiver extends broadcastreceiver{         @override         public void onreceive(context context, intent intent) {              if(intent.getaction().equals(intent.action_screen_on)){                 log.w("bam", "screen went on");             }              else if(intent.getaction().equals(intent.action_screen_off)){                 log.w("bam","screen went off");             }         }     } }  

tested, worked!


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -