Disable Android home button - android

I'm implementing lock screen apps, user required to key in passcode and unlock the device for some duration.
I test in Samsung Note 3 and xiaomi redmi 1.
All the physical buttons in Note 3 has been locked, but somehow I cannot disable xiaomi redmi 1's home button. **The home button will run onPause event.
I used the code couldn't show the button in logcat.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.d("button", String.valueOf(event.getKeyCode()));
return true;
}
I've try to use onPause and run my apps again, but it respond slow.
#Override
public void onPause() {
super.onPause();.
Intent intent = new Intent(LockScreenActivity.this, SettingActivity.class);
startActivity(intent);
}

Try this overridden method
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
but it's not a good practice to disable home button.

change laucher
modify AndroidManifest.xml
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
To:
--------------------------------------
<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.MONKEY" />
</intent-filter>

Related

Choosing launcher programatically without selecting from dialog box

I have two activities
a) LauncherMainActivity
b) FakeHome
The problem is that when I run the application and press the home button a dialog box opens up showing list of launchers.
I don't want any dialog box to open, just want to make the LaunherMainActivity implicitly as default home.
Also when user exit this application on button click, the previous launcher or default launcher should be made default home.
I have added the following lines to the manifest file
<activity
android:name="com.example.launcherm.LauncherMainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.launcherm.FakeHome"
android:label="#string/title_activity_fake_home"
android:enabled="false">
<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>
</activity>
The code for the button by which the default launcher is made home is given below. The main problem in this code is that it shows a dialog box to the user. But i want default home should be made implicitly on button click.
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
PackageManager p = getPackageManager();
ComponentName cN = new ComponentName(LauncherMainActivity.this, FakeHome.class);
p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Intent selector = new Intent(Intent.ACTION_MAIN);
selector.addCategory(Intent.CATEGORY_HOME);
startActivity(selector);
p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}
});
I don't want any dialog box to open, just want to make the LaunherMainActivity implicitly as default home
Fortunately, that is not possible, except perhaps on rooted devices, for obvious security reasons. You cannot hijack the user's HOME button. The user is welcome to make your your app be the default home screen via the dialog.

Click-and-hold the headset button shows no action

I have a BroadcastReceiver and my goal is to detect a long press on the headset button. Tried different versions and none worked. So now I'm trying to check the actions received. If I click normally, I can see the actions 0 (KeyDown) and 1 (KeyUp). If I click and hold, nothing shows up, for either key down or up. What's wrong?
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
Log.e("action",""+event.getAction());
...
}
}
I can post more code if required, but I didn't think it was relevant
Running on an S4 with Android 4.3. Not sure if it matters, S-Voice is frozen, Google Now is disabled. And I've noticed that if my screen is off and I long click, I get a menu "Handle long press with Google Search / AutoVoice"
I think you can take control over long press with this bit of code in manifest. This will open up a dialog which lets you choose which app to use long press. It worked for me when phone is unlocked but it wont work when the phone is locked. It pops out dialog but it vanishes when you unlock the phone. Hope I can get some help with this.
Key is to open up "Activity" instead of implementing broadcast receiver.
<activity
android:name="packagename.activityname"
android:launchMode="singleInstance"
android:showOnLockScreen="true">
<intent-filter android:priority="2147483647">
<action android:name="android.speech.action.WEB_SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter android:priority="2147483647">
<action android:name="android.speech.action.VOICE_SEARCH_HANDS_FREE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="com.sec.action.SVOICE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
I have tested this on Kitkat. No idea what happens on previous versions.

Implement Home Button in custom Launcher

I've written a custom Launcher and it works pretty nice. The Launcher has some pages filled with applications. I want to implement, that the variable that represents the page number is set to 0, if the homebutton is pressed. So if you are on a different page of the launcher and press the homebutton, you enter the starting page.
I've searched a lot, but couldn't find an answer that worked for me.
I've set the app as an launcher in the manifest.
<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>
Is it possible to achieve this? Just change the value of one variable, if the home button is pressed and leave the rest of the home button, as it is.
For everybody who want to do the same thing. that's the way i solved it:
I added the following to the manifest:
<activity
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true">
and wrote this into the java activity:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (Intent.ACTION_MAIN.equals(intent.getAction())) {
final boolean alreadyOnHome =
((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
!= Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
if (alreadyOnHome) {
Log.d("whatever", "Home pressed");
}
}
}
I got it from here: CyanogenMod trebuchet

Custom android launcher goes back to default launcher on pressing back button

I am working on a launcher application. It is being show along with the default launcher on pressing the back button. However, wherever I am in my custom launcher or whether in the starting page of my launcher it self, when I press the back button, the screen navigates back to the default android launcher. I need help.
This is the code I used to set my app as a launcher
<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.MONKEY" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
thank you in advance
This seemed to work for me
Kotlin
override fun onBackPressed() {}
Java
#Override
public void onBackPressed() {}
Those are categories for intents, android.intent.category.LAUNCHER just means when you run the application it will launch the specified class

Android custom launcher (Like toddler lock)

I am making a children's app and I would like to add a child lock to it, just like the one on the app called "Toddler lock". Toddler lock asks you to set the default launcher when the lock is turned on which allows the home button to be disabled.
How would I do this?
Ok so ive got a rough working example here:
Create two activities and two layouts, in the main layout place a normal button. The other layout can just have a blank <LinearLayout>.
This is what my manifest looks like:
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".secondact"
android:label="#string/secondtitle">
<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>
</activity>
Notice <category android:name="android.intent.category.HOME" /> in second activity.
Here is the code from my main activity:
#Override
public void onResume()
{
super.onResume();
Button btn = (Button)findViewById(R.id.startBtn);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent startMain = new Intent(Intent.ACTION_MAIN, null);
startMain.addCategory(Intent.CATEGORY_HOME);
startActivity(startMain);
}
});
}
And in my second activity i have:
#Override
public void startActivity(Intent intent)
{
super.startActivity(intent);
}
When i click the startbtn i get the dialog that you are talking about and you get to chooose launcher or the secondactivity, and select always use. The back button still gets you home, but hopefully this helps. This question might help you further: Disable home button in android toddler app?

Categories

Resources