I am using a Project A, and a library project to develop my Android app. Both of these projects have activities in them. If I am currently in Project A, then it is easy to just start an activity in the library project by just importing it. However, I'm not sure how to start an Activity in Project A if I am coming from an activity in the library project. I'm trying to make the library project independent of the package name of the Project A since I will be using it for multiple apps. Any ideas on how to do this? Thanks.
There are a few possible solutions.
The best is to register an intent filter in the Manifest entries for the activities that you wish to be accessible to your other projects. There is a great tutorial on intent filters here.
Another option would be to pass a Class to the library project, and use the Intent (Context packageContext, Class cls) constructor to create an intent to fire off and start the activity. It would be a better practice and learning experience to use intent filters, however.
Good luck!
I believe this is to be the simplest answer: you'll need an object which exists in the library, which you can extend in your project.
Imagine that your library has a LibraryApplication which your ProjectApplication extends. The LibraryActivity can call:
((LibraryApplication)getApplication()).startNewActivity(this, "goHome")
Your ProjectApplication implements this new method:
public void startNewActivity(Context context, String action) {
if("goHome".equals(action)) {
startActivity(context, ProjectHomeActivity.class);
}
}
You can add project as external library. Also you can use maven for this kind of things.
From a simple and good answer:
try {
startActivity(new Intent(this, Class.forName("com.package.HomeActivity")));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
And also is possible with this answer, using actions.
Related
I have two applications, one works as a main app, and the another as a service.
In the service app manifest file, I configure a service like:
<service android:name=".services.FirstService">
<intent-filter>
<action android:name="ch.service.action.FIRST_SERVICE"/>
</intent-filter>
</service>
And in the main app, I start a service like:
Intent intent = new Intent("ch.service.action.FIRST_SERVICE");
startService(intent);
Here, I have to duplicate "ch.service.action.FIRST_SERVICE".
How can I avoid this?
How can I share some common constants, for example define one in service app, and can be retrieve in main app?
Any answer is appreciated.
You should use SharedPreference between both of your application. In your Service application you should write the String ch.service.action.FIRST_SERVICE to SharedPreference :
SharedPreference sharedPreferences = getSharedPreferences("any_name", Context.MODE_WORLD_READABLE);
SharedPreference.Editor mEditor = sharedPreferences.edit();
mEditor.putString("service_string","ch.service.action.FIRST_SERVICE");
mEditor.commit();
When you want to get the value from other application, you first need to get Context of your service application :
Context mServiceAppContext = createPackageContext("com.example.service_app_package", 0);
You can use mServiceAppContext to get the String value :
SharedPreference sharedPreferences = mServiceAppContext.getSharedPreferences("any_name", Context.MODE_WORLD_READABLE);
// Context.MODE_WORLD_READABLE is important, it makes the value readable across other applications
sharedPreferences.getString("service_string", /* default value */ "service_string","ch.service.action.FIRST_SERVICE");
Best Explaination is given at : http://thedevelopersinfo.com/2009/11/25/getting-sharedpreferences-from-other-application-in-android/
for something like this you have to create a library project and include it to both your main and service application and define your name in the library you included. Hope this helps, not sure if there is any other better way :)
If you can merge two applications together, certainly you can share anything you want. Actually one application can contains many service and intent service.
If you have some reason to separate one application into two, or they are two irrelevant apps, using intent to communicate always need class name which is unavoidable I think.
There are different ways to archive the same thing, but why not keep it simple:
Do you have a reference to the service project? if it so, why not just create a setting in the service project and reuse it on the main one when create the intent:
Intent intent = new Intent(cxt.getString("service_string", default value));
or, like I did in my apps, just create a public static final variable on the service class and access it from the main app?
public static final String ACTION_FIRST_SERVICE = "ch.service.action.FIRST_SERVICE";
If you don't have a reference to the service project and don't want to add, the best way is to expose it to anyone ... or create a lib project that both use
I have 2 projects in Android Studio . I want to launch the activty of one of these projects in an other.
I searched a lot. Some says to create the library project of the project where the activity I want to launch is and then import this library in the other project.
However I don't even know how to create this library project since I want to create it from an existing project.
There is the option 'Import Module' , however i'm afraid it's modifying the whole project.
Help please
Thanks in advance
This is one way to do it
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.exp.yourpackage");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
This will call the launcher activity of the other application. It is possible to start any activity you want with that method
I don't know why but the solution
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.exp.yourpackage");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
didn't work for me. Maybe I used it in a wrong way, I don't know.
What works is using
<intent-filter>
in the manifest file of the app we want to trigger and then call the action name of the
<intent-filter>
in the main application.
All details here : http://hmkcode.com/android-start-another-activity-of-another-application/
I am trying out the Atooma SDK. They provide an abstract class IntentBasedTrigger for creating triggers that fires when the intent fires. The class has an abstract method
public String getIntentFilter() throws RemoteException
In the docs (http://www.atooma.com/developers#start/trigger) They use this with a standard Android intent. However, I want to use it with a custom Intent, so I made up a string and returned it from getIntentFilter. However, this does not work (I can't be more specific at the moment) which leads me to believe that defining intents is a bit more complicated?
IntentBasedTriggers works with custom Intent as well.
I am part of the Atooma team, unfortunally we don't have a plugin sample with IntentBasedTrigger, but you can see the SnapBack plugin using it
Here is the source code
https://github.com/SnapbackLabs/AtoomaPlugins/blob/master/AtoomaBlowDetectionPlugin/src/com/atooma/plugin/blow/TR_BlowDetectionSensorBased.java#L92
I've added a reference to a new module and try to open an Activity from it.
It throws an Exception that says:
android.content.ActivityNotFoundException: Unable to find explicit
activity class{
com.giljulio.imagepicker.ui/com.giljulio.imagepicker.ui.ImagePickerActivity
};
have you declared this activity in your AndroidManifest.xml?
Do I need to add anything else beside reference the new module?
You have to define in gradle dependencies(in module where you want to call another module activity):
dependencies{
...
compile project(':yourModuleName')
...
}
After adding this sync the gradle and now can you use the activity in the module.
User like this. This will help you
Intent intent = null;
try {
intent = new Intent(this,
Class.forName("ir.test.testlibary1.HelloWorldActivity"));
startActivity(intent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Ok, so I am a few years late.
The issue is not that you don't have the gradle dependency as #arpit suggested, it seems you report a runtime exception. Also, what #Aman suggested will help with the handling the exception, it will just not help you start the activity.
If understood correctly, you have a multi-module app (let's say A-app module and B-lib module) and need to call another lib-module(C) from one B.
If that is the case, you need to declare that library's activity(C) in your module's(B) Manifest.xml, inside the tag.
If it has not already been setup, you also need to enable manifest merger.
Ok,
So I'm making a library project of UI elements. The Library has some activities which are based of ActionBarSherlock which is a backwards compatibility library for the action bar in android. In these activities I would like to have a button in the action bar which will take the user home regardless of which activity they are using in the Library project.
Some terminology. The 'library' refers to the Android UI library project I'm working on. The 'Application' refers to whatever customer a developer might be using with the Library included.
Usually, when you make an activity and you want to call another, you would do something like this.
intent = new Intent(this, WhateverMyActivityName.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Simple enough. But here's the tricky bit. Android Libraries have little to no knowledge of what application is using them. So 'WhateverMyActivityName.class' is useless as there is no way to predict what the developers application will call their activities.
I need to replace
intent = new Intent(this, WhateverMyActivityName.class);
with something like this
intent = new Intent(this, getApplication().MainActivity().getClass());
or possibly use some sort of intent action which will call the main Activity in the application (Intent.ACTION_MAIN or Intent.CATEGORY_LAUNCHER)
So in short: How do I get an applications main activity from a library project?
We can use reflection to get class object.
Class.forName("com.mypackage.myMainActivity")
Add this code in Library project to call,
try {
Intent myIntent = new Intent(this,Class.forName("com.mypackage.myMainActivity"));
startActivity(myIntent );
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
"com.mypackage.myMainActivity" is the Activity present in Main project, that we need to call from its Library project.
The application calls some method in your library providing the Intent to be invoked, or providing the Class of the activity to be invoked. Your library stores that someplace and uses it.
Your assumption that the right answer is "Intent.ACTION_MAIN or Intent.CATEGORY_LAUNCHER" may be inaccurate. For example, some apps have that be a splash screen activity (which is an issue in its own right, but that's beside the point), and that would not be where a home affordance within the app should go.
You can get the list of activities using the code mentioned in the this post. After that loop through the resolveinfo and check the intenet filter to find the activity with your desired intent action.