In an android app I am making, I want to allow users to place shortcuts on their homescreen that will make my app perform certain actions. So far, I made it successfully create the shortcut (they are created through the launcher, not through the app directly). However, when I click on them, it says
Application isn't installed on your phone
Which I assume means I have created the shortcut wrong. The code I am using to create the shortcut is:
Intent shortcutIntent = new Intent(ShortcutActivity.this, com.example.myapp.MyActivity.class);
ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(ShortcutActivity.this, R.drawable.clean);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
setResult(RESULT_OK, intent);
Solved it myself. Realized I had to add the following to the activity in the manifest:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
Related
I am trying to create a new application ("My New App") from the source of another application that I have ("My Previous App").
In order to do it, I've created a new flavor, changed the app name and new icons for the new flavor, and everything goes ok.
But I've encountered that I can't launch (at least) one activity.
The activity is declared in a library included as a module and is defined in this way in the library manifest.xml:
<activity android:name="com.my.company.core.views.download.VersionActivity"
android:label="#string/app_name"
android:exported="false"
android:screenOrientation="portrait" />
And I am trying to launch that activity in the following way:
Intent intent = new Intent(CurrentActivity.this, VersionActivity.class);
startActivity(intent);
If I have only one app, for example "My New App" installed in the terminal it works perfect. But if I have installed both apps "My New App" and "My Previous App" when I try to launch the activity as described Android says "No application can perform this action".
Can't I share code between apps? Is there a problem with the declaration of the activity in thee manifest? By the way, I've tried with exported="true" but it didn't work anyway.
Thanks in advance.
You can specify that your current app should handle the intent using setPackage.
Intent intent = new Intent();
intent.setAction("com.my.company.core.views.download.VersionActivity");
intent.setPackage(context.getPackageName());
startActivity(intent);
I need to add a shortcut for my app on the home screen (programmatically).
I know that the app store do this by default, but for start, the app won't be on the google app store.
I searched a lot, and found basically the same lines of code over and over, and it doesn't seem to work for me.
the code I used:
in the manifest:
<activity android:name=".MainScreenActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
in the onCreate method I called the function that does the following:
private boolean createShortcut()
{
//create shortcut intent
Intent shortcutIntent = new Intent(getApplicationContext(),MainScreenActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
//create intent to add and define the shortcut
Intent addingIntent = new Intent();
addingIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,shortcutIntent);
addingIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"SenseGuard");
addingIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.drawable.peak_detection_icon));
addingIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addingIntent);
}
I tried switching "getApplicationContext()" to "this".
I tried with an actual tablet and on an emulator but I can't get it to work.
Do like This:
Step 1:
Update your manifest.xml :
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Step 2:
in your MainActivity.java create addShortcut() method and in it`s block put this code :
private void addShourcut(){
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 , "Convertor");
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);
}
Step3:
set onClickListener for your view that be create shortcut :
img_pin = (ImageView) findViewById(R.id.img_pin);
img_pin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addShourcut();
Toast.makeText(MainActivity.this, "shortcut created !", Toast.LENGTH_SHORT).show();
}
});
This is worked for me ...
happy codinngggg...:)
That code isn't guaranteed to work. That broadcast is also sent by ShortcutManagerCompat (which you should probably be using instead of manually sending the broadcast).
However, there are two problems with this.
Your default launcher isn't guaranteed to listen for this broadcast. Nova Launcher, for example, has this behavior disabled by default. Other launchers might not even listen for that action at all.
On Android Oreo (26) and above, this won't work how you expect it to (read the comments on the method I linked for more details).
You can use this logic still and hope that it works for some of your users, but keep in mind that many default launchers no longer even have app drawers, so adding a shortcut could give your users duplicate icons. Also, I know that, at least for me, I have my home screen organized how I want, and if I install an app, it would be really annoying for it to add itself to my home screen.
If you are using the default AOSP launcher (or a close fork), however, and it isn't working, make sure you add this to your manifest:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
I'm new to android and am trying to make an application that when the user presses a particular button in App A, he is sent to App B. The user can then come back to App A by pressing another button in App B. No content is transferred from one app to another.
I want to accomplish this by making custom intents for both the applications. How should I start with this? Also what exactly is Broadcastreceiver and do I need to use it for the above mentioned problem?
Thanks!
Switching between another Application can be by two ways that is
1.) If you know the MainActivity of the Application to Call, you use
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName(
"package_name","package_name.MainActivity"));
startActivity(intent);
2.) If you don't know the MainActivity to Call you just use PackageName, you use
Intent LaunchIntent = getPackageManager()
.getLaunchIntentForPackage("package_name");
startActivity(LaunchIntent);
I don't think you need a BroadCastReceiver here as it is what you use when you want to catch some event/action for eg- Low Battery. For further details check my answer here
See Code to launch external app explicitly (especially this answer). You'll have to create a custom intent for each of the applications, and then call that intent explicitly.
In App A Manifest:
<intent-filter>
<action android:name="com.mycompany.APP_A" />
</intent-filter>
In App B Manifest:
<intent-filter>
<action android:name="com.mycompany.APP_B" />
</intent-filter>
In App A Button Press:
Intent intent = new Intent();
intent.setAction("com.mycompany.APP_B");
startActivity(intent);
In App B Button Press:
Intent intent = new Intent();
intent.setAction("com.mycompany.APP_A");
startActivity(intent);
As you know when you long press on the home screen, the phone shows up a list menu.
You can add shortcuts, widgets, Folders, etc. I would like my app to be in the shortcut list.
How can I do that?
Shortcuts had existed since API level 1, and can be used by 3rd party apps as well.
To add an activity to the shortcuts manu simply add this intent filter to your activity in your manifest:
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
To make the activity plant a new icon with an intent on the home screen, do this before finishing:
Intent intent = new Intent();
Intent launchApp = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchApp);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "My shortcut");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, myIcon);
setResult(RESULT_OK, intent);
Change the launchApp intent to whatever you want launched when this is clicked.
See here: http://developer.android.com/reference/android/content/Intent.html#ACTION_CREATE_SHORTCUT
That functionality is already there by default, they just have to choose from the "Applications" list. You can't add directly to the main shortcuts list (it would get far too cluttered quickly), that's provisioned by the operating system and to my knowledge cannot be populated by a third party application.
I have an app that allows you to create Home "shortcuts" to a specific Activity. It turns out that some of my users will use the app, hit the home key to go do something else, then use one of the shortcuts to jump back to that Activity. Since the app is still in memory it just opens the new Activity on top of the others and the "Back" key will take them back through the whole history. What I'd like to have happen is if they use a shortcut then to effectively kill the history and have the back key just exit the app. Any suggestions?
First, set up the taskAffinity in the Manifest to make the Activity run as a different "task":
<activity
android:taskAffinity=""
android:name=".IncomingShortcutActivity">
<intent-filter>
<action android:name="com.example.App.Shortcut"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
then, when building the shortcut, set the FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TOP flags. Something like:
// build the shortcut's intents
final Intent shortcutIntent = new Intent();
shortcutIntent.setComponent(new ComponentName(this.getPackageName(), ".IncomingShortcutActivity"));
shortcutIntent.putExtra(EXTRA_STOPID, Integer.toString(this.stop_id));
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// Sets the custom shortcut's title
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, custom_title);
// Set the custom shortcut icon
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.bus_stop_icon));
// add the shortcut
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);
Try adding Intent.FLAG_NEW_TASK to the Intent.