I am creating a receiver that captures the power button, and now it works, but it's such behavior Does not do as i want It is defined in some definition, if the user presses 5 times less than 5 seconds, but it is no longer responsive and sometimes takes more than 5 presses so that the receiver performs the specified task.
Someone can tell me how I can improve my code Thanksgiving
Here is my receiver. Class:
Public class receiver extends Broadcast receiver {Public Boolean successful = false; Public boolean test = wrong; Int press; for a long time; Shared Presentations Prefix; Personal reference references; Override public zero at @Receive (Reference Reference, intent to intent) {Audio Manager Manager = (Audio Manager) context.getSystemService (context.AUDIO_SERVICE); If (manager.getMode ()! = AudioManager.MODE_IN_CALL) {this.context = context; If (intent.getAction (.) Equals (Intent.ACTION_SCREEN_OFF) || intent.getAction (.) Equal (Intent.ACTION_SCREEN_ON)) {calculationLogic (); }}} Public Zero Native () {prefs = context.getSharedPreferences (context.getPackageName (), Context.MODE_PRIVATE); Press = prefs.getInt ("repeats", 0); Log D. ("-", "power pressed, #presses:" + press); Log D. ("-", "time" + system timetable ()); // screenoff = true; Time = System.currentTimeMillis (); // Save the initial power BTN if (prefs.getLong ("time", 0) == 0) {log d. ("Receiver", "40"); Prefs.edit (). PutLong ("time", time) .apply (); Press + = 1; } Other {Log D ("Receiver", "44"); // calculate the difference between the first power press and current power press log.d ("-", "time diff =" + time + "-" + prefs.getLong ("time", 0) + "=" + Time - prefs.getLong ("time", 0))); If (time - prefs.getLong ("time", 0)> 5000) {reset (); If (test) {vibration (); (IntroActivity Reference) .onbpressed (); (IntroActivity Reference) .showTestSuccess (Successful); Context.unregisterReceiver (this); } Log D. ("Receiver", "unsuccessful"); } And (if (press == 5) {log D. ("receiver", "success"); reset (); successful = true; log D. ("-", "stare activity"); vibration; // TODO if (test!) {DoAction (reference);} and ((IntroActivity reference) .onbpressed (); (introActivity reference) .showTestSuccess (successful); context.unregisterReceiver (this);} time = 0 ;} And {press + = 1; log D. ("receiver", "press so far", press);}}} prefs.edit (). PutInt ("repeats", press) .apply ();} Private Zero Reset () {Press = 0;. Prefs.edit () ("Time") apply to delete (.);} Private zero vibration () {vibrator V = (vibrator) context.getSystem Service (References.DVBRATOR_SERVICE); v.vibrate (200);} Private Wide DotAction (Reference Reference) {Log D. ("Receiver", "Start Menu"); Intent I = New Intent (); I.setClass (Reference , SplashActivity.class); // i.setClassName ("com.emergencyapp", ".plashActivity"); i.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity (i);}}
To trigger the state of the state, the trigger is queued on the trigger receipt by the power button and by PowerManagerService
Managed In this way, the switch event effectively degrades in such a way that to press the button position to toggle the screen position (or even the actual screen on / off event) between 1: 1 mapping between the press and the system transmission Will not be necessary.
In addition to this, delay between the internal amount and the actual transmission distribution (which is the variable) becomes difficult to press the correct time button for the events you receive.
No comments:
Post a Comment