Existing
an activity class
public class MainActivity extends Activity {
/ / ... Other code omitted
@ Override
public boolean onKeyDown (int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction () == KeyEvent.ACTION_DOWN) {
if ((System.currentTimeMillis () - exitTime)> 2000) / / System.currentTimeMillis () whenever called, certainly greater than 2000
{
Toast toast = Toast.makeText (getApplicationContext (), "press to exit", Toast.LENGTH_SHORT);
toast.setGravity (Gravity.CENTER, 0, 0);
toast.show ();
exitTime = System.currentTimeMillis ();
} Else {
finish ();
System.exit (0);
}
return true;
}
return super.onKeyDown (keyCode, event);
}
}
OnKeyDown want above code inside the class wrote their own tools, others are easy to handle, is the last one super.onKeyDown (keyCode, event) I do not know how to write, I implore heroes to help solve? Thank
public class MyTool {
Context context;
Long exitTime;
public MyTool (Context context) {
this.context = context
}
public boolean exit (int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction () == KeyEvent.ACTION_DOWN) {
if ((System.currentTimeMillis () - exitTime)> 2000)
{
Toast toast = Toast.makeText (context.getApplicationContext (), "press to exit", Toast.LENGTH_SHORT);
toast.setGravity (Gravity.CENTER, 0, 0);
toast.show ();
exitTime = System.currentTimeMillis ();
} Else {
context.finish ();
System.exit (0);
}
return true;
}
return super.onKeyDown (keyCode, event) ;/ / here I do not know how to write
}
}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
super is an object, the Senate changed:
public boolean exit (Activity superActivity, int keyCode, KeyEvent event) {
/ / ...
return superActivity.onKeyDown (keyCode, event);
} Reply:
Positive solution, thank you
Reply:
Just find a problem, how to instantiate superActivity:
MyTool tool = new MyTool (this);
Activity superActivity;
public boolean onKeyDown (int keyCode, KeyEvent event) {
tool.exit (superActivity, keyCode, event)
}
Reply:
Change in thinking, and see what I wrote:
private int pressed = 0;
@ Override
public void onBackPressed () {
if (pressed + + == 0) {
KDialogHelper.showToast (this, "press again to exit the system");
new Timer (). schedule (new TimerTask () {
@ Override
public void run () {
pressed = 0;
}
}, 3000);
}
if (pressed> 1) {
finish ();
}
}
No comments:
Post a Comment