Reputation: 81
I am developing a application using VOIP. While making call, i don't want user to go to the home screen. I disabled the click of back button by overriding onbackpressed method. But i cant able to figure out how to disable home button.
i have tried
public void onAttachedToWindow()
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
But it throws me exception. I want the code to work in all the versions of android.
Thanks in advance.
Upvotes: 0
Views: 899
Reputation: 101
It's possible but your app must be signed by system key. I decompiled a wizard application and found an example of code, that disables a home button. I used this code in my wizard application but I can't guarantee that it'll be working everywhere. I checked this code on STB with Android 5, 6 and 7.
private void disableHomeButton(Context context){
ContentResolver contentResolver = context.getContentResolver();
try {
if (Build.VERSION.SDK_INT < 17) {
Settings.System.putInt(contentResolver, Settings.System.DEVICE_PROVISIONED, 0);
} else {
Settings.Global.putInt(contentResolver, Settings.Global.DEVICE_PROVISIONED, 0);
}
Settings.Secure.putInt(contentResolver, Settings.Secure.USER_SETUP_COMPLETE, 0);
}
catch(SecurityException e){
}
}
private void enableHomeButton(Context context){
ContentResolver contentResolver = context.getContentResolver();
try {
if (Build.VERSION.SDK_INT < 17) {
Settings.System.putInt(contentResolver, Settings.System.DEVICE_PROVISIONED, 1);
} else {
Settings.Global.putInt(contentResolver, Settings.Global.DEVICE_PROVISIONED, 1);
}
Settings.Secure.putInt(contentResolver, Settings.Secure.USER_SETUP_COMPLETE, 1);
}
catch(SecurityException e){
}
}
AndroidManifest.xml
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="tv.test.wizard"
android:sharedUserId="android.uid.system">
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
....
Upvotes: 0
Reputation: 5566
Actually you can't do this, as for a power button too. It's the system buttons, to which user must always have access, independent from apps purposes. Imagine, that your application will stuck (lag), or lost internet connection. The whole device will be blocked, because user can't return to main menu. The only way to user - is poweroff device. So that is disabled by Android system architecture. Even default call and voip apps not do this, try to follow this rule too. Also, meny vendors have different implementation of it's button by hardware, so this button have different behaviour. As for alternative, you can set your Activity to be fullscreen, and show user warning message, not close your app, when making call. Hope it helps.
Upvotes: 2