0

I have two apps on an Android device:

My app, called "app1", and a separate app called "app2" with no uses-permission element set.

I am merely trying to invoke app2's activity via app1.

When I try from adb, the activity launches just fine as in:

am start -n com.test.app2/.Special

Is there a way to code an app in Eclipse to do something similar to am?

I've tried coding app1 to call my own .class file which starts an intent with the same detail, as in:

Intent app1intent = new intent(); 
app1intent.setComponent(new ComponentName("com.test.app2","com.test.app2.Special"));
startActivity(app1intent);  

But when I try that all I see is the same white screen from where app1 was launched.

So instead, I figured I would just call app2's .class file directly via the manifest file.

Here is the AndroidManifest.xml from app1.

I suspect I am missing some additional files as part of my package, as this manifest is merely based on one of the sample apps that came with Eclipse, but could use some help.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.app1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.test.app2.Special"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
jww
  • 97,681
  • 90
  • 411
  • 885

2 Answers2

0

Try this instead:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.test.app2", "com.test.app2.Special"));
startActivity(intent);
ipavl
  • 828
  • 11
  • 20
  • I've tried that, Unable to but I get this: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.test.app1/com.test.app2.Special}: java.lang.ClassNotFoundException: com.test.app2.Special – user4115444 Oct 18 '14 at 22:10
  • Does `com.test.app2` have a class named `Special`? As in `com/test/app2/Special.java`. – ipavl Oct 19 '14 at 01:47
  • Yes, it contains Special.java.I'm getting closer. I now get this error: java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.MAIN cat[android.intent.category.LAUNCHER] cmp=com.test.app2/.Special } from ProcessRecord {41530450 553:com.test.app1/u048} (pid=838, u=10048) not exported from uid 10045 – user4115444 Oct 19 '14 at 16:01
  • The manifest for app1 now looks like this: – user4115444 Oct 19 '14 at 16:03
  • Here's MainActivity for app1: public class MainActivity extends Activity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bClock = (Button) findViewById(R.id.button1); bClock.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent i = new Intent(Intent.ACTION_MAIN); i.addCategory(Intent.CATEGORY_LAUNCHER); i.setComponent(new ComponentName("com.test.app1", "com.test.app1.Special")); startActivityForResult(i, 0); } }); } } – user4115444 Oct 19 '14 at 16:05
  • So app1 launches just fine, but when button1 is clicked, that's when I see Permission Denial: starting Intent........in logcat. In app2's manifest, there are no use-permission elements nor is android:exported used. Altogether, app2 is not my app, but because it doesn't enforce permissions, I'm trying to launch its activity similar to the way I did it via am start -n as mentioned at the beginning of this question. I can only assume that worked because adb runs as root, but I would think it would still work outside of adb (via this custom app1) because of the lack of permissions on app2. – user4115444 Oct 19 '14 at 16:08
  • Slight edit for the above MainActivity class for app1. It reads like this instead: public class MainActivity extends Activity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bClock = (Button) findViewById(R.id.button1); bClock.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent i = new Intent(Intent.ACTION_MAIN); i.addCategory(Intent.CATEGORY_LAUNCHER); i.setComponent(new ComponentName("com.test.app2", "com.test.app2.Special")); startActivityForResult(i, 0); } }); } } – user4115444 Oct 19 '14 at 23:10
  • `not exported from uid 10045` means that the activity needs to be exported. See also: http://stackoverflow.com/a/19829733/4088425 – ipavl Oct 24 '14 at 02:06
  • below are two points need to remember 1. add exported = true for the activity which is going to launch by application2 2. set the absolute path of the class while providing the class name in component . – Krishna Jul 02 '16 at 12:52
0

I have the following working in my apps:

app1's code in an Activity:

Intent launchIntent = new Intent("com.app2.SOMETHING");
startActivity(launchIntent);

app2's AndroidManifest.xml, within the Activity you want to launch:

<intent-filter>
    <action android:name=com.app2.SOMETHING" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Bonus:

To check if app2 is installed on the device:

 boolean isAppTwoInstalled = false;
 PackageManager packageManager = getPackageManager();

 try {
     PackageInfo packageInfo = packageManager.getPackageInfo("com.app2", PackageManager.GET_ACTIVITIES);
     isAppTwoInstalled = true;
 } catch (Exception e) {
     // not installed
 }
BVB
  • 5,380
  • 8
  • 41
  • 62
  • Please include the full .java file and app1's manifest file. It seems that logcat continues to show this when I include the full path to app2's activity in the manifest file: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.test.app1/com.test.app2.Special}: java.lang.ClassNotFoundException: com.test.app2.Special ..........Alternatively, if I just reference app1's MainActivity for the activity name, I just see a blank white window as rendered by app1 even though I really want to launch app2's activity via the MainActivity.java file. – user4115444 Oct 19 '14 at 00:30
  • I don't use any ComponentInfo instances in my example. Please see how I set up the intent. I am unable to include the full .java file. – BVB Oct 19 '14 at 18:05