android - How to disable virtual home button in any activity? -
i need disable 3 virtual buttons in activity in app. disabled button , multitask button somehow cannot dsable home button.
i tried onattachedtowindow()
style answers on stackoverflow didn't work me.
i don't want disable home button entire app, want disable single activity window. helps!
note : highly encourage not in app, if want deploy it. show how can it.
since android 4 there no effective method disable home button.it needs little hack. think need kiosk mode in app. in general idea detect when new application in foreground , restart activity
immediately. processes below..
at first create class called kioskservice
extends service
, add following snippet :
public class kioskservice extends service { private static final long interval = timeunit.seconds.tomillis(2); // periodic interval check in seconds -> 2 seconds private static final string tag = kioskservice.class.getsimplename(); private static final string pref_kiosk_mode = "pref_kiosk_mode"; private thread t = null; private context ctx = null; private boolean running = false; @override public void ondestroy() { log.i(tag, "stopping service 'kioskservice'"); running =false; super.ondestroy(); } @override public int onstartcommand(intent intent, int flags, int startid) { log.i(tag, "starting service 'kioskservice'"); running = true; ctx = this; // start thread periodically checks if app in foreground t = new thread(new runnable() { @override public void run() { { handlekioskmode(); try { thread.sleep(interval); } catch (interruptedexception e) { log.i(tag, "thread interrupted: 'kioskservice'"); } }while(running); stopself(); } }); t.start(); return service.start_not_sticky; } private void handlekioskmode() { // kiosk mode active? if(iskioskmodeactive()) { // app in background? if(isinbackground()) { restoreapp(); // restore! } } } private boolean isinbackground() { activitymanager = (activitymanager) ctx.getsystemservice(context.activity_service); list<activitymanager.runningtaskinfo> taskinfo = am.getrunningtasks(1); componentname componentinfo = taskinfo.get(0).topactivity; return (!ctx.getapplicationcontext().getpackagename().equals(componentinfo.getpackagename())); } private void restoreapp() { // restart activity intent = new intent(ctx, myactivity.class); i.addflags(intent.flag_activity_new_task); ctx.startactivity(i); } public boolean iskioskmodeactive(final context context) { sharedpreferences sp = preferencemanager.getdefaultsharedpreferences(context); return sp.getboolean(pref_kiosk_mode, false); } @override public ibinder onbind(intent intent) { return null; } }
add following method in appcontext
class start service
via application context creation.
@override public void oncreate() { super.oncreate(); instance = this; registerkioskmodescreenoffreceiver(); startkioskservice(); // add } private void startkioskservice() { // ... , method startservice(new intent(this, kioskservice.class)); }
you appcontext
class looks
public class appcontext extends application { private appcontext instance; private powermanager.wakelock wakelock; private onscreenoffreceiver onscreenoffreceiver; @override public void oncreate() { super.oncreate(); instance = this; registerkioskmodescreenoffreceiver(); } private void registerkioskmodescreenoffreceiver() { // register screen off receiver final intentfilter filter = new intentfilter(intent.action_screen_off); onscreenoffreceiver = new onscreenoffreceiver(); registerreceiver(onscreenoffreceiver, filter); } public powermanager.wakelock getwakelock() { if(wakelock == null) { // lazy loading: first call, create wakelock via powermanager. powermanager pm = (powermanager) getsystemservice(context.power_service); wakelock = pm.newwakelock(powermanager.full_wake_lock | powermanager.acquire_causes_wakeup, "wakeup"); } return wakelock; } }
add service declaration , permission retrieving foreground process manifest:
<service android:name=".kioskservice" android:exported="false"/> <uses-permission android:name="android.permission.get_tasks"/> //added permission edit 1 <uses-permission android:name="android.permission.wake_lock" />
i've seen @ 1 site forgot link, whatever have contents , codes, posting answer. link, share you.
Comments
Post a Comment