Choosing launcher programatically without selecting from dialog box - android

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.

Related

Use the app selection interface to choose different ways to launch activity

I'd like to use the app selection interface to change starting values in an activity. The app selection interface I'm referring to is the one that pops up when you have multiple apps to catch the same type of intent, and allows the user to choose one as well as always use that selection.
The activity in question currently displays a button for the selection of two choices, builds an intent based on that, fires the intent, and exits. That's it. Instead of the custom built button, I'd like to use the above stated interface to display the choices, as the majority of the interface will retain the user's chosen language. Ideally, this would be used to just set a value in the Activity's class. The two selections would need to have different names, in this case term/xterm.
Is there a way to accomplish this?
final Intent intent = getLaunchIntent();
//Would like to replace this Alert Dialog with the app selection interface
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.launch_preference);
builder.setPositiveButton(R.string.term_preference, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
intent.putExtra("launchType", "launchTerm");
dialog.dismiss();
startActivity(intent);
finish();
}
});
builder.setNegativeButton(R.string.xterm_preference, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
intent.putExtra("launchType", "launchXTerm");
dialog.dismiss();
startActivity(intent);
finish();
}
});
builder.create().show();
builder.create().dismiss();
Warning : Partial answer (look at the bottom)
The app selection interface you refereing to is showing when there are several ways to react to an intent.
So I guess your application should declare 2 different definitions for the same intent.
The idea is to create 2 activities for this (let's call them "ReactActivity1" and "ReactActivity2"), which will launch your third activity with different parameters
AndroidManifest.xml
<activity android:name=".ReactActivity1" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="(insert a scheme here)" />
(...)
</intent-filter>
</activity>
<activity android:name=".ReactActivity2" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="(insert a scheme here)" />
(...)
</intent-filter>
</activity>
ReactActivity1
(...)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getLaunchIntent();
intent.putExtra("launchType", "launchTerm");
startActivity(intent);
finish(); // Running finish() inside onCreate() method will make this activity
// "invisible" (it will never be shown to the user, and will
// immediatly be replaced by the next one...)
}
ReactActivity2
(...)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getLaunchIntent();
intent.putExtra("launchType", "launchXTerm");
startActivity(intent);
finish();
}
Issue : The problem is that I'm not sure it is possible to make both choice look different (with a different name, like "Term"/"XTerm"). I think that Android system will show the name of the application in both case.
I succeeded in this by doing the following:
If the intent that creates my main activity comes from being launched, I send a custom intent that is caught by two other activities in the app. This allows for a selection between the two. The text that is displayed can be set by putting an android:label element in the opening intent-filter.
Each of those activities then sends an intent to the main activity with an extra determining launch type, and execution can continue as normal.
<activity android:name=".Activity1" >
<intent-filter android:label="text to be displayed in selection" >
<action android:name="SELECTION_INTENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".Activity2" >
<intent-filter android:label="text to be displayed in selection" >
<action android:name="SELECTION_INTENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="MainActivity" >
<intent-filter>
<action android:name="CHOICE_INTENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Random Activity opens instead of Launcher Activity

After exiting from my app if i open it again random activities open instead of Launcher Activity. The problem continues even after putting in manifest. I have set the category as DEFAULT as shown in the code below. In the main activity i have put the code (given below) to exit the app on back press.
//Android Manifest -- i have set all activities as DEFAULT except LAUNCHER activity.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
#Override// In Main Activity I have put this code to exit app when back is pressed
public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
Have you tried setting the category to LAUNCHER like this
`<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
`

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 - Launch activity from search button - remove my app's activity from search long press options

One of the features my app offers is to select something to be done in the background at search button long press. The activity that does that appears in the search long press options even if this feature is not activated in my app's settings screen. Is there a way to make my app's option not appear in the search long press options programmatically in this case?
This is how I define my activity in the manifest:
<activity
android:name="com.bill.deuterh.SearchButtonActivity"
android:theme="#style/Theme.Transparent"
android:label="#string/title_activity_search_button" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH_LONG_PRESS"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
Split your <intent-filter> into 2 separate Activitys like this:
<activity
android:name="com.bill.deuterh.SearchButtonActivity"
android:theme="#style/Theme.Transparent"
android:label="#string/title_activity_search_button" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity-alias
android:targetActivity="com.bill.deuterh.SearchButtonActivity"
android:name="com.bill.deuterh.SearchButtonActivityAlias"
android:enabled="false"
android:label="#string/title_activity_search_button">
<intent-filter>
<action android:name="android.intent.action.SEARCH_LONG_PRESS"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity-alias>
You'll notice that the alias has enabled="false" which means that the default state of this Activity is disabled. In this state it won't show up in the search long press options list.
When you want to enable the search activity so that it shows up in the list, you just need to enable the alias component using the PackageManager, like this:
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(new ComponentName(this,
"com.bill.deuterh.SearchButtonActivityAlias"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
If you want to disable it again, you can do it like this:
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(new ComponentName(this,
"com.bill.deuterh.SearchButtonActivityAlias"),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);

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