java - Android - Count Power button clicks and Start Activity -
i used below code, didnt find solution.
myreceiver.java:
import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.util.log; import android.widget.toast; public class myreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { log.v("onreceive", "power button pressed."); toast.maketext(context, "power button clicked", toast.length_long) .show(); } }
and mainactivity.java:
import android.os.bundle; import android.app.activity; import android.view.menu; public class mainactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.activity_main, menu); return true; } }
and androidmanifest.xml:
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <receiver android:name=".myreceiver"> <intent-filter> <action android:name="android.intent.action.screen_off"></action> <action android:name="android.intent.action.screen_on"></action> <action android:name="android.intent.action.action_power_connected"></action> <action android:name="android.intent.action.action_power_disconnected"></action> <action android:name="android.intent.action.action_shutdown"></action> </intent-filter> </receiver> </application>
but not getting toast messages on click of power button
. please me on how count of powerbutton
clicks , if clicks equals 5 move actvity
. please me acheive this?
try this,
public class myreceiver extends broadcastreceiver { static int countpoweroff=0; private activity activity=null; public myreceiver (activity activity) { this.activity=activity; } @override public void onreceive(context context, intent intent) { log.v("onreceive", "power button pressed."); toast.maketext(context, "power button clicked", toast.length_long) .show(); if (intent.getaction().equals(intent.action_screen_off)) { countpoweroff++; } else if (intent.getaction().equals(intent.action_screen_on)) { if(countpoweroff==5) { intent =new intent(activity,newactivity.class); activity.startactivity(i); } } }
and,
public class mainactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); intentfilter filter = new intentfilter(intent.action_screen_on); filter.addaction(intent.action_screen_off); myreceiver mreceiver = new myreceiver (this); registerreceiver(mreceiver, filter); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.activity_main, menu); return true; } }
Comments
Post a Comment