使用Android的Codename One本机代码找不到活动

时间:2016-03-04 15:18:42

标签: codenameone

我正在尝试从我的应用安装.apk文件。

我将.apk文件放到其他应用程序也可以访问的目录中,之后,我在Android native中调用以下代码:

private static Activity activity() {
    return com.codename1.impl.android.AndroidNativeUtil.getActivity();
}   

public void install(String fileName) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.parse(getDownloadPath() + File.separator + fileName), "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    activity().startActivity(intent);
}

public String getDownloadPath() {
    return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + File.separator + "myfolder";
}

我还在构建提示中添加了以下权限:

<uses-permission     android:name="android.permission.INSTALL_PACKAGES" />

日志也指示路径,当我手动浏览到该路径时,.apk就坐在那里。我也可以手动安装它。 但是,上面的代码提供了以下内容:

    D/Instrumentation( 4697): checkStartActivityResult  :Intent { act=android.intent.action.VIEW dat=/storage/emulated/0/Download/myfolder/MyOtherApp.apk typ=application/vnd.android.package-archive flg=0x10000000 }
    D/Instrumentation( 4697): checkStartActivityResult  inent is instance of inent:
    W/dalvikvm( 4697): threadid=16: thread exiting with uncaught exception (group=0x41f02c08)
    E/AndroidRuntime( 4697): FATAL EXCEPTION: Thread-3980
    E/AndroidRuntime( 4697): Process: com.myapplication.myapp, PID: 4697
    E/AndroidRuntime( 4697): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=/storage/emulated/0/Download/myfolder/MyOtherApp.apk typ=application/vnd.android.package-archive flg=0x10000000 }
    E/AndroidRuntime( 4697):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1660)
    E/AndroidRuntime( 4697):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1430)
    E/AndroidRuntime( 4697):    at android.app.Activity.startActivityForResult(Activity.java:3532)
    E/AndroidRuntime( 4697):    at android.app.Activity.startActivityForResult(Activity.java:3493)
    E/AndroidRuntime( 4697):    at com.codename1.impl.android.CodenameOneActivity.startActivityForResult(CodenameOneActivity.java:534)
    E/AndroidRuntime( 4697):    at android.app.Activity.startActivity(Activity.java:3735)
    E/AndroidRuntime( 4697):    at android.app.Activity.startActivity(Activity.java:3703)
    E/AndroidRuntime( 4697):    at com.codename1.impl.android.CodenameOneActivity.startActivity(CodenameOneActivity.java:540)
    E/AndroidRuntime( 4697):    at com.myapplication.cn1.application.interfaces.IApplicationNativeImpl.install(IApplicationNativeImpl.java:40)
    E/AndroidRuntime( 4697):    at com.myapplication.cn1.application.interfaces.IApplicationNativeStub.install(IApplicationNativeStub.java:17)
    E/AndroidRuntime( 4697):    at com.myapplication.cn1.application.Application.install(Application.java:33)
    E/AndroidRuntime( 4697):    at com.myapplication.myapp.worker.ApplicationWorker.install(ApplicationWorker.java:68)
    E/AndroidRuntime( 4697):    at com.myapplication.myapp.ui.component.MenuButton.doApplicationDownloadCallback(MenuButton.java:93)
    E/AndroidRuntime( 4697):    at com.myapplication.myapp.ui.component.MenuButton.callback(MenuButton.java:78)
    E/AndroidRuntime( 4697):    at com.myapplication.myapp.worker.ApplicationWorker$1.run(ApplicationWorker.java:59)
    E/AndroidRuntime( 4697):    at java.lang.Thread.run(Thread.java:841)
    W/ActivityManager( 2418):   Force finishing activity com.myapplication.myapp/.MyAppStub

安装的代码段也可以在原始Android应用程序上成功运行。

有什么我忘记做的事,或者在CodenameOne中使用错误的活动吗?

2 个答案:

答案 0 :(得分:0)

安装嵌入在应用程序中的APK听起来像是不可能的事情,并且您收到的错误是没有可用于处理该文件的活动。

答案 1 :(得分:0)

问题在于这一行:

 intent.setDataAndType(Uri.parse(getDownloadPath() + File.separator + fileName), "application/vnd.android.package-archive");

应该是:

 intent.setDataAndType(Uri.fromFile(new File(getDownloadPath() + File.separator + fileName)), "application/vnd.android.package-archive");

原因是file:///被添加到前面。

此外,似乎不需要INSTALL_PACKAGES权限。