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

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>

Related

Activate Splash Activity or Main Activity based on the status of app

I want to open a specific file from another app with my app to handle it. For example, open a attached file in a mail from Gmail app.
With the following intent-filter setting, it works fine. When I open a file, MainActivity shows up and handles it irrespective of whether it is opened through another app or directly from my app. But I want the flow starting from SplashScreen Activity, in the event that the file is opened from another app. On the other hand, if app has been activated, activate directly MainActivity.
Does anyone know how to fix it??
AndroidManifest.xml
<activity android:name=".SplashScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:mimeType="*/*"
android:scheme="content" />
</intent-filter>
</activity>
You have to replace your activity from
to
<activity android:name=".SplashActivity">
And in SplashActivity get Action from Intent and then check if the Action is android.intent.action.VIEW Then Open your MainActivity and send your file from SplashActivity to MainActivity.
That's it
You can't change launcher activity from manifest file. You can set more than one <category android:name="android.intent.category.LAUNCHER"/> in intent-filter but You have to set a default activity.
But there is a way you can solve your problem...
Make a launcher activity with No UI and open the activity with condition in onCreate method.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent;
if (condition) {
intent = new Intent(this, ClassA.class);
} else {
intent = new Intent(this, ClassB.class);
}
startActivity(intent);
finish();
}
OR
In your splash activity write condition before setContentView method.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent;
if (condition) {
setContentView();
} else {
intent = new Intent(this, ClassB.class);
startActivity(intent);
finish();
}
}
Hope this will help you.

Android Lollipop App Notification Settings

In Android Lollipop, when you long press a Notification, it gives you access to settings for that Notification's app, such as priority, or simply blocking it. Is there an intent that I can use to access those settings?
Found the answer here - https://plus.google.com/+CyrilMottier/posts/YY6tbJrJMra
You need to add this to the activity you want to be attached to that settings icon
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.NOTIFICATION_PREFERENCES" />
</intent-filter>
In addition to the correct answer from #shmuel :
In case you want this "App settings" system button to jump to one specific fragment of your app's Settings activity deriving from PreferenceActivity, you can create a dummy intermediary activity to handle the intent-filter as details above, and then in this activity you simply do:
public class DeepSettingsFragmentDummyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent settingsIntent = new Intent(this, SettingsActivity.class);
// Standard mechanism for a PreferenceActivity intent to indicate
// which fragment to activate automatically.
settingsIntent.putExtra( PreferenceActivity.EXTRA_SHOW_FRAGMENT,
YourTargetPreferenceFragment.class.getName());
startActivity(settingsIntent);
finish();
}
}
This code automatically launches the YourTargetPreferenceFragment fragment, and then dismisses itself (finish()) so as to not remain in the activity stack when the user hits Back.
Your Manifest must contain:
<activity
android:name=".DeepSettingsFragmentDummyActivity"
android:label="...">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.NOTIFICATION_PREFERENCES" />
</intent-filter>
</activity>

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.

Android library project custom redirect

I’ve an android app and checked as library let it be Mainapp. Now I have created two separate app using that library viz. subapp1 and subapp2. Individual apps are running fine. I’ve login activity in the library package. On successful login user will be redirected to dashboard activity.
Written simply in loginactivity page of the library pack
Intent i = new Intent();
i.setClass(getApplicationContext(), UserhomeActivity.class);
startActivity(i);
Now I need to specify to which activity the user will be redirected based on the sub app. So how I can manage this without replicating the pages. Thanks.
you could provide the class and package name as an argument and start a new intent:
Intent sccuess = new Intent();
sccuess.setClassName(packageName, className);
startActivity(sccuess);
I am not sure I understand you correctly but If I understand you correct, you need to derive your activities from SecureActivity which user is supposed to be logged in to access there.
After you extend your classes from SecureActivity, you can check it in onResume() method.
Here is an example:
public class SecureActivity extends Activity
{
#Override
public void onResume()
{
// Check if user logged in or not.
}
}
public class YourActivity extends SecureActivity
{
// ...
}
In the Mainifest of your Library, your should define like that
<activity
android:name="com.gmail.app.activities.A_Activity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/NoActionBar" >
<intent-filter>
<action android:name="com.gmail.app.A.Fire_Activity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.gmail.app.activities.B_Activity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/NoActionBar" >
<intent-filter>
<action android:name="com.gmail.app.B.Fire_Activity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
In library code where to fire the intent:
Intent i = new Intent(_context.getPackageName() + ".Fire_Activity");
//Action will be like com.gmail.app.A.Fire_Activity or com.gmail.app.B.Fire_Activity
startActivity(i);
PS:
Your Sub-Apps have package-names:
com.gmail.app.A
com.gmail.app.B

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