I have 2 projects in Eclipse. When I click on a button in project1, I want it to launch an activity from project2.
how can I do that ?
i tried the following code
Intent intent = new Intent(this,com.project2.tp02.MainActivity.class);
intent.setClassName("com.project2.tp02", "com.project2.tp02.MainActivity.class");
startActivity(intent);
I get an error message telling that first application stopped. How can I lauch it properly without such error?
Thanks in advance to any helper.
I got it work using this :
Intent intent = new Intent();
String pkg ="com.project2.tp02";
String clazz =pkg + ".MainActivity";
intent.setComponent(new ComponentName(pkg, clazz));
startActivity(intent);
I don't understand why that way it works and not previous one, if someone can explain I will be thankfull, but anyway I got my problem solved that way.
This is working code to invoke an activity which is in another project.
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.my.package", "com.my.package.MyClass"));
startActivity(intent);
I think your first code did not work because you provide your current activity as context of invoked activity where it is in another application.
<intent-filter>
<action android:name="com.project2.tp02.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Adding the above lines in the Manifest of your first activity might solve your problem.
It would be interesting what the error message is saying exactly... you do need the intent-filter as described by Swayam on the activity you want to call.
Try
new Intent("com.project2.tp02.MainActivity");
to get the intent, instead of setting the class name.
Related
I have a confusion with how android starts its launcher activities.
If I am declaring a launcher activity in the manifest file like this
<activity android:name=".Activities.Home">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and I have an Application class which calls an activity based on a check like
if(ParseUser.getCurrentUser() == null){
Intent intent = new Intent(context,Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}else{
Intent intent = new Intent(context,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
which one has the precedence ? The manifest or the Application. What is the flow of events ? e.g. Application->Manifest (or) Manifest->Application (or) Application overrides Manifest ?
If I am receiving a notification in Android, my Application class is called. This makes the activities in the application class to be started, like shown above. Is there a way to detect who calls the Application class ? I mean whether the user starts it, or it starts from the notification which comes in ?
If there is a way to figure this out. How do I prevent the activity in the Application class to be called when I receive a notification ?
Thanks in advance.
Just pass the boolean extras from application and make the diffrence in the call from Application class and User Launch.
Intent intent = new Intent(context,Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("APP_CALL",True);
startActivity(intent);
Now in your Home activity Just check for APP_CALL if it is called from Application class then bool value will be true else false.
Also you can write in splash activity for checking login .
I got log cat message from startAnotherActivity() method
private void startAnotherActivity() {
Log.i(TAG, "Entered startAnotherActivity()");
Intent intent = new Intent();
intent.setAction(ANOTHER_ACTIVITY);
intent.addCategory("android.intent.category.DEFAULT");
startActivity(intent);
}
Another activity doesn't start, no other messages in log cat.
How can I resolve this issue?
UPDATE#1:
Sorry, I forgot to mention that AnotherActivity is an Activity in the other application, and therefore ANOTHER_ACTIVITY == 'some.other.app.domain.ANOTHER_ACTIVITY'
Shouldn't Dalvik complain if it cannot find specified activity?
One possible reason may be not declaring other activity in the manifest. You can do this like the following:
<activity android:name="your.package.your.activity">
</activity>
And then you can start the activity by doing the following:
Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
startActivity(intent);
Hope this helps.
Since it's an activity in another application, you may need to set the component (fully qualified package name and fully qualified activity name).
See here: How to start activity in another application?
Or here:
Launch an application from another application on Android
Finally I found out my mistake.
In the project there are two similar messages in two activities, so I thought that runs one, but that was another.
Thank you for your assistance!
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);
I am looking to start an activity in my app using a custom action. I have found a few answers but everything I try it throws java.lang.RuntimeException saying No Activity found to handle Intent { act=com.example.foo.bar.YOUR_ACTION }.
This is the activity in my manifest file:
<activity
android:name=".FeedbackActivity" >
<intent-filter>
<action android:name="com.example.foo.bar.YOUR_ACTION" />
</intent-filter>
</activity>
And this is how I'm starting the activity:
Intent intent = new Intent("com.example.foo.bar.YOUR_ACTION");
startActivity(intent);
Any help would be greatly appreciated.
I think what you need is to add a default category to your intent-filter,
eg.
<activity
android:name=".FeedbackActivity" >
<intent-filter>
<action android:name="com.example.foo.bar.YOUR_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
see this answer for more info.
I think you are creating your intent wrong. Try like this:
String CUSTOM_ACTION = "com.example.foo.bar.YOUR_ACTION";
//Intent i = new Intent(this, FeedBackActivity.class); // <--- You might need to do it this way.
Intent i = new Intent();
i.setAction(CUSTOM_ACTION);
startActivity(i);
Just add and intent-filter category as Default.
Implicit intent works perfectly and in many cases its better to use a implicit intent with Intent-action to call a service/Activity than using class-name.
Before startActivty() / startService() with proper context you cane use this method 'queryIntentActivities(Intent intent, int flags)' from package manager class.
It helps the ActivityManager (responsible for launching activities) to check whether the Android system is getting any match with you Intent.
If it doesn't it returns a list size 0 or else >0.
By this you can also check if your app is getting the call,and in this case even if your app is not installed / has got some problem, it will not crash but will throw a warning in Log. Users will face no big trouble apart from app not being launched.
(users will never forgive you if tour app crashes).
Hope this will help !!!
Happy Coding. :)
I faced the same problem when trying to launch the activity residing in the dynamic feature module and starting through action String as the Activity is not resolvable by name at compile time.
So I set the action but the activity crashes every time (No Activity found to handle intent bla bla.. ) until I set the correct package name.
Context c = getApplicationContext();// flag would be require Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
Intent i = new Intent(action_string);
i.setPackage(context.getPackageName());//this did the trick actually
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
In the Manifest : add catrgory default to the intent filters
from google docs:
<category android:name="android.intent.category.DEFAULT"/>
Note: In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If you do not declare it in your intent filter, no implicit intents will resolve to your activity.
I'm working on an activity which other 3rd parties want to use in their own apps, via intents.
Right now this activity is catching urls via an intent filter, like this:
<activity android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:host="www.mysite.com" android:pathPrefix="/test/" android:scheme="http"></data>
</intent-filter>
</activity>
The above works, whenever a user clicks a link in my app like:
"mysite.com/test/blah.html"
my app comes up as a choice, along with the browser, to open the link.
Now if a third party wants to use my app, I think they can use the above like this:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://mysite.com/test/somedata"));
startActivity(intent);
While this would work, this probably won't give them the desired effect of jumping directly to my activity from theirs - the android chooser dialog will appear, asking if they want to open the intent data with the browser, or my app.
How can I let 3rd parties call my activity directly without broadcasting the intent like this? I'd like to make them still pass the same exact data scheme to me, but just let them open my activity directly.
Thank you
You would most likely need for them to call your activity directly
Class yourClass = Class.forName("com.yourdomain.yourapp.YourClass");
Intent intent = new Intent(this, yourClass);
If they don't have a jar to link against. Otherwise, they could just use
Intent intent = new Intent(this, YourClass.class);
And then put some extras in there. The whole concept of the browsable intent (along with the others) is to provide users with a choice of how they would like to view/use something. This is similar to what happens when you click "share" from the media viewer. The whole concept is to give them choice. If somebody wants to just start your activity, they will need to explicitly call it.
Edit: My reflection example above won't directly work unless the Dalvik class loader knows about your class (which it probably won't). You will actually need to specifically tell the VM to load a class from a foreign package. You can do that with the following code
Context foreignContext = createPackageContext("com.yourdomain.yourapp", Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
Class<?> yourClass = foreignContext.getClassLoader().loadClass("com.yourdomain.yourapp.YourClass");
Now that they have the class object, they can then fire the intent like before. So the complete code is something like
Context foreignContext = createPackageContext("com.yourdomain.yourapp", Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
Class<?> yourClass = foreignContext.getClassLoader().loadClass("com.yourdomain.yourapp.YourClass");
Intent intent = new Intent(this, yourClass);
startActivity(intent);
The solution described above by Chris Thompson didn't work for me. This one did: Android: Starting An Activity For A Different Third Party App
Just in case anyone runs into the same problem as I did.