呃,在这里和 Qt 论坛上搜索了大量资源,最终从许多来源中找到了一个运行应用程序的解决方案:
C++代码:
Q_INVOKABLE void start(const QString &packageName)
{
QAndroidJniObject javaPackageName = QAndroidJniObject::fromString(packageName);
bool success = (bool)QAndroidJniObject::callStaticMethod<jboolean>(
"com/mycompany/myapp/PackageActivity",
"runApplication",
"(Ljava/lang/String;)Z",
javaPackageName.object<jstring>());
if(!success) {
qDebug() << "Error:" << packageName << "package not found!";
}
}
Java代码:
package com.mycompany.myapp;
import android.content.pm.PackageManager;
import android.content.Intent;
import android.content.Context;
import android.app.Activity;
public class PackageActivity
{
public static boolean runApplication(String packageName)
{
Activity activity = org.qtproject.qt5.android.QtNative.activity();
PackageManager pm = activity.getApplicationContext().getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(packageName);
if (intent != null) {
activity.startActivity(intent);
return true;
} else {
return false;
}
}
}
主要问题是
- 得到工作
Activity
对象
- 然后调用
getPackageManager()
适当的对象,因为方法返回以下错误给我调用任意创建的对象Acivity
和Context
对象:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference