How can I add homescreen shortcut for app after installing - java

How can I add shortcut of my application to Android homescreen before I lauch app?
I need it added right after installation of app.

if you publish your app in google play store after installing app auto-create shortcut but if you want to handle that Android provides us an intent class com.android.launcher.action.INSTALL_SHORTCUT which can be used to add shortcuts to the home screen. In following code snippet we create a shortcut of activity MainActivity with the name HelloWorldShortcut.
First, we need to add permission INSTALL_SHORTCUT to android manifest XML.
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
The addShortcut() method creates a new shortcut on the Home screen.
private void addShortcut() {
//Adding shortcut for MainActivity
//on Home screen
Intent shortcutIntent = new Intent(getApplicationContext(),
MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.ic_launcher));
addIntent
.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false); //may it's already there so don't duplicate
getApplicationContext().sendBroadcast(addIntent);
}
Note how we create shortcut Intent object which holds our target activity. This intent object is added into another intent as EXTRA_SHORTCUT_INTENT.
Finally, we broadcast the new intent. This adds a shortcut with the name mentioned as EXTRA_SHORTCUT_NAME and icon defined by EXTRA_SHORTCUT_ICON_RESOURCE.
Also put this code to avoid multiple shortcuts :
if(!getSharedPreferences(Utils.APP_PREFERENCE, Activity.MODE_PRIVATE).getBoolean(Utils.IS_ICON_CREATED, false)){
addShortcut();
getSharedPreferences(Utils.APP_PREFERENCE, Activity.MODE_PRIVATE).edit().putBoolean(Utils.IS_ICON_CREATED, true);
}

Related

How to prevent activity from reloading itself when coming back after visiting other activities?

I have 5 different activities in my app, i enable the user to navigate between those activities by clicking buttons. The main activity has a RecyclerView in it (used to make a feed). the other activities are used for search, profile, notifications, etc. The user opens the app, the main activity is displayed. The user can scroll through the feed (which is the RecyclerView) in the main activity, now the user decides to go to other activities, search, profile, etc. Now, when the user clicks on the button that navigates to the main activity, I want to open back the main activity and display the recycler view at the same scrolling position and with the same items as was when the user just left it off.
Note: I'm not talking here about pressing the back button or using any goBack functions. I want to enable the user to use the main activity and visit all other activities and after all of that he will be able to come back to the main activity and find the feed exactly as he left it off.
In every activity I have five navigation buttons that allowing the user to go from every activity to every activity: click on profileBtn will open the profile activity, etc.
There is a way in which i can back up the state of the recycler view in the main activity before the user wants to go to other activities and restore it when the user clicks on the homeBtn?
Code:
HomeActivity.java:
public void profileBtn_OnClick(android.view.View view)
{
Intent intent = new Intent(getBaseContext(), ProfileActivity.class);
startActivity(intent);
}
ProfileActivity.java, SerachActivity.java, NotificationsActivity.java:
public void homeBtn_OnClick(android.view.View view)
{
/* HERE I WANT TO OPEN BACK THE HOME ACTIVITY AND DISPLAY THE FEED
EXACTLY AS IT WAS WHEN THE USER LEFT IT, HOW DO I DO THAT? */
Intent intent = new Intent(getBaseContext(), HomeActivity.class);
startActivity(intent);
}
You can use the launchMode="singleTask".
The system creates the activity at the root of a new task and routes
the intent to it. However, if an instance of the activity already
exists, the system routes the intent to the existing instance through
a call to its onNewIntent() method, rather than creating a new one.
You can get more information about launchMode here: https://developer.android.com/guide/topics/manifest/activity-element
android:launchMode=["standard" | "singleTop" | "singleTask" | "singleInstance"]
<activity
android:name=".ui.HomeActivity"
android:launchMode="singleTask" />
Please do NOT use the special launch mode singleTask for this purpose. It causes more problems than it solves. These launch modes have complex side-effects that are not obvious and will cause you more problems later.
Your problem is simple to solve using standard Android behaviour. Change your method to look like this:
public void homeBtn_OnClick(android.view.View view)
{
Intent intent = new Intent(getBaseContext(), HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
The flag CLEAR_TOP tells Android to remove all activities from the task that are on top of the target Activity (in this case the target Activity is HomeActivity.
The flag SINGLE_TOP tells Android not to recreate HomeActivity, but to reuse the existing instance of it.

Android api29 How to automatically add app icon on the home screen at install?

I would like my app to automatically add the launch icon on the home screen at installation.
What I've tried already:
addShortcut method in ActivityMain
private void addShortcut() {
Intent shortcutIntent = new Intent(getApplicationContext(),
MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.ic_launcher_background));
addIntent
.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false);
getApplicationContext().sendBroadcast(addIntent);
}
AndroidManifest.xml
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Unfortunately, it doesn't work and I think this method is a bit out of date?
Thank You!
It is not possible to run code in direct response to your own app being installed the first time - only once the user opens your app. However, if the user installs your app from Google Play, it will generally add the icon to the home screen by default.

How to add an already created activity to my project?

I'm creating an App, with some classmates, and we want to share our Activities with others, so we don't have to do all over again. Is that possible?
You can copy the activity class and the XML layout of the activity from one project to another.
The activity class file goes in the source folder and the xml layout goes in the layouts folder.
To declare the activity in the manifest.xml you have to add:
<activity
android:name="com.example.stockquote.StockInfoActivity"
android:label="#string/app_name" />
with the correct name and label (You have to declare in manifest, otherwise it won't work).
You can try to open other activities from main activity with:
Intent myIntent = new Intent(this, MyActivityName.class);
startActivity(myIntent);
In that case, you can add a Button to open the new activities and test if it works:
Button clickButton = (Button) findViewById(R.id.clickButton);
clickButton.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(this, MyActivityName.class);
startActivity(myIntent);
}
});
You can copy the activity class into the Java folder in your project and the XML layout of the activity from one project to another, paste it in the layout folder (under "res").
Remember to fix the package name, and define the activity in the manifest! <activity android:name=".the.right.path.ActivityName"/>
you should enter a path according to the package structure in your project
good luck

Open the default photo gallery app

I want the gallery app to be launched in a separate window, not in my app. I also dont want to choose a picture, I just want to open the default gallery app.
Some questions are very similiar to this one, but they all open the gallery not as a standalone app, always inside of the app which has called
startActivity(intent);.
see here. This is my app called SM2. inside, the default gallery app is visible, which is not the desired behaviour.
The following code has no use if there is no package named 'com.android.gallery' on the phone:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.android.gallery");
if (launchIntent != null) {
startActivity(launchIntent);//null pointer check in case package name was not found
}
this opens a gallery in my app, not as wished as a standalone task:
Intent intent1 = new Intent(Intent.ACTION_VIEW, Uri.parse(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES)));
startActivity(intent1);
and this does also open the gallery in my app:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(
"content://media/internal/images/media/"));
startActivity(intent);
It is launched inside of the application
It is being launched in your task. You can use FLAG_ACTIVITY_NEW_TASK on your Intent to have it launch in its own task.
I also dont want to choose a picture, I just want to open the default gallery app
You are welcome to try using CATEGORY_APP_GALLERY, though not all gallery apps might have an activity that supports this.

getting installed applications programmatically

I am trying to get the installed application in Android after clicking a icon. Under the icon click code I wrote following code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.ACTION_ALL_APPS);
startActivity(intent);
But it kills the application. I want to show the installed applications on the screen programmatically like when we see when press app key on Android.
Is it possible?
where am I doing wrong?
To get list of all installed application you can use the following code
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List pkgAppsList = getPackageManager().queryIntentActivities( mainIntent, 0);

Categories

Resources