I have two sperate applications and I want to call start an activity from the second application in the first, here is my code to do so :
Intent intent1 = new Intent(Intent.ACTION_MAIN);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent1.setComponent(new ComponentName("org.two.three.application","org.two.three.application.one));
Context H= context;
H.startActivity(intent1);
And in the android manifest of the project I have this code, I have the line :
<activity android:name=".one">
</activity>
But I keep getting a runtime error, logcat says :
"Unable to find explicit activity class
{org.two.three.application/org.two.three.application.one}; have you
declared this activity in your AndroidManifest.xml?"
Can anyone see my error? The only thing I can think of is my package of the first activity is org.two.three.Class while the second is org.two.three.application.SecondClass. Does this matter?
Thanks in advance
At first try removing those code that you are adding
**
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent1.setComponent(new ComponentName("org.two.three.application","org.two.three.application.one));
Context H= context;
**
Then add following code into an action method like onClick
Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);
Add your Android Manifest configuration file
<activity android:name="NewActivity"></activity>
You just need to make your activity publicly available. To do that just add
android:exported="true"
to the <activity> tag in your manifest.
Normally, activities are not available to other components that are outside of the package. This is the standard default behaviour. But, of course, you can make them available if you want to.
Related
I have an Android application with a button to find other games on Play Store. This button should open the Google Play Store application. I have a working version which uses the startActivity() function and Intent flags, just as mentioned in this official doc.
Intent intent = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("https://play.google.com/store/apps?someapp")
.setPackage("com.android.vending");
this.startActivity(intent)
Is this possible to achieve using Navigation Components instead? I have tried implementing a Navigation Deep Link and to set the equivalent of intent actions and data like so:
<activity
android:id="#+id/playstore_activity">
<deeplink
app:uri="https://play.google.com/store/apps?someapp"
app:action="android.intent.action.ACTION_VIEW"
app:targetPackage="com.android.vending"/>
</activity>
But I am getting an ActivityNotFoundException: No activity found to handle Intent error. What am I doing wrong? I am unable to find any further documentation of this.
I figured it out in the end:
<activity
android:id="#+id/playstore"
android:label="Travel to PlayStore"
app:action="android.intent.action.VIEW"
app:data="#string/playstore_path"
app:targetPackage="com.android.vending"
/>
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 a list adapter that allows the user to launch a new instance of the same Activity by pressing one of the items:
Intent intent = new Intent(mContext, CommentActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(ParseConstants.KEY_OBJECT_ID, commentId);
intent.putExtra(ParseConstants.KEY_SENDER_ID, userId);
mContext.startActivity(intent);
The problem is that I'm already in CommentActivity. If I write (Activity) mContext.finish() before launching the new Intent I will get to where I want to go, but then when the user presses back I return to very first Activity in the stack, which is MainActivity. I want to return the previous CommentActivity. How can I resolve this?
Manifest Entry:
<activity
android:name=".activity.CommentActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/app_name"
android:launchMode="singleTask"
android:parentActivityName=".activity.CommentActivity"
android:screenOrientation="portrait"
tools:ignore="UnusedAttribute" />
When you use the Intent.FLAG_ACTIVITY_NEW_TASK flag you are telling Android to start a new Task (collection of Activities) to contain the new Activity rather than pushing the new Activity onto the stack associated with the current Task.
When the Activity exits, the Task is empty so you return from the root of the Application.
What you want to do is push the new Activity onto the existing stack. This is the default behavior, so remove the
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
line.
Read more about Tasks and Activities here.
I'm just learning to work with Android through a textbook (Learn Android App Development by Jackson). At the moment, I have a MainActivity class. I'm adding Intents to the menu of this activity to launch one of four other activities depending on which option. All activities are in the same package, and all have been declared in the AndroidManifest.XML file.
I am determining which activity to run using a Switch case as follows:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add:
System.out.println("Add");
Intent intent_add = new Intent(this, NewPlanet.class);
this.startActivity(intent_add);
break;
case R.id.menu_attack:
System.out.println("Attack");
Intent intent_attack = new Intent(this, AttackActivity.class);
this.startActivity(intent_attack);
break;
case R.id.menu_config:
System.out.println("Config");
Intent intent_config = new Intent(this, ConfigActivity.class);
this.startActivity(intent_config);
break;
case R.id.menu_travel:
System.out.println("Travel");
Intent intent_travel = new Intent(this, TravelActivity.class);
this.startActivity(intent_travel);
break;
default:
return super.onOptionsItemSelected(item);
}
System.out.println("Outside switch.");
return true;
}
The problem, however, is that this only works when pressing the "Add" menu button, which successfully launches the NewPlanet activity and displays it.
All of the others, however, produce an ActivityNotFoundException and force the program to crash (same result on various combinations of virtual devices, as well as on my physical Galaxy Note II device).
I've done everything I can think of to try to fix this to no avail. As far as I know, the code is identical to that presented in the book, but the book has begun to move onto the next sections while my project is not yet working yet.
I have LogCat output if anybody wants to see that, but any help or advice would be greatly appreciated. I've Googled the issue which did not help much.
EDIT: As requested, here is my Manifest: (I attached it as a high-res images since I'm having trouble with the editor right now)
http://i.imgur.com/c7wJ8bM.png
And here is the relevant LogCat output:
07-03 10:42:53.954: E/AndroidRuntime(29754): FATAL EXCEPTION: main
07-03 10:42:53.954: E/AndroidRuntime(29754): android.content.ActivityNotFoundException: Unable to find explicit activity class {chapter.two.hello_world/chapter.two.hello_world.ConfigActivity}; have you declared this activity in your AndroidManifest.xml?
FINAL EDIT:
I've solved this problem thanks to user E. Odebugg: I was referring to my activities in the manifest by incorrect names (ConfigPlanet instead of ConfigActivity). I simply did not notice the difference. A silly mistake, but it has been fixed now. Thank you everybody for your help!
There might be something wrong with your manifest. Maybe you copied and pasted the same attributes for all Activities and hence have multiple Launcher IntentFilter Categories. Can you post both, the stack trace and the Manifest?
Clearly, the screen-shot you have shared shows that you have declared some ConfigPlanet activity, while in your switch-case u are calling ConfigActivity.
Replace .ConfigPlanet from your AndroidManifest.xml with .ConfigActivity, making sure that you do have a ConfigActivity.java file in your proper package.
You don't have the ConfigActivity declared in your manifest.
Somehow you have an entry there for ConfigPlanet which is not the same. So that's the cause.
Make sure that all your activities are declared in your AndroidManifest.xml.
<manifest ... >
<application ... >
<activity android:name="your.packagename.AttackActivity" />
<activity android:name="your.packagename.ConfigActivity" />
<activity android:name="your.packagename.TravelActivity" />
...
</application ... >
...
</manifest >
For details, see http://developer.android.com/guide/components/activities.html
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.