Add HomeScreen WebApps from another application? [duplicate] - android

Do you know is there a programmatic way to create a web shortcut on the phone user's home screen?
What I want to do is:
When the phone user clicks a button in our Android application, the application will then place a website shortcut onto the phone user's home screen.

First you'll need to add a permission to your manifest.xml
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT">
</uses-permission>
You'll need to build an intent to view the webpage. Something like...
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://www.blablaba.com"));
You can test this by creating a little test app and doing startActivity(i); This should open the browser. Once you verified that the above intent is correct you should move on to the next step.
Now you'll need to actually install the shortcut.
Intent installer = new Intent();
installer.putExtra("android.intent.extra.shortcut.INTENT", i);
installer.putExtra("android.intent.extra.shortcut.NAME", "THE NAME OF SHORTCUT TO BE SHOWN");
installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", I THINK this is a bitmap); //can also be ignored too
installer.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(installer)
;
It's also possible some home screens don't accept this, but most do. So enjoy.
EDIT:
Icon can be set to the shortcut using:
installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(mContext, R.drawable.icon));

As an addition to the correct answer by #Mike-dg and #Gagan, instead of using
putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(mContext, R.drawable.icon))
which requires a ShortcutIconResource, you can use
putExtra("android.intent.extra.shortcut.ICON", Bitmap))
which lets you use any bitmap as the icon. This makes it easy to use a the favicon of the shortcut's website as the icon.

You can't place UI elements on the phone's home screen through an .apk. For the web shortcut - you can create a widget that opens the browser (with the specified URL) upon click.

Related

Prevent auto choose which sharing application use

I'm develop an application in Android.
I have a ListView with some image, when I click a row I open ContextMenu and I have two Choise, eliminate the image or sharing with other application.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
Uri r = Util.getImageUri(getActivity().getApplication(), image);
intent.putExtra(Intent.EXTRA_STREAM, r);
startActivity(intent);
I use this code to sharing the image, but after the first time, every time that I try to share image, the application choose always the first application with wich I send the first image.
How can I prevent it?
I want that user choose every time sharing application.
Thanks.
t
I use this code to sharing the image, but after the first time, every time that I try to share image, the application choose always the first application with wich I send the first image.
That was because you indicated, to the chooser, that you wanted to make this choice "always", instead of "just once".
I want that user choose every time sharing application
Use:
startActivity(Intent.createChooser(intent, "..."));
where "..." is your explanation for this.
In your phone go to settings-->Apps then select the app which get opens by default then there you will find an option "Open by default" open that option, in there press "Clear Defaults".
Then in your app again you will get the list of application to share with

Install launcher icon in home screen once

When a user installs an Android app, a launcher icon is created in the apps menu. Many users I talk to expect that when they install an app, an icon should appear automatically on their home screen ("launch pad").
A lot of apps achieve this somehow. My preference would be to have a window appear on install asking the user "Do you want to add a shortcut?" If that's not possible, any code that auto-adds the shortcut will do.
Android gives a bunch of code here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LauncherShortcuts.html
It is implied that adding this code (and the related xml) to your project will do the trick. But it does not have the effect I want. It seems the code provided is passive, and I need to trigger it somehow.
So my question is:
How do I trigger the installation of a shortcut, and how do I make sure it happens only once, preferably triggered by some kind of "app install" event?
PS:
A complicating factor is that I am building my app using PhoneGap, meaning the main activity is not "Activity" but "DroidGap".
Intent shortcutIntent = new Intent(getApplicationContext(), HomeScreen.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "AIMS ICD");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.aims));
addIntent.putExtra("duplicate", false);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
In the example, it returns the intent in setResult(...). I believe you need to run sendBroadcast(intent) to trigger installation of the shortcut.
The class DroidGap extends Activity so you can just add in the code from the link you provided to add a shortcut.

Cant hide application icon from the Android Menu?

I have also landed in a situation where I want to start Install of a third party app (lets say app Y) from my app (X) and I do not want application Y icon to get created on android main menu.
I have tried code below but still there is a icon of app Y that is getting created in main menu after App Y gets installed successfully. Just please remember that I can not change manifest of App Y as it is a third party app.
I have also tried suggestions on following link but they have not resolved my problem:
How to hide application icon from the Android Desktop?
++++++++++
File file = new File("/sdcard/MyApps/App Y.apk");
Intent intent = new Intent();
Uri uri = Uri.fromFile(file);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.removeCategory("android.intent.category.LAUNCHER");
intent.setDataAndType(Uri.parse(uri.toString()),
"application/vnd.android.package-archive");
startActivity(intent);
++++++++++
Please let me know your suggestions/inputs on this.
Thanks
The only way to do this is to remove the IntentFilter defined in the package's AndroidManifest.xml file: there is no way as a third party installer to do this.
In most cases the answer will be no you can't....
It depends of what kind of application "App Y" is. For e.g. if App Y is a library it is included in your app. There are some sample app's available on the Android site like; "Soft Keyboard"
http://developer.android.com/resources/samples/SoftKeyboard/index.html
Those are actually "services" which are not installed as applications. If App Y is a "normal" application it will be installed on it's own as this is Android's behaviour.
Kind regards and good luck on further development.
You're just modifying your intent. I don't think it's possible to accomplish what you're trying to accomplish. Why not let the user launch the other app themselves?
// Hide Application Icon
try{
PackageManager p = getPackageManager();
p.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}catch (Exception e) {
e.printStackTrace();
}
// UnHide Application Icon
PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName("com.example.removeicon","com.example.removeicon.LauncherActivity");
p.setComponentEnabledSetting(componentName , PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Yes you can hide your application icon but only on rooted devices or system signed apps.....the solution will be to first disable your application using shell command pm disable com.yourapppackagename and then enable it back using pm enable com.yourapppackagename this will first disable your app removing app icon from device and then enabling back your app will bring back the app icon only in device menu and not on homescreen.
if you do not want your app icon anywhere in device then just do not enable it back but then your app wont be of any use as its hidden now and cannot be used till you enable it back.
You can bluff just create an image with 0 opacity and smallest possible size then user will not be able to see the application.

Android: Is there a programming way to create a web shortcut on home screen

Do you know is there a programmatic way to create a web shortcut on the phone user's home screen?
What I want to do is:
When the phone user clicks a button in our Android application, the application will then place a website shortcut onto the phone user's home screen.
First you'll need to add a permission to your manifest.xml
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT">
</uses-permission>
You'll need to build an intent to view the webpage. Something like...
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://www.blablaba.com"));
You can test this by creating a little test app and doing startActivity(i); This should open the browser. Once you verified that the above intent is correct you should move on to the next step.
Now you'll need to actually install the shortcut.
Intent installer = new Intent();
installer.putExtra("android.intent.extra.shortcut.INTENT", i);
installer.putExtra("android.intent.extra.shortcut.NAME", "THE NAME OF SHORTCUT TO BE SHOWN");
installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", I THINK this is a bitmap); //can also be ignored too
installer.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(installer)
;
It's also possible some home screens don't accept this, but most do. So enjoy.
EDIT:
Icon can be set to the shortcut using:
installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(mContext, R.drawable.icon));
As an addition to the correct answer by #Mike-dg and #Gagan, instead of using
putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(mContext, R.drawable.icon))
which requires a ShortcutIconResource, you can use
putExtra("android.intent.extra.shortcut.ICON", Bitmap))
which lets you use any bitmap as the icon. This makes it easy to use a the favicon of the shortcut's website as the icon.
You can't place UI elements on the phone's home screen through an .apk. For the web shortcut - you can create a widget that opens the browser (with the specified URL) upon click.

Strange force close on acore when adding a home screen shortcut

I want to add home screen shortcuts to individual chat rooms, in my app. Here's my code to do so:
Intent roomIntent = roomIntent(room).putExtra("shortcut", true);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, roomIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, room.name);
Parcelable resource = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, resource);
setResult(RESULT_OK, intent);
finish();
When I go to add the shortcut to my home screen, I get a Force Close, not on my own process, but on com.android.acore(!). I've run the debugger and verified that my code gets executed all the way to the call to finish().
If I do this instead for the EXTRA_SHORTCUT_ICON:
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, R.drawable.icon);
It works fine and places the shortcut, and the shortcut behaves correctly -- but of course the shortcut has the stock Android icon, not mine, since this isn't the proper way to specify the icon.
When I look at the source code of other apps that have done this, and at the one example of it in the official Android reference area, my code looks identical. My icon's a standard 48x48 png that I use for the app's main icon, without problems. I've verified this problem on an emulator running stock 1.6, haven't tested other versions.
I have no idea what I'm doing wrong. Any ideas?
The Javadoc for ACTION_CREATE_SHORTCUT says that you should use EXTRA_SHORTCUT_ICON_RESOURCE for Intent.ShortcutIconResource objects, rather than the EXTRA_SHORTCUT_ICON key you're using, which is used for directly placing a Bitmap into the Intent extras.
You should probably also file a bug for the crash on the Android bug tracker, as getting something like this wrong shouldn't bring down acore.

Categories

Resources