I want to disable home button action using the accessibility service. I already read the docs from https://developer.android.com/training/accessibility/service
but didn't understand how to do it? Can anyone help me out in this
Create your own Accessibility Service using any tutorial and put this code inside it....
public class button_lock extends AccessibilityService {
#Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
}
#Override
public void onInterrupt() {
}
#Override
protected boolean onKeyEvent(KeyEvent event) {
if(event.getKeyCode()==KeyEvent.KEYCODE_HOME)
return true;
else
return super.onKeyEvent(event);
}}
Then enjoy....
Override the below method in your Activity,
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
And now handle the key event like this,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{
Log.i("Home Button","Clicked");
}
if(keyCode==KeyEvent.KEYCODE_BACK)
{
finish();
}
return false;
}
You can Disable Home Key by adding this code in your activity:
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}
However, this doesn't work while dialog is showing. To get rid of this problem simply add my code to your Dialog object:
Disable home Key while Dialog showing
Related
I am using a CountDownTimer in a Fragment and trying to stop it if the user hit the physical back button in the phone. I have tried overriding onPause, onDestroy, onStop, onDestroyView but nothing seems to be working. Kind of lost here. Can some one give me a pointer here?
public class Foo extends Fragment {
CountDownTimer myTimer;
#Override
public void onStop() {
super.onStop();
myTimer.cancel();
}
#Override
public void onPause() {
super.onPause();
myTimer.cancel();
}
#Override
public void onDestroyView() {
super.onDestroyView();
myTimer.cancel();
}
#Override
public void onDestroy() {
super.onDestroy();
myTimer.cancel();
}
#OnClick(R.id.btn_greenleft_no)
public void goBack() {
myTimer.cancel();
Objects.requireNonNull(getActivity()).onBackPressed();
}
#OnClick(R.id.btn_greenright_yes)
public void showSuccess(View view) {
markAll();
myTimer.cancel();
(new MusicPlayer()).playSound(getContext(), "cheers.mp3");
final Snackbar snackbar = Snackbar.make(snackBarView, R.string.congratulations, Snackbar.LENGTH_SHORT);
snackbar.show();
myTimer.cancel();
}
private void startTimer(final View view) {
int Seconds = 5;
myTimer = new CountDownTimer(Seconds * 1000 + 1000, 1000) {
public void onTick(long millisUntilFinished) {
String rem = String.valueOf(millisUntilFinished / 1000);
Log.d("APP_DEBUG", "Timer: " + rem);
}
public void onFinish() {
goBack();
}
}.start();
}
}
Here is my 2 cents. Fragment doesn't have an onBackPressed() method which is present in the Activity class. It gets called when physical back button is pressed by the user. Here is what docs says:
Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.
What you can do is override the onBackPressed() method in the parent activity of your Foo fragment, then using an interface communicate to the fragment that back button was pressed by the user. Inside the fragment you can have the desired code to cancel the timer. This answer in the question How to implement onBackPressed() in Fragments? can help with sample code.
Try to modify onBackPressed in Your fragment's parent activity.
#Override
public void onBackPressed() {
// I assume this is the way how You add fragment to fragment manager
//getSupportFragmentManager().beginTransaction().replace(android.R.id.content, Foo.getInstance(), Foo.TAG).commit()
// Find fragment by its string TAG and when You get it, call to stop countDownTimer
Foo foo = (Foo) getSupportFragmentManager().findFragmentByTag(Foo.TAG);
if (foo != null) {
foo.stopCountDownTimer();
}
super.onBackPressed();
}
Next step is to declare in Your Foo fragment two things:
public static final String TAG = "Foo";
and
public void stopCountDownTimer() {
myTimer.cancel();
}
For fragment you cant use onBackPressed method, Instead please use this code
#Override
public void onResume() {
super.onResume();
if (getView() == null) {
return;
}
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
myTimer.cancel();
return false;
}
return true;
}
});
}
You have to call countDownTime.finish() as well. I have used it and it works for me.
#Override
public void onDetach()
{
super.onDetach();
if (countDownTimer != null)
{
countDownTimer.cancel();
countDownTimer.onFinish();
}
}
have you tried onBackPressed()
#Override
public void onBackPressed() {
super.onBackPressed();
myTimer.cancel();
myTimer =null;
}
Have you tried adding an OnKeyListener in your fragment like this:
Here "view" is the parent layout of your fragment , the one that hosts the timer.
view.setFocusableInTouchMode(true);
view.requestFocus();
view.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.i(tag, "keyCode: " + keyCode);
if( keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
myTimer.cancel();
return true;
}
return false;
}
});
I want to force exit my android app when it's going to background.So i use this code in my base Activity class.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
finish();
System.exit(0);
}
return super.onKeyDown(keyCode, event);
}
But it's not working.Please help for resolving this.
You need to do like this,
#Override
public void onStop(){
super.onStop();
finish();
}
This will override your Onstop method
I know this question have been asked many times around here, but i didn't find the propert answer for my issue.
this code can disable back button:
#Override
public void onBackPressed() {
// Do Here what ever you want do on back press;
}
but is there anyway that i can disable back button for a temporary time,not for the whole Activity ?
nice answer by Dixit. Just another option
public boolean onKeyDown(int keyCode, KeyEvent event) {
boolean result = false;
if (keyCode == KEYCODE_BACK) {
if (condition) {
result = true;
}
}
return result;
}
N.B ..
it will work on ancient version also
returning true from onKeyDown consumes the default behavior
You have to set on boolean flag where you have to require disable back button set flag value true;
In onBackPressed() you have to put condition as per #Dixit says
#Override
public void onBackPressed() {
if(condition to check){
// this block disable back button
}else{
// this block enable back button
super.onBackPressed();
}
}
If you want to disable backbutton for certain time use this,
//for 5 sec = 5000
countDownTimer = new CountDownTimer(5000,1000) {
#Override
public void onTick(long millisUntilFinished) {
txtWait.setTextColor(getResources().getColor(R.color.errorcolor));
txtWait.setText("Wait( " + millisUntilFinished / 1000+" sec)");
onBackPressed();
}
#Override
public void onFinish() {
YourActivityName.super.onBackPressed();
}
}.start();
And in the override method:
#Override
public void onBackPressed() {
//super.onBackPressed(); commented this to disable the back press
}
Full working code:
YourActivity extends AppCompatActivity{
boolean isBackButtonDisabled = false;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState)
setContentView(R.layout.somelayout);
disableBackButton(4000); //<--Back button is disabled
}
#Override
public void onBackPressed(){
if(!sBackButtonDisabled){
super.onBackPressed();
}
}
private void disableBackButton(final int timeInMilis){
if(!isBackButtonDisabled) {
isBackButtonDisabled = true; //<-- Keep it outside Thread code
Thread t = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(timeInMilis);
} catch (InterruptedException e) {
} finally {
isBackButtonDisabled = false;
}
}
});
t.start();
}
}
}
Note: You can use disableBackButton(time) in other scenarios as well. For example Button click. If
you click button multiple times the Thread will only run once. Because in this code
isBackButtonDisable variable is thread-safe "in a way".
In the app i m looking for the users location using gps.In the Async pre execute method i m showing a toast.I want that while i show that toast the back button should be disabled
aftr the location is found i want to enable the back button in the post execute!
to disable the back button i have used.But this is not working
OnKeyListener mainScreenKeyListener = new OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
boolean disableEvent = false;
if (event.getKeyCode()==KeyEvent.KEYCODE_BACK) {
disableEvent = true;
}
return disableEvent;
}
};
You can declare global variable disableEvent by
final boolean disableEvent;
Your Preexecute method can set it to false by
disableEvent = false;
Your Postexecute method can set it to true by
disableEvent = true;
You can override onBackPressed as shown below:
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
if (disableEvent)
{
// do nothing
}
else
{
// do something
}
}
Here you go
Assign one static variable.and set its value to "NO" in onPreExecute.
in onPostExecute assign its value to "YES".
And write following code in your onBackPressed.
#Override
public void onBackPressed() {
if (decision.equals("NO")) { //Here no means dont allow user to go back
} else {
super.onBackPressed(); // Process Back key default behavior.
}
}
hi for disable you simply call the above function
public void onBackPressed()
{
}
for enable
public void onBackPressed()
{
super.onBackPressed();
super.finish();
//Intent
}
if you want both set flag inside the function
override onBackPress method in your activity
Class A
{
public static boolean isToastShown=false;
#Override
public void onBackPressed() {
if(isToastShown==true)
return false;
else
super.onBackPressed();
}
}
#Override
protected void onPreExecute() {
super.onPreExecute();
//Show your toast here
A.isToastShown=true;
new CountDownTimer(2000,2000) {
#Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
}
#Override
public void onFinish() {
A.isToastShown=false;
}
}.start();
}
I try to use MediaController to play music.
I want the MediaController appear until the "back" button is pressed.
Now I have try below code:
MediaController mediaController = new MediaController(this){
#Override
public void setMediaPlayer(MediaPlayerControl player) {
super.setMediaPlayer(player);
this.show();
}
#Override
public void show(int timeout) {
super.show(0);
}
//instead of press twice with press once "back" button to back
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Activity a = (Activity)getContext();
a.finish();
}
return true;
}
};
But it still one trouble while the MediaController visible.
When the MediaController appear touch the screen, the MediaController will hide.
I also already try below code:
#Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("screen","touch");
return true;
}
But it did not work.
The string did not show in Logcat.
Anyone has idea to do it?
Override this method also inside media controller
#Override
public void hide() {
// TODO Auto-generated method stub
super.show();
}
If you want to keep the hide() method but not have the controller disappearing every time a control is used :
this.mediaController = new MediaController(this){
#Override
public void show(int timeout) {
super.show(0);
}
};