launch activities from different package - android

I have activity A in package one, and I want to run an intent which will up an activity B which is in package two.
How can I do this? Any samples will be welcome.
this is what ive done, and the error i get:
first activity ("MainActivity") in a package: com.abelski.currencyclient
and second activity("SecondActivity" in a diffrent package: com.idan.second
now i wanna call from MainActivity to SecondActivity.
ive added this line at the manifest of the MainActivity:
<activity android:name="com.idan.second.SecondApplicationActivity"></activity>
now in main Activity i got this button which run this line:
Intent intent = new Intent(MainActivity.this,SecondApplicationActivity.class);
and this is the rror:
04-29 09:20:59.197: ERROR/AndroidRuntime(399): Uncaught handler: thread main exiting due to uncaught exception
04-29 09:20:59.276: ERROR/AndroidRuntime(399): java.lang.NoClassDefFoundError: com.idan.second.SecondApplicationActivity
04-29 09:20:59.276: ERROR/AndroidRuntime(399):

I am assuming that by "packages" you mean applications.
We have:
- ApplicationA with FirstActivity
- ApplicationB with SecondActivity
If, in the ApplicationB's AndroidManifest.xml file, in the declaration of SecondActivity you add an intent filter such as:
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="applicationB.intent.action.Launch" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
You can create an Intent to launch this SecondActivity from the FirstActivity with:
Intent intent = new Intent("applicationB.intent.action.Launch");
startActivity(intent);
What this all means is:
The SecondActivity has a filter for the intent action of "applicationB.intent.action.Launch"
When you create an intent with that action and call 'startActivity' the system will find the activity (if any) that responds to it
The documentation for this is at: https://developer.android.com/reference/android/content/Intent.html

I had the same problem and the solution was another level in the root of your package name.
If you have two packages "com.first...." and "com.second...", and the manifest is referencing "com.first". Then you can refactor both packages in order to reuse the first part. For instance, "com.package.first" and "com.package.second". Then your manifest has to be also updated
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package">
...
<activity android:name=".first.FirstPackageActivity" android:label="FirstPackageActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".second.SecondPackageActivity" android:label="SecondPackageActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Your java code can create an intent and start the activity in the usual way:
Intent intent = new Intent(this,ActivityClassName.class);
startActivity(intent);

If the package you mentioned here is same to application,I think the answer in the question Android: Starting An Activity For A Different Third Party App is simpler。With the first answer to that question, you don't need to make any modification to your second application.

Use explicit intents:
Intent intent = new Intent(context,ClassName.class);
where ClassName is from another package.
Sometimes, you will not know the name of the class in such cases you will have to rely on the Intent that the target class advertises to handle.

First, you need to declare both packages and activities on Manifest :
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
and for the second activities, on android:name --> .Packages name.activity, assuming your second packages name com.iden.second :
<activity android:name=".second.SecondActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
<intent-filter>
<action android:name="android.intent.action.SECOND" />
<category android:name="android.intent.category.SECOND" />
</intent-filter>
</activity>
then on the MAINACTIVITY JAVA CLASS, asuming you will start second activity using a button :
public class MainActivity extends AppCompatActivity {
private Button mButtonSecond;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonSecond = findViewById(R.id.btn_second);
mButtonSecond.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent newActivity = new Intent(MainActivity.this,com.iden.second.SECOND.class);
startActivity(newActivity);
}
});
}
}
hope that can help, since i am not clear with the question structure

Related

Calling Main Activity using Implicit Intents (specifying action and category)

I'm trying to call main activity using implicit intents. I give both action and category in intent but before starting the activity android system gives me a list of applications to select from for opening the activity.
Code snippet I am using to call the main activity follows:
protected void initiateActivity(int requestCode, String value, String oper) {
Intent i = new Intent("android.intent.action.MAIN");
i.addCategory("android.intent.category.LAUNCHER");
i.putExtra("VALUE", value);
i.putExtra("OPER", oper);
startActivityForResult(i, requestCode);
}
It seems to me that every app in system will be having same action, category combo, hence android is giving me that list of apps to select from. What changes can I make to my Main Activity so that this issue is not seen?
Looks like you might need to separate out with intent-filters. It looks there is a good explanation in this post:
How can I start MAIN activity with the help of <intent-filter>?
Which recommends adding the filters such as these below or you will be calling the launcher:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="stateHidden"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="com.package.name.MyAction"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

start an application from another

I'm developing two applications, AppA and AppB, and I want to start AppB from AppA.
In AppA, I am using
Intent initIntent = getPackageManager().getLaunchIntentForPackage("com.example.appB.ActivityB");
in AppB, I am adding an intent filter to the manifest file :
<activity
android:name="com.example.appB.ActivityB"
android:label="#string/title_activity_init" >
<intent-filter>
<action android:name="com.example.appB.ActivityB" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
But I got a java.lang.NullPointerException on the intent...
Any ideas would be welcome.
Your intent filter isn't complete for this API:
The current implementation looks first for a main activity in the category CATEGORY_INFO, and next for a main activity in the category CATEGORY_LAUNCHER. Returns null if neither are found.
So you need to modify Activity B's intent filter to include:
<action android:name="android.intent.action.MAIN" />
You are trying to specify an Activity in the Intent, even though you are creating a Intent meant to launch a package; this is not neccesary. Simply remove the Activity from your Intent like so:
Intent initIntent = getPackageManager().getLaunchIntentForPackage("com.example.appB");
And then just make sure that you have the following two lines in whichever Activity you wish to start when the application is launched:
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
They should be already be in there for your main Activity, although it looks like you were missing the android.intent.action.MAIN intent filter in your ActivityB.

When i use Activity in camel case it gives error but works fine in upper case spelled

Hy! Expets i have two activities
1- MainActivity
2- Startup
I am starting my MainActivity activity through new intent after starting Startup activity using Thread. but when when i call new intent by passing MainActivity spelled in camel case and within inten-filter tag <action android:name="com.example.test.MainActivity" as shown below
Thread timer =new Thread(){
public void run(){
try{
sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent startUpIntent = new Intent("com.example.test.MainActivity");
startActivity(startUpIntent);
}
}
};
and here is AndroidMaifest.xml File code
<activity
android:name="com.example.test.Startup"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.newboston.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.test.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
it gives error.
But when I use code as
Intent startUpIntent = new Intent("com.example.test.MainActivity");
AndroidManifest.xml code
<intent-filter>
<action android:name="com.example.test.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
then it works fine as i want.
I want to know reason to use Upper case spelled instead of camel case.???
Thanx...
You probably want to be defining an intent for a specific component:
Intent startUpIntent = new Intent(Startup.this, MainActivity.class);
Then you don't need to bother with an <intent-filter> for MainActivity in your manifest.
There are a number of action constants defined in the Intent class. Some of them are:
ACTION_MAIN: Start up as the initial activity of a task, with no data input and no returned output.
ACTION_CALL: Initiate a phone call.
ACTION_EDIT: Display data for the user to edit.
To use ACTION MAIN in your manifest, you will replace the ACTION_ part with android.intent.action.. Since they are constants defined in a class you are using, they need to be used as defined.
For example, the following two declarations are not the same. They define and instantiate two different variables:
int myVariable = 1;
int MYVARIABLE = 1;
So, you cannot write:
<action android:name="android.intent.action.MAIN" />
as
<action android:name="android.intent.action.main" />
or anything other that the former.
From the android developers resource page on <action>:
To assign one of [standard actions defined in the Intent class] to
this attribute, prepend "android.intent.action." to the string that
follows ACTION_. For example, for ACTION_MAIN, use
"android.intent.action.MAIN" and for ACTION_WEB_SEARCH, use
"android.intent.action.WEB_SEARCH".
To read up more on this:
Intent
action

Android Activity / TabActivity

The Android app I am building uses a library that is constantly pushing data to a remote server. In a demo MyMainactivity did exactly this without any user interface and the activity worked well.
Now that I am building the UI around this activity with a TabView I am puzzled how to execute MyMainActivity - in my manifest I now have MyTabActivity as LAUNCHER so how can I make MyMainActivity and MyTabActivity both start up on launch? (MyMainActivity should run while the user is able to scroll through the tabs ans at a later stage should have influence on how MyMainActivity sends data to our servers).
<activity
android:name=".MyTabActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You can put the MyMainActivity in the first visible tab, so that it gets started when the tab is becoming visible.
Or you can launch MyMainActivity start what you need to do and then forward your app to the MyTabActivity.
Every activity with a GUI has an onCreate method that you override to define what happens when the activity is launched. You would add the code to run background service when MyTabActivity is created.
class MyTabActivity extends Activity {
protected void onCreate(Bundle savedInstanceState){
Intent myIntent = new Intent(getApplicationContext(), MyMainActivity.class);
startService(myIntent);
setContentView(viewid);
}
}
You can try by this.
<activity
android:name=".MyTabActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity
android:name=".MyMainactivity " >
</activity>
</activity>
Another way you also try by this way in your tab activity. Here, you use your stuff.
<receiver android:name=".AutoConnection" >
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver
public class AutoConnection extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {

Unable to find explicit activity class in android

I have an android project with several packages. The structure of the packages in this case is com.siva.restorative is the package that contains the activity I want to run.
My activities are declared in my manifest as
<activity android:theme="#style/YtdTheme" android:name="com.siva.restorativecare.RestorativeCare">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
The mainScreen activity displays fine, since it is inside the com.WAPP package. But when I try to run the setLocationActivity, I get the unable to find explicit class error. Here is how I have the intent parameters:
Intent i = new Intent(this,SecondActivity.class);
I guess first you should change the action string in manifest as:
<action android:name="com.siva.restorativecare.RestorativeCare" />
and then start running your activity as:
Intent i = new Intent("com.siva.restorativecare.RestorativeCare");
startActivity(i);

Categories

Resources