0

总是通过阅读您的所有答案来感谢您的帮助,我经常使用堆栈溢出并且它总是很有帮助,如果有人可以帮助我,我现在没有找到解决我的问题的帮助,那就太好了。

我做了一个简单的程序来自动更新我的应用程序。所以下载运行良好,将 apk 带入应用程序会给我一个致命错误。

通过调试错误在

startActivity(intent);

代码是:

package com.example.updateversion;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);

    // ***************************

    // if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);
    // }
    java.text.DateFormat dateFormat = android.text.format.DateFormat
            .getDateFormat(getApplicationContext());

    // ***************************
    String apkurl = "http://192.168.1.1/ANDROID/helloVersion.zip";// "http://www.vogella.com";
    try {

        URL url = new URL(apkurl);

        HttpURLConnection urlConnection = (HttpURLConnection) url
                .openConnection();
        String PATH = Environment.getExternalStorageDirectory()
                + "/download/";
        try {

            File file = new File(PATH);
            file.mkdirs();

            File outputFile = new File(file, "helloVersion.apk");
            FileOutputStream fos = new FileOutputStream(outputFile);

            InputStream is = new BufferedInputStream(
                    urlConnection.getInputStream());

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.close();
            is.close();

        } finally {
            try {
                urlConnection.disconnect();
            } catch (Exception e) {
            }
        }

        File apkFile = new File(PATH + "helloVersion.apk");
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(apkFile),
                "application/com.example.helloversion");
        startActivity(intent); // here is the problem
    } catch (IOException e) {
        e.printStackTrace();
    }
    return true;
}

}

我的错误是:

03-19 08:58:29.792: D/AndroidRuntime(28060): Shutting down VM
03-19 08:58:29.792: W/dalvikvm(28060): threadid=1: thread exiting with uncaught exception (group=0x40a591f8)
03-19 08:58:29.822: E/AndroidRuntime(28060): FATAL EXCEPTION: main
03-19 08:58:29.822: E/AndroidRuntime(28060): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/download/helloVersion.apk typ=application/com.example.helloversion }
03-19 08:58:29.822: E/AndroidRuntime(28060):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1512)
03-19 08:58:29.822: E/AndroidRuntime(28060):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
03-19 08:58:29.822: E/AndroidRuntime(28060):    at android.app.Activity.startActivityForResult(Activity.java:3190)
03-19 08:58:29.822: E/AndroidRuntime(28060):    at android.app.Activity.startActivity(Activity.java:3297)
03-19 08:58:29.822: E/AndroidRuntime(28060):    at com.example.updateversion.MainActivity.onCreateOptionsMenu(MainActivity.java:87)
03-19 08:58:29.822: E/AndroidRuntime(28060):    at android.app.Activity.onCreatePanelMenu(Activity.java:2444)
03-19 08:58:29.822: E/AndroidRuntime(28060):    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:392)
03-19 08:58:29.822: E/AndroidRuntime(28060):    at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:743)
03-19 08:58:29.822: E/AndroidRuntime(28060):    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2859)
03-19 08:58:29.822: E/AndroidRuntime(28060):    at android.os.Handler.handleCallback(Handler.java:605)
03-19 08:58:29.822: E/AndroidRuntime(28060):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-19 08:58:29.822: E/AndroidRuntime(28060):    at android.os.Looper.loop(Looper.java:137)
03-19 08:58:29.822: E/AndroidRuntime(28060):    at android.app.ActivityThread.main(ActivityThread.java:4424)
03-19 08:58:29.822: E/AndroidRuntime(28060):    at java.lang.reflect.Method.invokeNative(Native Method)
03-19 08:58:29.822: E/AndroidRuntime(28060):    at java.lang.reflect.Method.invoke(Method.java:511)
03-19 08:58:29.822: E/AndroidRuntime(28060):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-19 08:58:29.822: E/AndroidRuntime(28060):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-19 08:58:29.822: E/AndroidRuntime(28060):    at dalvik.system.NativeStart.main(Native Method)

我的权限是:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
4

1 回答 1

2

尝试使用:

String filepath = "yourfile";
Uri fileLoc = Uri.fromFile(new File(filepath));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(fileLoc, "application/vnd.android.package-archive");
startActivity(intent);
于 2013-03-19T07:51:21.310 回答