我正在为Android编写自己的cordova插件。我有一个独立的Android项目内的插件。我似乎无法从插件java文件开始我的活动。它一直触发无法找到MainActivity的错误(无法找到符号)。我希望这很容易解决,我似乎不知道。
Android Manifest:
<?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>
Plug-In 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);
答案 0 :(得分:2)
我这样解决了:
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);