Start activity from another package

淺唱寂寞╮ 提交于 2019-12-07 03:32:14

问题


I have this manifest:

<manifest ...
    package="com.my">

    <application ...>

        <activity ...
            android:name=".app.Run">
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    <activity ...
    android:name=".app.Preferences"/>

    <activity ...
    android:name=".library.error.ErrorDialog"/>

    </application>

</manifest>

How can I start ErrorDialog activity from Run activity?

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.my.library.error", "com.my.library.error.ErrorDialog"));
startActivity(intent);

Or

Intent intent = new Intent();
intent.setComponent(new ComponentName("library.error", "library.error.ErrorDialog"));
startActivity(intent);

Not working


MEA CULPA... MEA CULPA...

My ErrorDialog Activity was not public. :D


回答1:


1. Related to Application Manifest File

Manifest: 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  package="app.run"/> 
<activity android:name="app.run"/> 
<activity android:name="library.error.ErrorDialog"/>

package app.run  // Your Main Application Package Name

Activity:
Intent i = new Intent(); 
i.setClassName("app.run", "library.error.ErrorDialog"); //
startActivity(i); 

setClassName()


2. Not related to Application Manifest File

Intent intent = new Intent();
intent.setComponent(new ComponentName("packagename whos activity u want to launch","classname.java"));   
startActivity(intent); 

setComponentName()

In your Case

Intent intent=new Intent();
intent.setComponent(new ComponentName("library.error", "library.error.ErrorDialog")); 
startActivity(intent);



回答2:


After creating two different applications(packages). Go to the manifest file of your first application and edit it as follows:--

<activity
android:name="com.example.applicationfire.MainActivity"
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.application2.Second"
android:label="@string/app_name" >
</activity>

Here just declare the activity from the second application which you want to open from the first one. Please notice that here, "com.example.application2" is the package name of another application and "Second" is the name of the activity which is in the second package.

And the intent which you would fire for another activity in another application to start would be something like this --

btnStart.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View arg0) 
        {
            // TODO Auto-generated method stub
Intent intent = new Intent("android.intent.action.MAIN");
intent.setComponent(new   ComponentName("com.example.application2","com.example.application2.Second"));
startActivity(intent);
}       });}

Now, here the first arguement which is passed into intent would be the package name of your second application and the second arguement would be the name of the activity to be opened. That's it. PS: Run the first app!




回答3:


I fixed the issue with the Intent and setClass

Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setClassName("packagewheretheactivityyouwantcallis", "packagewheretheactivityyouwantcallis.ActivityYouWantCall");
            startActivity(intent);

Don't forget to put into the Manifest of the activity you are writing the activity you want to call like this:

<application 
....>
 <activity
        android:name="packagewheretheactivityyouwantcallis.ActivityYouWantCall">
    </activity> 
</application>



回答4:


You can just use this code:

Intent myIntent= new Intent(FirstActivity.this,SecondActivity.class); 
startActivity(myIntent);

It doesn't make any diffirent if a class is in the same package of in another package. Just be sure you import the Class.

I used this code in my manifest:

<activity
    android:name="com.mycompany.mainapplication.package1.SecondActivity"
    android:label="Simple Math Questions" >
</activity>


来源:https://stackoverflow.com/questions/14423617/start-activity-from-another-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!