Block all means to close an android application - android

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

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

Need to switch my app to background after a phone restart or power on

I need my android app to be in background mode after a phone restart/power on.
Currently I am using the following code, so that my app successfully gets launched after a phone restart/power on.
AndroidManifest.xml:
<receiver android:enabled="true" android:name="my_package.BootUpReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
BootUpReceiver.java:
public class BootUpReceiver extends BroadcastReceiver
{
private static SharedPreferences aSharedSettings;
#Override
public void onReceive(Context context, Intent intent)
{
aSharedSettings = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
boolean isUserLoggedIn = aSharedSettings.getBoolean(Key.AUTHENTICATED, false);
if(isUserLoggedIn)
{
Intent aServiceIntent = new Intent(context, MyHomeView.class);
aServiceIntent.addCategory(Intent.CATEGORY_HOME);
aServiceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(aServiceIntent);
}
}
}
As I said above, my app successfully gets launched after a phone restart/power on.
However, after the phone restart/power on, my app was in foreground mode. But I need my app to be in background mode.
Can anyone please say, how to make an app to be in background mode after a phone restart or power on.
I even tried by changing the intent category to
<category android:name="android.intent.category.HOME" />
But no use in it. Can anyone please help me?
Thanks.
I need my app to be just running in background after the phone restart, so that users can select from the minimized app
I think your approach is wrong. All you are trying to do now is to add icon of your app to recent apps list. Your app won't run in background and I think you don't really want it. Am I right?
Recent apps list managed by android and IMHO forcing your app to be in recent apps list is not a very good idea. User will start you app when he wants from launcher or icon on his desktop.
If your broadcast receiver is working fine and app is starting successfully then you can use the below code in your MyHomeView activity's onCreate method to go to the home screen.
Trick is to click HOME button programmatically when app starts.
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
you can pass some variable from the BroadcastReceiver to differentiate a normal request and BroadcastReceiver's request to make the above code conditional.
But if you want to execute it always in background then it would be better to use Service.
It is recommended to change your code to the service to run it in background.
The suggestion which Leonidos replied is correct.
However, Just a workaround for this:
In my BootUpReceiver, I had a seperate boolean flag for this! (Its a bad way. but just a workaround)
SharedPreferences.Editor aPrefEditor = aSharedSettings.edit();
aPrefEditor.putBoolean(Key.IS_DEVICE_RESTARTED, true);
aPrefEditor.commit();
In Oncreate method of MyHomeView:
boolean isDeviceRestarted = aSharedSettings.getBoolean(Key.IS_DEVICE_RESTARTED, false);
if(isDeviceRestarted)
{
SharedPreferences.Editor aPrefEditor = aSharedSettings.edit();
aPrefEditor.putBoolean(MamaBearKey.IS_DEVICE_RESTARTED, false);
aPrefEditor.commit();
moveTaskToBack(true);
}
Thanks

overriding the Home Key Long press in a category.HOME activity

I just created my own "Home" to replace the stock android one or Sense.
All is working fine and I get all I want. My only problem is to replace to long press on home key ( that usually show the last 6 activities you launched) by my own launcher.
I successfully replace the long press on MENU button with this code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Log.i(TAG,"Keycode: "+keyCode);
if (keyCode == KeyEvent.KEYCODE_MENU) {
// this tells the framework to start tracking for
// a long press and eventual key up. it will only
// do so if this is the first down (not a repeat).
event.startTracking();
return true;
}
(...)
and this part part for the long press:
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
//Log.i(TAG,"LONG"+keyCode);
Toast.makeText(Launcher.this,"LONG "+keyCode, Toast.LENGTH_SHORT).show();
if (keyCode == KeyEvent.KEYCODE_MENU) {
(...)
But the problem is that I wasn't able to replace the KeyEvent.KEYCODE_MENU with KeyEvent.KEYCODE_HOME
is that something locked in the code that avoid user to use a Home long press?
Thank a lot for all the information you woulg give me.
Everything I have ever read states that this can't be done... Here is a post on Android Beginners where I asked a very similar question:
http://groups.google.com/group/android-beginners/browse_thread/thread/d8cdcd1c52d79ef1/0f4b184da6f248a9?lnk=gst&q=home+key#0f4b184da6f248a9
However, I have recently come across an app that successfully allows you to launch it by double-tapping the home key so there has got to be something that can be done. I looked into that approach for a while but couldn't get it to work. Now that I know someone else figured it out I'm going to take another stab at it....
EDIT
While overriding a long-press of the home button cannot be done, I have found a way to successfully implement a double-press of the home button. The general idea for this is as follows:
Make your app act as a home replacement app (Look at the sample home app in the SDK samples)
Allow a way in your app to specify a home app to use (it is pretty straightforward to present the user a list of home-replacement apps)
On the first press of the home button start a timer.
If the timer times out, launch the home application
If the user presses the home key a second time before the timer stops, launch your app
Essentially, the home-replacement activity does nothing more than either launch the real home app specified by the user or launch your app... It never displays its own UI.
I have found that this works pretty well, and actually have an app published in the Android Market that does this. If you would like to see it in action, it is called "Quick Launch" and the publisher name is listed as "MagouyaWare"
Hope this helps!
You can register fake activity for the long press HOME button
by adding to manifest:
<intent-filter>
...
<action android:name="android.intent.action.ASSIST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
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.
Actually there is not much code.I will try to explain it if it helps.
For the andriod application in the manifest file, we generally keep the intent filter as:-
Instaed we should make the intent filter look like:-
This is the same intent filter as that of LAuncher.
Now we have an alternate launchjer application. To make it the only launcher application we have to unistall/disable the existing launcher application(default launcher in android).
For this we need to connect the device/emulator and start the adb(android debug bridge).
Then follow the below steps:-
adb shell
pm list packages //This will list all the packages installed
pm disable com.android.launcher //This will disable the launcher application.
Reboot.

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

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.

Categories

Resources