Block the home button on android 4 - java

I need to lock the home button in an app, because it will be used by older people and they will not know how to get back if they accidently touch on the home button. I already have that code below but that is not working on android 4.
What I really want is when somebody touches the home button, it does not do anything. Do you have any idea that can help me?
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR
&& (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME)
&& event.getRepeatCount() == 0)
{
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed() {
// Do nothing
return;
}

The only way to do this is to make your app the launcher app on that device, which may not be desirable to the vast majority of Android users, regardless of their age.

Related

Android: Stop Back Button From Exiting LockTask / Kiosk Mode

I want to implement a KioskMode, I'm targeting only Android L, since this is a very specific App.
I already went through the process of setting my App as DeviceAdmin, and
DevicePolicyManager.isLockTaskPermitted(this.getPackageName()) already returns true.
I then start a LockTask via startLockTask().
Everything is fine, but when I hold down the backbutton, the app still exits the kiosk mode.
I have overridden onKeyPress to show a custom Dialog for unlocking the app, but this does not hinder android to automatically exit my lock task if the user holds down back.
I don't really know what to do at the moment and would be thankful for every input.
I now have overridden
#Override
public boolean onKeyDown(int KeyCode, KeyEvent event)
{
if(KeyCode == KeyEvent.KEYCODE_BACK)
{
BackDownButtonPressed = true;
if(VolDownPressed)
showTaskLockDialog();
return true;
}
else if(KeyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
VolDownPressed = true;
if(BackDownButtonPressed)
showTaskLockDialog();
return true;
}
return super.onKeyDown(KeyCode, event);
}
#Override
public boolean onKeyUp(int KeyCode, KeyEvent event) {
if(KeyCode == KeyEvent.KEYCODE_BACK)
{
BackDownButtonPressed = false;
return true;
}
else if(KeyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
VolDownPressed = false;
return true;
}
return super.onKeyUp(KeyCode, event);
}
#Override
public void onBackPressed()
{
return;
}
#Override
public boolean onNavigateUp() {
return true;
}
#Override
public boolean dispatchKeyEvent (KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
return true;
}
return true;
}
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//do something or nothing in your case
return true;
}
return super.onKeyLongPress(keyCode, event);
}
For the record, I am using a Samsung SM-T700 Tablet with Cyanogenmod CM12.1
Just to close this topic..
I couldn't figure out a perfect solution to this day. My current workaround is receiving an event if the user leaves the kiosk mode and just entering the kiosk mode again.
Sadly this leaves the user with 2 toasts saying "screen unpinned" and "screen pinned", which is unfortunate. But this satisfies my current needs.
Perhaps you need to override onKeyLongPress
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//do something or nothing in your case
return true
}
return super.onKeyLongPress(keyCode, event);
}
Not sure if it's helpful at all, but I wrote a blog about setting Kiosk Mode here:
http://www.sureshjoshi.com/mobile/android-kiosk-mode-without-root/
And also wrote sample code for it here:
https://github.com/sureshjoshi/android-kiosk-example
Not sure if you see any major differences between your code and mine, but I just tried to do a long press on a Samsung Galaxy Tab 4 running Android 5.0, and it won't exit the app.
Could it be something with rooting with Cyanogen?
If you don't have this in your code, perhaps add it in and check out if you see any problems:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Remove title bar and notification bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ComponentName deviceAdmin = new ComponentName(this, AdminReceiver.class);
mDpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
if (!mDpm.isAdminActive(deviceAdmin)) {
Toast.makeText(this, getString(R.string.not_device_admin), Toast.LENGTH_SHORT).show();
}
if (mDpm.isDeviceOwnerApp(getPackageName())) {
mDpm.setLockTaskPackages(deviceAdmin, new String[]{getPackageName()});
} else {
Toast.makeText(this, getString(R.string.not_device_owner), Toast.LENGTH_SHORT).show();
}
mDecorView = getWindow().getDecorView();
}
and
protected void enableKioskMode(boolean enabled) {
try {
if (enabled) {
if (mDpm.isLockTaskPermitted(this.getPackageName())) {
startLockTask();
mIsKioskEnabled = true;
} else {
Toast.makeText(this, getString(R.string.kiosk_not_permitted), Toast.LENGTH_SHORT).show();
}
} else {
stopLockTask();
mIsKioskEnabled = false;
}
} catch (Exception e) {
// TODO: Log and handle appropriately
}
}
I have the same problem: How to stop "holding the back button" from escaping "Lock Task mode" on Android 6+
The issue only occurs on my Android 7 tablet.
Running the app on my Android 6 tablet fixed the problem.
Could you add some code to illustrate your current work-around?

I need to minimize the android application on back button click

I need to minimize the application when back button is pressed.
I use following code to catch hardware back button click event
help me with the code of minimize on back key pressed
#Override
public boolean onKeyDown(int keyCode, keyEvent event) {
switch(keyCode) {
case KeyEvent.KEYCODE_BACK;
//minimize application
return true;
}
return super.onKeyDown(keyCode, event);
}
I think that you need to treat back event as home event. The code below is how I emulate home pressed when User press back button:
public void minimizeApp() {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
This is a simple code to minimize the application
#Override
public void onBackPressed() {
this.moveTaskToBack(true);
}
try this code, this will minimize Activity.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
this.moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
or
If you want to close the activity use this.finish() method to close the current running activity. instead of this.moveTaskToBack(true);

Tapping / Swiping not detected in Android Studio Google Glass emulator

I'm running an emulator for google glass as seen here, works pretty flawlessly by showing settings, main display and even my activity (which I pretend to be an interactive static card).
http://mobilevangelist.com/2014/01/02/gdk-and-the-android-emulator/
I've seen that the motion gestures are captured using onKeyUp or onKeyDown events but neither are working and I don't understand why.
Here is my code.
public class LiveCardMenuActivity extends Activity {
private TextView textView;
#Override //isn't catching a thing, even with onKeyDown (mouse taps or slides in the emulator)
public boolean onKeyUp(int keycode, KeyEvent event){
Log.d("tag","keyUp");
if(keycode == KeyEvent.KEYCODE_DPAD_CENTER){
Log.d("tag","keypadcenter");
textView.setText("tap");
}else if(keycode == KeyEvent.KEYCODE_BACK){
Log.d("tag","swipedown");
textView.setText("down");
}
return true;
}
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
setContentView(R.layout.live_card);
//does successfully, I can see the layout in the emulator
//and I can swipe it to the left (returning to the main display successfully)
textView = (TextView) findViewById(R.id.textView);
Log.d("tag","attached to window");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.live_card, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_stop:
// Stop the service which will unpublish the live card.
stopService(new Intent(this, LiveCardService.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onOptionsMenuClosed(Menu menu) {
super.onOptionsMenuClosed(menu);
// Nothing else to do, finish the Activity.
finish();
}
}
Can someone help me on this one? Tyvm!
Try calling setFocusable(true) on your content view.
Though I don't know much about how this emulator works, this is how you might get this working for standard Android.
You're using onKeyUp() when you need to be using onKeyDown(). Here's an example I'm using in my Glass project (but I have a physical Google Glass, not using an emulator):
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch( keyCode ) {
case KeyEvent.KEYCODE_DPAD_CENTER:
Log.e("GESTURE_EVENT", "PostVideoActivity.onKeyDown() TAP/KEYCODE_DPAD_CENTER");
// DO SOMETHING
return true;
case KeyEvent.KEYCODE_BACK:
Log.e("GESTURE_EVENT", "PostVideoActivity.onKeyDown() SWIPE/KEYCODE_BACK");
// DO SOMETHING
return true;
default:
Log.wtf("GESTURE_EVENT", "PostVideoActivity.onKeyDown() DEFAULT -- SHOULDNT BE HERE!");
return false;
}
}
edit Maybe this is an issue with the emulator? I doubt it though but you can test this theory by creating some dummy Android project for a phone/phone emulator that is known to work. If this snippet doesn't even work for the other Android emulation then maybe its something else?

Executing simple code on android app before closing

Hello, I've been trying to integrate airpush in andriod but I've been having some issues.
#Override public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
As you can see is the andriod go back code for webview via history.
The problem is that I want execute this code when it has no more history, meaning when the application is about to close.
airpush.startLandingPageAd();
Any help will be greatly appreciated guys :)..
You need to edit your code, like this:
#Override public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
if(mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
else
{
//Your webview doesn't have any history here and you can close your app here.
//You can also execute your Airpush code here.
airpush.startLandingPageAd();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
I hope this helps.
Consider overriding onPause method of Activity for executing a piece of code before the activity hides.
#Override
protected void onPause() {
super.onPause();
airpush.startLandingPageAd();
}

setOnKeyListener onKey not working with Eclipse/ADT Android Virtual Device - Nexus One

Below is the code I'm using.
When the Enter key is pressed, it intercepts (consumes) the key press and clicks an on-screen button instead.
It works fine on my Motorola Android phone, with both the hardware keyboard and the on-screen keyboard.
It also works fine on the Galaxy Nexus Android Virtual Device in Eclipse/ADT.
But it doesn't work on the Nexus One Android Virtual Device in Eclipse/ADT.
Anyone know why that might be?
I'm worried that someone with a Nexus One will download my app and it won't work.
PS: I've also tried several other onKey methods (using onKeyUp, onKeyDown and ACTION_UP) and none of them worked with the Nexus One.
MyEditText.setOnKeyListener(new EditText.OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))
{
MyButton.performClick();
return true;
}
return false;
}
});
MyEditText.setOnKeyListener(new EditText.OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if ((event.getAction() == KeyEvent.ACTION_DOWN) || (keyCode == KeyEvent.KEYCODE_ENTER))
{
MyButton.performClick();
return true;
}
return false;
}
});
Maybe you should try with || instead &&

Categories

Resources