从另一个应用程序调用应用程序(Intent)

时间:2014-08-26 07:13:32

标签: java android

美好的一天! Noob在这里关于android java。

我一直在搜索整个Stackoverflow并询问很多关于Intent的问题。这可能是我要做的最后一篇文章,因为我已经在StackOverFlow中充斥着关于Intent的冗余问题。

我不想完全被勺子喂,所以我创建了两个快速的android应用程序。一个用于主启动器,第二个用于调用应用程序。我在主启动器上添加了一个按钮,下面是我的代码,该按钮在本教程(https://www.youtube.com/watch?v=qurvm-E9AiU)上似乎是正确的。

public void ClickMe(View v) {
    Intent i=new Intent(this, com.idd.applicationtocall.MainActivity.class);
    startActivity(i);
}

启动后,单击按钮后应用程序崩溃。我不知道什么是错的。我几乎尝试了所有推荐的答案,我认为这些答案都适用于他们。下面是我创建的两个快速应用程序的下载链接。我不希望你完成它,因为我不会从中学到任何东西。我只是想让你们告诉我我错过了什么,我做错了什么或者我对Intent功能的理解不清楚。

https://www.mediafire.com/?wflsmaah5n7x49y

我使用Java Eclipse和BlueStacks作为我的模拟器。

下面是logcat:

08-26 15:22:11.197: E/dalvikvm(13823): Could not find class 'com.idd.applicationtocall.MainActivity', referenced from method com.idd.applicationtolaunch.MainActivity.ClickMe
08-26 15:22:11.197: W/dalvikvm(13823): VFY: unable to resolve const-class 1140 (Lcom/idd/applicationtocall/MainActivity;) in Lcom/idd/applicationtolaunch/MainActivity;
08-26 15:22:11.197: D/dalvikvm(13823): VFY: replacing opcode 0x1c at 0x0002
08-26 15:22:11.227: I/PGA(13823): New SOCKET connection: icationtolaunch (pid 13823, tid 13823)
08-26 15:22:14.897: D/AndroidRuntime(13823): Shutting down VM
08-26 15:22:14.897: W/dalvikvm(13823): threadid=1: thread exiting with uncaught exception (group=0xb2c00180)
08-26 15:22:14.897: I/Process(13823): Sending signal. PID: 13823 SIG: 9
08-26 15:22:14.897: D/AndroidRuntime(13823): procName from cmdline: com.idd.applicationtolaunch
08-26 15:22:14.897: E/AndroidRuntime(13823): in writeCrashedAppName, pkgName :com.idd.applicationtolaunch
08-26 15:22:14.897: D/AndroidRuntime(13823): file written successfully with content: com.idd.applicationtolaunch StringBuffer : ;com.idd.applicationtolaunch
08-26 15:22:14.897: E/AndroidRuntime(13823): FATAL EXCEPTION: main
08-26 15:22:14.897: E/AndroidRuntime(13823): java.lang.IllegalStateException: Could not execute method of the activity
08-26 15:22:14.897: E/AndroidRuntime(13823):    at android.view.View$1.onClick(View.java:3044)
08-26 15:22:14.897: E/AndroidRuntime(13823):    at android.view.View.performClick(View.java:3511)
08-26 15:22:14.897: E/AndroidRuntime(13823):    at android.view.View$PerformClick.run(View.java:14105)
08-26 15:22:14.897: E/AndroidRuntime(13823):    at android.os.Handler.handleCallback(Handler.java:605)
08-26 15:22:14.897: E/AndroidRuntime(13823):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-26 15:22:14.897: E/AndroidRuntime(13823):    at android.os.Looper.loop(Looper.java:137)
08-26 15:22:14.897: E/AndroidRuntime(13823):    at android.app.ActivityThread.main(ActivityThread.java:4424)
08-26 15:22:14.897: E/AndroidRuntime(13823):    at java.lang.reflect.Method.invokeNative(Native Method)
08-26 15:22:14.897: E/AndroidRuntime(13823):    at java.lang.reflect.Method.invoke(Method.java:511)
08-26 15:22:14.897: E/AndroidRuntime(13823):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
08-26 15:23:39.927: E/dalvikvm(14122): Could not find class 'com.idd.applicationtocall.MainActivity', referenced from method com.idd.applicationtolaunch.MainActivity.ClickMe
08-26 15:23:39.927: W/dalvikvm(14122): VFY: unable to resolve const-class 1140 (Lcom/idd/applicationtocall/MainActivity;) in Lcom/idd/applicationtolaunch/MainActivity;
08-26 15:23:39.927: D/dalvikvm(14122): VFY: replacing opcode 0x1c at 0x0002
08-26 15:23:39.957: I/PGA(14122): New SOCKET connection: icationtolaunch (pid 14122, tid 14122)

3 个答案:

答案 0 :(得分:1)

在OnClick中使用它:

public void ClickMe(View v) {
    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.idd.applicationtocall","com.idd.applicationtocall.MainActivity"));
    startActivity(intent);
}

ApplicationToCall的清单文件

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

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

您需要导出活动:

android:exported="true"

这样对我有用。

答案 1 :(得分:0)

您的logcat说未找到活动类。这可能是因为您没有将其添加到AndroidManifest.xml中。您应该添加标签

 <activity android:name="com.idd.applicationtocall.MainActivity"></activity>

到您的ApplicationToLaunch AndroidManifest.xml。

答案 2 :(得分:0)

我设法使用以下代码明确启动另一个应用程序的启动器活动。我在Stack Overflow上找到它,但我无法链接它,因为我不记得我找到它找到它。

        if (v.getId() == R.id.main_button)
        {
            android.content.pm.PackageManager mPm = getActivity().getPackageManager();
            PackageInfo info = null;
            try
            {
                info = mPm.getPackageInfo("com.example.other.app", 0);
            }
            catch (PackageManager.NameNotFoundException e)
            {
                Log.d(this.getClass().getSimpleName(), e.getMessage() + "does not exist", e);
            }
            Boolean isAppInstalled = info != null;
            Log.i(getClass().getSimpleName(), "The package was found: " + isAppInstalled);
        }
        else if(v.getId() == R.id.main_action_button)
        {
            Intent LaunchIntent = getActivity().getPackageManager().getLaunchIntentForPackage("com.example.other.app");
            startActivity(LaunchIntent);
        }

但是,如果您需要使用特定数据明确打开特定活动,那么您应该指定自定义操作,并尝试使用该操作启动其他应用程序。

Start Activity Using Custom Action