I am trying to add a short on home screen programmatically but it wont work.
The code works for Oreo and above but not below that.
public void addShortcutToHomeScreen() {
if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)) {
ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(this, "#1")
.setIntent(new Intent(this, ActivityMemes.class).setAction(Intent.ACTION_MAIN)) //!!! intent's action must be set on oreo
.setShortLabel("Memeify")
.setIcon(IconCompat.createWithResource(this, R.mipmap.ic_launcher))
.build();
ShortcutManagerCompat.requestPinShortcut(this, shortcutInfo, null);
} else {
Intent shortcutIntent = new Intent(getApplicationContext(),
Activity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Name");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.mipmap.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false);
getApplicationContext().sendBroadcast(addIntent);
}
}
Here is my Manifest file.
<activity
android:name="com.my.package.Activities.Activity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Please, Tell me what am I doing wrong cause I have tried to fixed it and have failed. And is there any better way than this? Any help would be appreciated thanks.
EDIT :
Found this in the Logcat.
2019-01-16 13:27:46.773 735-13377/? W/BroadcastQueue: Permission Denial: broadcasting Intent { act=com.android.launcher.action.INSTALL_SHORTCUT flg=0x10 launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } (has extras) } from com.chameleon.memeify (pid=14824, uid=10300) requires com.android.launcher.permission.INSTALL_SHORTCUT due to receiver com.sec.android.app.launcher/com.android.launcher3.home.InstallShortcutReceiver
you can use this method in your application class
public void createShortCut() {
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.logo);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), SplashActivity.class));
sendBroadcast(shortcutintent);
}
}
and apply this permission
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
If your are on Android 8+ you can't use the broadcast anymore and you have to use the ShortcutManager :
https://developer.android.com/about/versions/oreo/android-8.0-changes#as
Android 8.0 (API level 26) includes the following changes to app shortcuts:
The com.android.launcher.action.INSTALL_SHORTCUT broadcast no longer has any effect on your app, because it is now a private, implicit broadcast. Instead, you should create an app shortcut by using the requestPinShortcut() method from the ShortcutManager class.
The ACTION_CREATE_SHORTCUT intent can now create app shortcuts that you manage using the ShortcutManager class. This intent can also create legacy launcher shortcuts that don't interact with ShortcutManager. Previously, this intent could create only legacy launcher shortcuts.
Shortcuts created using requestPinShortcut() and shortcuts created in an activity that handles the ACTION_CREATE_SHORTCUT intent are now fully-fledged app shortcuts. As a result, apps can now update them using the methods in ShortcutManager.
Legacy shortcuts retain their functionality from previous versions of Android, but you must convert them to app shortcuts manually in your app.
To learn more about changes to app shortcuts, see the Pinning Shortcuts and Widgets feature guide.
Related
I am developing an android based note taking application with categories.I am supposed to create notes shortcuts on home screen. When user click on the shortcut the relevant activity should be open and the specific data should be set in Edit-texts i.e Its title and description.I unable to understand the logic to do that.
I tried all possible solutions that come into my mind. I passed Id of note in shortcut intent but when it launch from shortcut the fields are still empty.
This is my snippet of code to create shortcut:
Function to create shortcut:
private void createShortcutOfActivity() {
Intent shortcutIntent = new Intent(getApplicationContext(),
TextNotes.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, editTitle.getText().toString());
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.mipmap.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);
}
This function is called when user click on option to create shortcut.
In Menifest use permission:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Also add intent filter and exported property in non launching activity:
<activity
android:name=".TextNotes"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
The activity receive data from intent when open a note from another activity:
Intent newInt=getIntent();
isDoubleClicked=newInt.getBooleanExtra("Chk",false);
Cat=newInt.getStringExtra("Category");
Id=newInt.getIntExtra("Id",0);
String title=newInt.getStringExtra("Message");
String description=newInt.getStringExtra("Message2");
check=newInt.getStringExtra("Check");
editTitle.setText(title);
editText.setText(description);
I also tried to use this id in the shortcut intent of the function but having no change in result.
shortcutIntent.putExtra("key_primary",Id);
I want to keep the data when open using shortcut.For example for different note shortcut the rspected data should be set in fields just like in whatsapp the chat shortcuts of different contacts can be craeted . But unfortunately I am unable to understand that how should it be done because everytime I open using shortcut its fields become empty. Where should I pass id and how to set data when it launch from shortcut.
If you use shortcutIntent.putExtra("key_primary",Id); then you need to retrieve it using
Id=newInt.getIntExtra("key_primary",0);
That is the first parameter (key_primary) is the key that is used to identify the specific Intent Extra. Thus, the key value must match the key value used when putting the Intent Extra for a value (other than the default) to be retrieved.
As such coding Id=newInt.getIntExtra("Id",0); without using the matching/paired shortcutIntent.putExtra("Id",Id); will always result in 0 as the Intent Extra doesn't exist so the default value is returned.
I have 2 applications with their source codes. When I press button in application one, I need to background "press" button in application 2 (start specific action from it, not MainActivity). So, for example, do command like
send "press_button2" -> APP2
What is the best way to do it?
That's quite a generic question but you'll have to use an implicit intent from your "APP1" and read the intent action in your "APP2".
The steps will be:
Define the implicit intent in your APP1, something like
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "press_button2")
type = "text/plain"
}
startActivity(sendIntent)
In your APP2, set up your manifest to receive the specified action in an activity of your choice, using an intent filter:
<activity android:name="ClickActivity">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
In your "APP2" handle the incoming intent in your activity, something like:
#SuppressLint("MissingSuperCall")
override fun onCreate(savedInstanceState: Bundle?) {
when {
intent?.action == Intent.ACTION_SEND -> {
if ("text/plain" == intent.type) {
intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
// Update UI to reflect text being shared
if (it == "press_button2"){
myButton.performClick()
}
}
}
}
}
}
Be aware that also other apps would be able to manage your "send text action", so android will show an app chooser to the user, you'll not be able to switch between the two apps seamlessly.
Reference here
I'm building an Android application that needs to have customer specific code. Customer specific code needs to be separated from the actual Android product our company supplies.
To do this I tried to create 2 packages:
com.company.product.activity
com.company.product.customcode.activity
Both packages contain ExampleActivity.
I have written a factory that uses reflection to determine of a custom component exists on top of the product class. This works fine.
Using the following code to start the ExampleActivity of product works:
Intent intent = new Intent(this, com.company.product.activity.ExampleActivity.class);
startActivity(intent);
Using the following code to start the ExampleActivity of customcode fails:
Intent intent = new Intent(this, com.company.product.customcode.activity.ExampleActivity.class);
startActivity(intent);
Error:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.product.customername/com.company.product.customcode.activity.ExampleActivity}: java.lang.IllegalArgumentException: AppIndex: The URI host must match the package name and follow the format (android-app://<package_name>/<scheme>/[host_path]). Provided URI: android-app://com.company.product.customcode.activity/http/host/path
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
I also tried this code, but then Android gives a Toast that it cannot find the Activity:
Intent intent = new Intent();
intent.setClassName("com.company.product.customcode.activity", "com.company.product.customcode.activity.ExampleActivity");
startActivity(intent);
Manifest:
<activity android:name="com.company.product.activity.ExampleActivity"
android:label="#string/app_name"
android:noHistory="false"
/>
//Custom implementation of the ExampleActivity
<activity android:name="com.company.product.customcode.activity.ExampleActivity"
android:label="#string/app_name"
android:noHistory="false"
/>
Does anybody have any ideas or tips how to achieve the maingoal: To split custom code from the productcode where activity names might be equal.
I am not sure what you missed here. I have tried this and it worked.
I have created a class MainActivity under package com.sample.so_sample.activities
and another MainActivity under package com.sample.so_sample1.test.activities
My manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.so_sample">
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.sample.so_sample1.test.activities.MainActivity">
</activity>
</application>
</manifest>
and my call to navigate in com.sample.so_sample.activities.MainActivity is
Intent i = new Intent(this, com.sample.so_sample1.test.activities.MainActivity.class);
startActivity(i);
There is nothing wrong in the code.I had tried the same,it seems no issue.
Differen package names is the right way, it has to work fine, seem like problem is out of this code.
Your code can be in any java packages, you need just specify fully qulified name of activity in manifest. Android package name it's just a unique app id string.
instead of:
Intent intent = new Intent();
intent.setClassName("com.company.product.customcode.activity", "com.company.product.customcode.activity.ExampleActivity");
startActivity(intent);
try:
Intent intent = new Intent();
intent.setClassName("YOUR PACKAGE NAME IN MANIFEST", "com.company.product.customcode.activity.ExampleActivity");
startActivity(intent);
I've solved this question.
It seemed by further investigation that the Activity crashed because of some auto-generated code that was added while copy-pasting....
It seemed like a crash before the Activity was loaded, but this apparently was not the case.
Thank you all for you're answers, they were very useful!
How can I get the package name of the current launcher in android 2.3 and above programmatically in Java ?
I think you should be able to use PackageManager.resolveActivity(), with the home intent.
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
String currentHomePackage = resolveInfo.activityInfo.packageName;
With the package visibility changes introduced in Android 11, it is now necessary to add a queries element in your application's manifest file as below before you can query the PackageManager.resolveActivity(intent:flags:) method for the default home (a.k.a. launcher) activity that is installed on the device:
<queries>
<intent>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
</intent>
</queries>
If this queries element is omitted from your application's manifest, then the device will report the com.android.settings.FallbackHome activity as its default home activity and that is most likely not what you want.
For guidance on how to query the PackageManager.resolveActivity(intent:flags:) method, see the accepted answer in this thread.
in general, I agree to #JesusFreke using the PM resolveActivity
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
but to get the right package name, you should use
resolveInfo.loadLabel(packageManager).toString()
or
resolveInfo.activityInfo.applicationInfo.loadLabel(packageManager).toString()
Hint:
if there is no default set, this might become "Android System" or "open" as for the general System Intent Receiver
Hint:
if you're looking for web browsers, you might use net.openid.appauth.browser.BrowserSelector#select() (0.7.1+) to implicit get the default browser even there is no one explicit set.
It it possible to start an adobe air apk from my current android app?
I tried this code but I got ActivityNotFoundException:
Intent intent = new Intent();
intent.setAction("air.caarsvcmobile.debug");
startActivity(intent);
Manifest:
<activity
android:name="caarsvcmobile"
android:label="CAARS Video Chat" >
<intent-filter>
<action android:name="air.caarsvcmobile.debug" />
</intent-filter>
</activity>
Is it possible to check package name and class name of an Air app?
After some Googling I found the solution of my problem:
Since I only knew the package name of the apk file I could use this code to launch the default launcher activity.
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("air.caarsvcmobile.debug");
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
Thats all. I works fine.