应用程序在启动活动时崩溃

时间:2017-02-19 18:29:04

标签: java android webview

我有一个webview应用程序,并希望在网址中存在特定文字时从内部更新应用程序。

我在webview的shouldOverrideUrlLoading函数中调用它们:

Intent intent = new Intent(getApplicationContext(), Update.class);
startActivity(intent);
return true;

这是Update.class

public class Update extends MainActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
        String fileName = "app-file.apk";
        destination += fileName;
        final Uri uri = Uri.parse("file://" + destination);

        File file = new File(destination);
        if (file.exists())
            file.delete();

        DownloadManager.Request request = new DownloadManager.Request(
                Uri.parse(getIntent().getStringExtra("http://example.com/app-file.apk")));
        request.setDestinationUri(uri);
        dm.enqueue(request);

        final String finalDestination = destination;
        final BroadcastReceiver onComplete = new BroadcastReceiver() {
            public void onReceive(Context ctxt, Intent intent) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    Uri contentUri = FileProvider.getUriForFile(ctxt, BuildConfig.APPLICATION_ID + ".provider", new File(finalDestination));
                    Intent openFileIntent = new Intent(Intent.ACTION_VIEW);
                    openFileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    openFileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    openFileIntent.setData(contentUri);
                    startActivity(openFileIntent);
                    unregisterReceiver(this);
                    finish();
                } else {
                    Intent install = new Intent(Intent.ACTION_VIEW);
                    install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    install.setDataAndType(uri, "application/vnd.android.package-archive");
                    startActivity(install);
                    unregisterReceiver(this);
                    finish();
                }
            }
        };
        registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }
}

当我转到某个网址开始活动时,应用程序崩溃了。我很乐意看到任何解决方案。谢谢!

我解决了它,这是权限错误。

2 个答案:

答案 0 :(得分:0)

我建议您查看ADB Logcat,它将帮助您解决问题。 否则,请检查崩溃日志或确保已在manifest.xml中声明了Update Activity

答案 1 :(得分:0)

确保在Android Manifest.xml中声明新活动

尝试更改此

      Intent intent = new Intent(getApplicationContext(), Update.class);
      startActivity(intent);
      return true;

到这个

 Intent intent = new Intent(getApplicationContext(), Update.class);
 startActivity(intent);
 finish();
 return true;

让我知道那是怎么回事。