【发布时间】:2017-02-19 18:29:03
【问题描述】:
我有一个 webview 应用程序,并且想在 url 中存在特定文本时从内部更新应用程序。
我在 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));
}
}
当我转到某个 url 启动活动时,应用程序崩溃。我很想看到任何解决方案。谢谢!!
我解决了,是权限错误。
【问题讨论】:
-
任何崩溃报告?
-
使用
adb logcat评估堆栈跟踪。 -
我的钱在
NullPointerException。当有人在onCreate方法中发布问题时,它总是NullPointerException。 -
我从未尝试过 adb,但我现在才尝试过,而且不会停止。
-
看这个http://pastebin.com/raw/Zhgk66UD