android - 在 Cordova 插件内找不到 Intent 的 MainActivity

标签 android cordova phonegap-plugins

我正在为 Android 编写自己的 cordova 插件。我的插件在一个独立的 Android 项目中工作。我似乎无法从插件 java 文件开始我的 Activity 。它不断触发找不到 MainActivity(找不到符号)的错误。我希望这是一个我似乎缺少的简单修复。

Android list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp.testplugin" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:windowSoftInputMode="adjustResize"
            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>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>

Java 插件:

package com.myapp.testplugin;

import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaInterface;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.provider.Settings;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class TestPlugin extends CordovaPlugin {
    public static final String TAG = "Test Plugin";
    /**
    * Constructor.
    */
    public TestPlugin() {}
    /**
    * Sets the context of the Command. This can then be used to do things like
    * get file paths associated with the Activity.
    *
    * @param cordova The context of the main Activity.
    * @param webView The CordovaWebView Cordova is running in.
    */
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        Log.d(TAG,"Init TestPlugin");
    }

    @Override
    public boolean execute(final String action, JSONArray args, CallbackContext callbackContext) 
    throws JSONException {
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Context context = cordova.getActivity().getApplicationContext();
                Intent intent = new Intent(context, MainActivity.class);
                cordova.getActivity().startActivity(intent);
            }
        });

        return true;
    }
}

此处发生错误:

Intent Intent = new Intent(context, MainActivity.class);

最佳答案

我是这样解决的:

    Class mainActivity;
    Context context = getApplicationContext();
    String  packageName = context.getPackageName();
    Intent  launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
    String  className = launchIntent.getComponent().getClassName();

    try {
        //loading the Main Activity to not import it in the plugin
        mainActivity = Class.forName(className);
    } catch (Exception e) {
        e.printStackTrace();
    }

    Intent openActivityIntent = new Intent(context, mainActivity);

关于android - 在 Cordova 插件内找不到 Intent 的 MainActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28094523/

相关文章:

android - 如何修复 "Failed to install ' cordova-plugin-console' :Error "

android - 在适用于 Android 和 IOS 的 Phonegap 中使用日历插件

android - 具有 phonegap 的 Android 客户端的 Web 服务的权威签名证书或自己的证书

java - 使用 Java 在 Android 模拟器上的两点之间绘图

android - 如何在 Manifest.json 中设置 minSdkVersion (Android/Ionic)

javascript - document.addEventListener 适用于 Android 但不适用于浏览器

android - PhoneGap/ Cordova : Shake Event detection not smooth

android - 如何声明一个对所有模块的 build.gradle 文件可见的常量?

android - Flutter & AndroidX 不兼容 如何手动设置依赖

ios - 有没有适用于 phonegap 2.7.0 的 Facebook 插件?