Android - Is It possible to disable the click of home button - android

I have an application, when it launches I have to disable all the buttons on Android device, I succeeded in disabling end call and others. I need to disable home button click. It should not produce any action on click.
Any suggestions highly appreciated

I'm pretty sure Toddler Lock just uses a BroadcastReciever and listens for Intent.ACTION_MAIN and the category Intent.CATEGORY_HOME - that's why when you first launch it, it tells you to check the "use this application as default" box, and makes you select toddler lock.
So, it's not really blocking the Home button at all, it's just setting itself up as the default broadcast receiver for:
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
When you launch Toddler Lock, it probably sets an internal flag, and if you press the home button, it just brings the window to the front. If the flag is not set, it probably launches Launcher explicitly.
I hope that makes sense. It's just a theory, but I'm almost 100% sure that's how it's done.

Add following code to your activity:
#override
public void onAttachedToWindow()
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
Edit:
This works in all older version of android. But will not work in ICS and jelly bean and will give you crash in app
What does this 4 line java code means in android application?

Add this in your manifest.xml for your main activity:
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
The HOME button will always (re-)launch your activity.
Works in Froyo.

here you can find my Android sample application which persist on the home page.
Home, Back, Call, Power button are disabled.
User can end the application only by typing a password.

I found a way to tackle HOME key. For your application set the manifest as
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
Now ur application is an alternate Launcher application.
Use the adb, and disable the launcher application using package manager
pm disable com.android.launcher2
Now the Home key press will laways stay in the same screen.

A further addition to Jeffreys post, here is something that worked for me (and still allows translucent theme)
#Override
public void onAttachedToWindow()
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}
Becuase it makes the keyguard come up, you could also just disable the keyguard whilst the app is in use:
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
This works really well for making your own keyguard app.

Related

How to handle home button issue in Android Launcher applications

I'm creating a sample lock screen application in this i must override the home button, after i researched in both google and stackoverflow i got the result, it's complicated to do it. Here i mention what i did in my app,
Created a service with broadcast-receiver to show my lock screen when the screen goes to off. - working fine.
To override the home, menu, back and search buttons i used the following code,
hope we can override the home button when the application only becomes a launcher so in my manifest.xml i added this code.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<!-- <category android:name="android.intent.category.LAUNCHER" /> -->
</intent-filter>
Also in my Activity i used this code too
#Override
public void onAttachedToWindow() {
// TODO Auto-generated method stub
this.getWindow().setType(
WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG
| WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
So far in my sample application i successfully completed the above to. Now my problem is,
When i unlock the screen then go to any apps, then i click the Device home button, My Lock screen will appear. i tired to disable this but i don't know how can i exactly do this, for this i used some code like below,
/* This should come from a preference that let's the user select an activity that can handle the HOME intent */
String packageName = "com.android.launcher";
String packageClass = "com.android.launcher2.Launcher";
Intent home_intent = new Intent(Intent.ACTION_MAIN);
home_intent.addCategory(Intent.CATEGORY_HOME);
home_intent.setComponent(new ComponentName(packageName, packageClass));
home_intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
/* Here you should catch the exception when the launcher has been uninstalled, and let the user save themselves by opening the Market or an app list or something. Users sometimes use root apps to uninstall the system launcher, so your fake launcher is all that is left. Might as well give the poor user a hand. */
startActivity(home_intent);
No code will help me, my exact need is once i unlock the screen i need to show the default home screen until the screen goes to screen off. is any idea to handle this issue? Thanks in Advance.
Try this solution,
Create a static variable flag which is set to true when you receive the broadcast for when screen goes to off
now in your activity check if the flag is true
#Override
public void onAttachedToWindow() {
if(MyService.Flag == true){
//Continue with your code ...
//....
}else{
finish();
}
}
or do it on onCreate which ever is suitable for you
Once your screen is unlocked then
//Disable the flag
MyService.Flag = false;
Now when your user clicks the Home button the activity is called and check again the flag again and if its false then call the finish() to close the activity

disable all home button and task bar features on Nexus 7

I am building an app that will form part of an exhibition. It will be displayed on a Nexus 7 which will be securely mounted. The app has touchscreen functionality and will display interactive content.
I need to be able to disable as many features as possible whilst on display as I do not want the public to be able to get to anything other than the app.
The main thing I am struggling with is the back/home/recent app list button. I have found some examples of disabling home button (child lock Android - Is It possible to disable the click of home button
) but ideally I need the buttons to be invisible, so to turn off the 'glow' (black would be fine).
Is the bottom section on a Nexus 7 protected in some way, is there another version of Android that would allow me to do this? The Nexus device will only be used for displaying this app, no other functionality is needed.
Any suggestions would be great and very much appreciated.
Your best solution without creating your own custom Android rom to remove the bottom buttons, will be to make the app full screen, override the back button, and make your app a launcher in order to override the home button.
AFAIK, there is no way of overriding the recent apps button.
Edit: One other option would to have a fullscreen app and then use a mount that will cover the buttons. (Thanks to MaciejGórski for the idea).
To make your app full screen, put the following in your activity's onCreate():
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Or you can make the app full screen from within the manifest as well, thanks to #Niels:
<application android:theme="#android:style/Theme.Holo.Light.NoActionBar.Fullscreen">
To override the back button, add this method:
#Override
public void onBackPressed() {
return;
}
Now the home button is trickier, add the following to your manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
and this to your manifest under the <activity>:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
and this to your manifest under the <application>, make sure that the <receiver name> is the full package name path you define:
<receiver android:name="com.example.BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
And lastly, create a java class file called BootCompleteReceiver, and use this code:
public class BootCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startActivityIntent = new Intent(context, YourActivityName.class);
startActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(parentActivityIntent);
}
}
To later disable your app as a home screen launcher, press the recent app button, swipe down from the right side, tap settings, go to apps, then tap the upper right three dots (vertically aligned), press "Reset app preferences", and then finally press "Reset apps".
I think that should just about cover it all.
EDIT 2 I just realized/tested and you do NOT necessarily need the BOOT_COMPLETED intent if you make your application a launcher. This means that the <uses-permission>, <receiver>, and BootComplete.java are not needed. You can just use the <intent-filter> that includes the MAIN, HOME, and DEFAULT attributes.
EDIT 3 More/different information available here: Home Launcher issue with Fragments after reboot
Further to the above, which all worked great, and to make sure a comprehensive answer is out there.....
AFAIK, there is no way of overriding the recent apps button.
I got around this by change onPause app behavior to start an alarmmanager. There may be a more elegant solution, but this works.
First, create repeating alarmmanager setupAlarm(seconds)( full details here and here, note I used repeating alarm rather than one off, think both will work though) that starts your activity
then change onPause to set a 2 second alarm, so whenever someone selects the recent apps button on the nav bar, a 2 second 'alarm' to start mainActivity is set.
#Override
public void onPause() {
setupAlarm(2);
finish(); //optional
super.onPause();
}
So with this and the above, any attempt to use the navigation buttons or restart the app results in app starting. So until I get round to investigating the 'kiosk' style roms this is a very good compromise.
I may be a bit late.
But i've found the, in my opinion, best solution for the Recent Apps Button Problem:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (&& !hasFocus) {
// Close every kind of system dialog
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
// send task back to front
ActivityManager activityManager =
(ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
activityManager.moveTaskToFront(getTaskId(), 0);
}
}
The "send task back to front" part will prevent the pull down of the Notification bar by simply sending it back up instantly and will close the Recent Apps View.
The other one is to close the "Shutdown/Restart" View when he tries to shut down his phone.
Now Excuse my English and have a nice Day.
Greetings
Jimmy

Block all means to close an android application

I'm trying to develop an application for an android tablet.
This tablet will be shown to the public and they can touch it.
I want to block all means to close the app except for a button/preference menu that requires a password.
But after some research I am not sure if this is possible.
Long press on power button still works, home button too and return button.
Will this work? if so how?
you can find all the answers in already asked questions in stackoverflow
Home Button
Return Button
Power Button
I'm pretty sure this can't be done without root access to the device, in order to avoid a troll application to take control of your Android Device if you happen to run it.
At first you need to add your application as home from your manifest
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.settings.SETTINGS" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
after add flag
getWindow().addFlags(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY); //(dont forget to add flag before `setContentView`)
Disable device lock
private void disableLock() {
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(MainActivity.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
}
Disable home Long click
#Override
protected void onUserLeaveHint() {
startActivity(new Intent(MainActivity.this,MainActivity.class));
finish();
super.onUserLeaveHint();
}
After run you need to set your app to home application !!!
I have finally found a way to do this
No doc about this
getWindow().getDecorView().setSystemUiVisibility(8);
But the 8 is a hidden flag to completly disable system UI with this your app is permanly in full screen(Be carefull if you use this keep a way to close app)
The 8 flag is completly undocumented so i can't tell you since with version this work i dev for 4.0 and 4.1 it work for both.
Dunno for 3.0 but haven't any device to try it.
And don't forget android.permission.EXPAND_STATUS_BAR in your manifest
this is not perfect because if you use some alert dialogue the systemUi become visible but if you don't use any you can't quit
Long press power make a powerpopup who make system ui visible too
But you can kill it fast wit the following method
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(!hasFocus) {
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
}
}
If you do this you can't quit your app anymore(or i have forgot a way to close it?) so keep in mind before to make something like SureLock(app avaible on playstore), 3touch in 2 s launch an activity who ask a pass to quit it
Hope this can help and is complete
And a last question is still unanwsered
Can we custom an alert view to call setSystemUiVisibility(8); because if the battery make an alert or if you think you really need an alert, this will show system UI while you alert is visible
Simply you can't do this , you can't stop user to pressing Home Button
you can block Back press event .
You can stop user from pressing home Button using onAttachedToWindow() but this may not work from android 3.2

How to disable the home key when the menu or Alarm Dialog pop up?

How to disable the home key when the menu or Alarm Dialog pop up?
I know how to disable the home key in the activity. But when the menu or Alarm Dialog pop up, the way is invalid. How to implement this function. Or is there no way to deal with this case?
You can not disable Home Key . for this is the last chance the Android System ESCAPE the application.
I had the same requirement in my project. Here is my solution:
1) You should have a rooted device.
2) Make sure that your activity is main activity, it should have params like these:
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
If you have it, when you tap home button android suggest you other apps except Launcher...
3) Next, since we have rooted device, just find and rename Launcher.apk to LauncerBBB.apk(it can be done programmatically even).
4) That's all. Now only your app is main and it will be shown by Home key tap.
P.S. If you need to have an access to Launcher you can start LauncerBBB activity from secret menu of your application.

Overriding Home button for a Car Home replacement app

I have been working on a replacement for the stock Car Home app for a bit, and I am completely stumped on how to override the Home button so that my app will be brought back to the foreground whenever the phone is docked. This is the way that Car Home works, so there must be a way.
It seems that BroadcastReceivers won't work, because the intent that is broadcast whenever the Home button is pressed will still cause the default homescreen app to launch; I cannot prevent it. I can override the Home button from within my app, but that does me no good since this needs to work when the user is outside my app. Car Home also does not do anything weird like set itself as the default homescreen app while it's running (I checked the logcat to make sure).
What can I try next?
Well, after many months I have finally found the answer to this question. The key is the "android.dock_home" metadata element, found here:
http://developer.android.com/reference/android/content/Intent.html#METADATA_DOCK_HOME
By using this in your AndroidManifest.xml, you can make your dock application become the home application temporarily. To do this, add this line to the AndroidManifest.xml inside the Activity tags for the dock app activity:
<meta-data android:name="android.dock_home" android:value="true" />
If the value is set to true, as long as your phone is docked the Home button will return you to the dock app. After undocking, the Home button will take you back to your normal home app.
Unfortunately, there is no way in the public APIs to override the Home button without the user confirming it.
Your best bet would be to implement a CATEGORY_HOME Intent. This means when a user pressed Home they would be presented with the option to run the standard Home or yours and make yours the default if they wanted.
When your application was launched you could then check if the phone was docked. If the phone is not docked you could then open the standard Home screen and close your app before anything is displayed.
You need to the correct intent filter in your manifest for the app to launch automatically when you dock the phone. Refer to http://developer.android.com/reference/android/content/Intent.html#CATEGORY_CAR_DOCK for the information.
I found a way to tackle HOME key. For your application set the manifest as
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
Now your application is an alternate Launcher application.
Use the adb, and disable the launcher application using package manager
pm disable com.android.launcher2.
Now the Home key press will aways stay in the same screen.

Categories

Resources