and-roid
and-roid

Reputation: 21

disable/do nothing ( back , recent ,and home buttons)!

How to disable the mentioned buttons or set it to do nothing ? i know it is possible because ( button mapper )did it using Accessibility service ! even if using root access . how can i do that using android studio ? can you share/write the codes for that ?

Upvotes: 1

Views: 955

Answers (3)

M. Diana
M. Diana

Reputation: 19

Finally I found to achieve disabling the recent key by following:

@Override
protected void onPause() {
super.onPause();

ActivityManager activityManager = (ActivityManager) 
getApplicationContext()
    .getSystemService(Context.ACTIVITY_SERVICE);

activityManager.moveTaskToFront(getTaskId(), 0);
}

will need permission ...

<uses-permission android:name="android.permission.REORDER_TASKS" />

Upvotes: 0

and-roid
and-roid

Reputation: 21

solved

public class example extends AccessibilityService {
    @Override
    protected boolean onKeyEvent(KeyEvent event) {
        int action = event.getAction();
        int keyCode = event.getKeyCode();
        if (action == KeyEvent.ACTION_UP) {
            if (keyCode == 4 && getSharedPreferences("example",0).getBoolean("Back",false)) {
               return true;
            }
            else if (keyCode == 3 && getSharedPreferences("example",0).getBoolean("Home",false)) {
                return true;
            }
            else if (keyCode==KeyEvent.KEYCODE_APP_SWITCH && getSharedPreferences("example",0).getBoolean("Recent",false)){
                return true;
            }
            else {
                return false;
            }
        }
            else {
            return super.onKeyEvent(event);
        }
    }
}

Upvotes: 1

Sachin Titus
Sachin Titus

Reputation: 2349

Executing code when activity goes to background

@Override    
onBackButtonPressed(){
 //do nothing
}

This will do the job for back button

Home button/ recent_button: I think you're trying to do something, like pausing music on activity disappear. If this is the case, read about Activity Lifecycle - Medium post for an answer. I'll give you a a short introduction to activity lifecycle

  • When an activity starts, onCreate() method is called followed by onStart() followed by onResume()

  • When an activity is in the background and comes to the foreground, onCreate is not called! instead, onStart() is called, followed by onResume() When an activity goes to background, onPause() is called

  • If the activity in the background is no longer required to function, onStop() is called If the activity on background is about to be deleted

  • When activity goes to background, onPause() and onStop() are called

  • If the activity is no longer needed, onDestroy() is called to clear the memory

Please put your code to execute when your app is no longer visible on screen in,

@Override
onStop(){
  //your code
}

More reading, Activity lifecycle

Background Processes

Example: An app downloading songs in background

You should read about Services in Android: enter link description here. A service runs with the system, in the background. Make sure you execute the service in a new thread, and not on your UI thread or your application will become unresponsive and/or the service stops running when the Activity goes to pause, stop or destroy states. In other words, services don't run on their own threads

Read about background processses such as threads on Android here: enter link description here

The best resource to learn Services and Threads is This TeamTreeHouse Course

Goodluck!

Upvotes: 1

Related Questions