2016-09-14 48 views
7

Ứng dụng của tôi có tính năng cập nhật tự động tải xuống APK và sau đó sử dụng Intent.ACTION_VIEW để mở trình cài đặt gói.Android 7 APK mở với ACTION_VIEW không hoạt động (Trình cài đặt gói đã dừng)

Lên đến 7 nó làm việc một cách hoàn hảo (bằng cách cho ăn tiếp cận mục đích với một tập tin bình thường: //)

Với Android 7 Tôi đã phải thay đổi để sử dụng một FileProvider. Sự khác biệt duy nhất trong mã là:

Intent installIntent = new Intent(Intent.ACTION_VIEW); 
      if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) { 
      installIntent.setDataAndType(uri, 
       manager.getMimeTypeForDownloadedFile(downloadId)); 
      } else { 

      Uri apkUri = FileProvider.getUriForFile(AutoUpdate.this, 
       BuildConfig.APPLICATION_ID, file); 
       installIntent.setDataAndType(apkUri,manager.getMimeTypeForDownloadedFile(downloadId)); 
      } 
      activity.startActivity(installIntent); 

Khi startActivity được gọi là tôi có được điều này mỗi lần duy nhất

enter image description here

Đây có phải là một lỗi với Android 7? Hoặc một cái gì đó/sự cho phép là thiếu mặt của tôi?

EDIT AndroidManifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 
    <uses-permission android:name="android.permission.READ_LOGS" /> 

<application ...> 
... 
    <provider 
      android:name="android.support.v4.content.FileProvider" 
      android:authorities="com.myapp" 
      android:exported="false" 
      android:enabled="true" 
      android:grantUriPermissions="true"> 
      <meta-data 
       android:name="android.support.FILE_PROVIDER_PATHS" 
       android:resource="@xml/provider_paths"/> 
     </provider> 
    </application> 

Đường dẫn xmlFile

<?xml version="1.0" encoding="utf-8"?> 
<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
    <external-path name="myfolder" path="."/> 
</paths> 
+0

Bạn có thể gửi biểu hiện của bạn? – Techidiot

+0

Tôi đã thêm nó. Lưu ý rằng điều này xảy ra trên trình giả lập cũng như trên điện thoại di động thực sự – Johny19

+0

@ Johny19 hey tôi có cùng một vấn đề ... làm thế nào bạn cố định nó ..., xin vui lòng guid tôi – Mariyappan

Trả lời

1

thử như dưới đây, nó đã giúp tôi và làm việc của mình trong Android N7.0

File toInstall = new File(appDirectory, appName + ".apk"); 
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
    Uri apkUri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", toInstall); 
    Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE); 
    intent.setData(apkUri); 
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
    activity.startActivity(intent) 
} else { 
    Uri apkUri = Uri.fromFile(toInstall); 
    Intent intent = new Intent(Intent.ACTION_VIEW); 
    intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    activity.startActivity(intent); 
} 
+0

Đã lưu ngày của tôi! Nó phải là câu trả lời được chấp nhận. –

Các vấn đề liên quan