Recent apps button in android - android

I'm newbie in android development. I've created an android app which shows the current location, and I need to override the recent apps button in android and add some options, is that possible?

I faced similar problem, so, i needed to know when the user press the recent apps button (menu key button in old versions of android).
after a several hours of research i didn't found an event to catch when the home button or the menu button is pressed.
i found that it's take a longer time to call onStop() when the user press the home button, so i figure a trick to distinguish between these tow buttons relating on time of response and overriding two methods:
#Override
public void onUserLeaveHint() {
// do stuff
super.onUserLeaveHint();
userLeaveTime = System.currentTimeMillis() ;
isNotPower = true;
}
#Override
public void onStop() {
super.onStop();
if (isNotPower) {
defStop = System.currentTimeMillis() - userLeaveTime;
if (defStop > 200 ) {
// when the home button pressed
}
if (defStop < 200 ) {
// when the recent apps button pressed
}
}
isNotPower = false;
}
the isNotPower parameter that's to check that not the power button is pressed-
when the power button pressed the onStop() method is called but not th onUserLeaveHint().

Is that possible to override recent apps button in android?
Not from an ordinary SDK app.
You are welcome to build your own custom ROM that modifies the overview screen, then convince people to install your custom ROM on their devices.

Related

Screen Pinning unreliable: sometimes asks for passcode on awakening, other times skips directly to pinned app

(this question was originally posted on the android enthusiasts stub in a slightly altered form)
I've been trying to lock an android tablet in kiosk mode for a survey app I'm making; for that I've made use of the Screen Pinning lollipop feature (with passcode enabled).
I used the following code
#Override
protected void onResume() {
super.onResume();
startLockTask();
}
For the most part, this worked "reliably",
- If the application is pinned and the user lets the device screen time out (or presses the power/lock button), the device will enter sleep and next time he tries to awaken the device (via the power/lock button) the pinned app will pop into view again (without the user having to enter the passcode that he obviously doesn't know).
- On the other hand if the user attempts to unpin it, he/she is presented with the lock screen and the passcode.
So far so good.
However some times (and this is puzzling me) when the user tries to awaken the device, instead of going directly to the pinned app, the device will display the lock screen and ask the user for the passcode!
I'm not sure why the inconsistency in behavior (ie on wake, it sometimes asks for passcode, while other times goes directly to the pinned app) and couldn't find any mention anywhere of such a behavior.
Any input will be greatly appreciated!
Thanks!
Apparently every time the device wakes up, the startLockTask() is being executed. In case it was already in pinned mode this would run again, and cause issues.
Eventually, I tested out my initial assumption and edited the onResume() function to perform a startLockTask() only if it is not pinned already.
This seems to have solved the problems (even though I don't understand why it this behavior). Would be glad if somebody could explain this.
I'm posting the answer here for anybody who bumps into this issue.
onResume()
#Override
protected void onResume() {
super.onResume();
if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.LOLLIPOP) {
if (!isAppInLockTaskMode()) {
startLockTask();
}
}
}
isAppInLockTaskMode() was taken from enter link description here and solves the issue for different API versions.
public boolean isAppInLockTaskMode() {
ActivityManager activityManager=(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.M) { // When SDK version is 23
int lockTaskMode=activityManager.getLockTaskModeState();
return lockTaskMode != ActivityManager.LOCK_TASK_MODE_NONE ? true : false;
}
else if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.LOLLIPOP &&
Build.VERSION.SDK_INT< Build.VERSION_CODES.M) {
//When SDK version <=21 and <23. This API is deprecated in 23.
return activityManager.isInLockTaskMode();
}
else {
return false;
}
}

Disable Back button and Recent Task App Button when Intent called in android

I am developing an app that launches app from the device. I was thinking if it is possible to disable the Back and Recent Task App Buttons of the other application that will be called once a button is clicked?
EDIT:
I am developing an app in kiosk mode and I have a button (let's call it btn_CallApp) there to start another application from the device. In my kiosk app, the buttons are perfectly disabled except for Home Button. When I click the btn_CallApp, it will call the startActivity() which will go to the application that I provided.
public void onButtonClicked(View view) {
switch (view.getId()) {
case R.id.btn_CallApp:
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.example.registrationform");
LaunchIntent.putExtra("isFullScreen", true);
startActivity(LaunchIntent);
break;
}
}
It actually runs the Registration form Project but the Back Button and Recent Task App Button are enabled again.
Now, my question is, how can I disable those Button when I call the other application?
for other apps it is not possible.

android intercept recent apps button

I have an application meant for children and I do not want them to be able to click the "Recent Apps" button (the one that looks like two rectangles on top of each other). I am taking care of capturing the back button and the home button and I have searched and read a lot about about trying to capture the Recent Apps button, but most say you cannot or the way they do it is very questionable.
The App "Kids Place" pops up a view that says "Action Not Allowed" and redirects you to its home screen if you press the Recent Apps button, this works even if you are in a different app, so how are they doing this?
Any suggestions, hints would be appreciated.
Thanks.
After lots of searching and coding the current best solution I found is the following:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (!hasFocus) {
windowCloseHandler.postDelayed(windowCloserRunnable, 0);
}
}
private void toggleRecents() {
Intent closeRecents = new Intent("com.android.systemui.recent.action.TOGGLE_RECENTS");
closeRecents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
ComponentName recents = new ComponentName("com.android.systemui", "com.android.systemui.recent.RecentsActivity");
closeRecents.setComponent(recents);
this.startActivity(closeRecents);
}
private Handler windowCloseHandler = new Handler();
private Runnable windowCloserRunnable = new Runnable() {
#Override
public void run() {
ActivityManager am = (ActivityManager)getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
if (cn != null && cn.getClassName().equals("com.android.systemui.recent.RecentsActivity")) {
toggleRecents();
}
}
}
This requires that you use <uses-permission android:name="android.permission.GET_TASKS" />
When using this approach when the user presses the recent apps button it will cause your activity will go through the activity lifecycle as follows: onPause -> onWindowFocusChanged -> onResume.
To the user the behavior appears that pressing the recent apps button has no response. NOTE: that I have found that if you press the recent apps button quickly it will display that view for brief time.
This is not the best solution, but it is a stab at it. If you have a better solution please share.
The best way I have found is to do this:
public class BaseActivity extends Activity {
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.d("Focus debug", "Focus changed !");
if(!hasFocus) {
Log.d("Focus debug", "Lost focus !");
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
}
}
}// all credit goes here: http://www.juliencavandoli.com/how-to-disable-recent-apps-dialog-on-long-press-home-button/
This is not my own code, but this just hides the recent apps list from showing.
In the accepted answer you're using ClassName only for Android 4.2 - 4.4. It won't work on 5.0 and higher, or Android 4.1.
Here is the list of ClassNames for main Android versions:
Android 4.1: "com.android.internal.policy.impl.RecentApplicationsDialog"
Android 4.2 - 4.4: "com.android.systemui.recent.RecentsActivity"
Android 5.0 - 7.1: "com.android.systemui.recents.RecentsActivity" ("s" letter was added)
The best solution for you will be to utilize Accessibility Service.
Override onAccessibilityEvent() method, filter out ClassNames listed above and do something when you detect this event. For example simulate pressing the 'Home' button. You can do this by making a global action in Accessibility Service.
thanks esse for solution for higher SDK! I missed it.
But in my case I need to duplicate call (or effect is unstable)
if (SDK>15){
windowCloseHandler.postDelayed(windowCloserRunnable, 10);
windowCloseHandler.postDelayed(windowCloserRunnable, 300);
}
If you are interested in disabling all system buttons, may be the option would be to kill system bar completely, see Easy way to hide system bar on Android ICS

Android "HOME" key issue at first time , calling onCreate()

When open my application first time , i pressed home key.
again i click my application.
its not calling onResume() directly. its loading from splash screen onCreate().
is it android default.?
After i've pressed "Back" button , app has closed . there after ,i opened the application and pressed home key, the issue dont come its calling onResume() method not from splash screen onCreate().
My problem is , before pressing back key, if we press home key and open the app, tha app will opened newly. its added in stack.
I've download "Facebook" application and checked. that app also hav same issue.
How do resolve this problem in android?
Android may decide to kill your application when it's not in the foreground. If the application was killed, starting it again would probably show the splash screen again.
When open my application first time , i pressed home key.
again i click my application.
When you press Home Key then your application will go in background, and it will start from where you left if you directly open your application from background apps list. (Button beside HOME key)
And if you click on application icon from list of apps, it will launch again from first activity.
You can refer to this link
How to make an android app return to the last open activity when relaunched?
Check your Developer options.
My guess is that your problem is "Don't keep activities".
i got solution from here: http://code.google.com/p/android/issues/detail?id=2373
add this code onCreate() method in splash screen activity:
if (!isTaskRoot()) {
final Intent intent = getIntent();
final String intentAction = intent.getAction();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) &&
intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
finish();
}
}
I think your app will be killed if you need too much memory in the background. So if you know that your app goes to the background free some memory. It must not be all but for testing you could try it.
There is also a callback which informs you that you should free your memory:
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
if(level >= TRIM_MEMORY_MODERATE) {
// free some memory
} else if(level >= TRIM_MEMORY_BACKGROUND) {
// free some more memory
}
}

Home key press Event Listener [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can I detect user pressing HOME key in my activity?
I am using below lines of code to find if the use press the backkey from android phone,its working fine .
But I want to detect home key Button press event,Anyone can guide how it is possible ?
#Override
public void onBackPressed()
{
Toast.makeText(getApplicationContext(),"BackKeyPressed", Toast.LENGTH_LONG).show();
super.onBackPressed();
}
Thanks . . .
You cannot "detect home key Button press event", sorry.
While technically the people who responded are correct here is a simplistic way to detect the home key press by monitoring two events in your activity, it has worked for my simple needs and maybe it will work for yours as well.
I use a 100ms fence around the two events which I find always works for me. NOTE: I have only tested on a handful of phones, like all things in Android your mileage will vary dependent on OS / Hardware (heck even the stuff that's documented and supposed to work sometimes doesn't)
long userInteractionTime = 0;
#Override
public void onUserInteraction() {
userInteractionTime = System.currentTimeMillis();
super.onUserInteraction();
Log.i("appname","Interaction");
}
#Override
public void onUserLeaveHint() {
long uiDelta = (System.currentTimeMillis() - userInteractionTime);
super.onUserLeaveHint();
Log.i("bThere","Last User Interaction = "+uiLag);
if (uiDelta < 100)
Log.i("appname","Home Key Pressed");
else
Log.i("appname","We are leaving, but will probably be back shortly!");
}
No, it is not possible.
From the documentation of the Home keycode:
http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_HOME
public static final int KEYCODE_HOME
Key code constant: Home key. This
key is handled by the framework and is never delivered to
applications.

Categories

Resources