2016-09-05 11 views
5

Trong ứng dụng của tôi, tôi có một ô tô tải tùy chỉnh và cài đặt APK nó hoạt động nhưKhông Hoạt động phát hiện để xử lý Ý định khi sử dụng FileProvider

// auto register for the complete download 
    activity.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 



// Download the file through DownloadManager 
String destination = Environment.getExternalStorageDirectory() + "/"; 
    String fileName = "myfile.apk"; 
    destination += fileName; 
    final Uri uri = Uri.parse("file://" + destination); 
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl)); 
    request.setDescription("description"); 
    request.setTitle("title"); 
    request.setDestinationUri(uri); 
    final DownloadManager manager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE); 
    final long downloadId = manager.enqueue(request); 

onComplete = new BroadcastReceiver() { 
     public void onReceive(Context ctxt, Intent intent) { 

      Intent install = new Intent(Intent.ACTION_VIEW); 
      // BEFORE working doing this 
      //install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      //install.setDataAndType(uri, 
      // manager.getMimeTypeForDownloadedFile(downloadId)); 

      // Using file provider it doesnt work 
      Uri apkUri = FileProvider.getUriForFile(AutoUpdate.this, 
       "com.myapp", file); 
       install.setDataAndType(apkUri,manager.getMimeTypeForDownloadedFile(downloadId)); 
      activity.startActivity(install); 
      activity.unregisterReceiver(this); 

     } 
    }; 

manifest Android này:

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

Provider_path (Xin lỗi cho một số lý do để cắt thẻ đường dẫn)

tên đường dẫn bên ngoài = "myfolder" path = "." />

Khi tập xong để tải onComplete được gọi nhưng activiy không bắt đầu:

Không Hoạt động phát hiện để xử lý Ý định {hành động = android.intent.action.VIEW dat = Nội dung: //com.myapp/myfolder/myfile.apk typ = application/vnd.android.package-archive FLG = 0x4000000}

Khi sử dụng các tập tin bình thường: // nó làm việc

có một cái gì đó tôi thiếu khi sử dụng nhà cung cấp tệp? Hoạt động không bắt đầu vì không tìm thấy tệp? Tôi có cần thêm sự cho phép không? (tại thời điểm này tôi có INTERNET, READ và VIẾT trên bộ nhớ ngoài)

+0

'Hoạt động không bắt đầu vì không tìm thấy tệp '. Thông báo là hoạt động không được tìm thấy. – greenapps

+0

Lưu ý rằng "nếu" tôi thay thế loại mime bằng "*/*", nó sẽ mở lựa chọn ứng dụng nhưng khi tôi chọn "trình quản lý tệp", nó chỉ mở nó và giữ nguyên gốc. Đó là lý do tại sao tôi tự hỏi bằng cách sử dụng FileProvider tệp không thực sự được tìm thấy? – Johny19

+0

bạn đã khai báo biến "tệp" ở đâu? – emaillenin

Trả lời

12

Trình cài đặt gói chỉ hỗ trợ content các sơ đồ bắt đầu trên Android 7.0. Trước đó — và mặc dù tài liệu hướng ngược lại — trình cài đặt gói chỉ hỗ trợ các lược đồ file.

Bạn sẽ cần đặt Uri trên Intent khác nhau dựa trên việc bạn có đang chạy trên Android 7.0+ hay không, chẳng hạn như bằng cách phân nhánh trên Build.VERSION.SDK_INT.

+1

Bạn quên google! Cảm ơn bạn đã chỉ ra điều đó! – Johny19

+0

@ Johny19 bạn có thể đăng giải pháp làm việc không? – emaillenin

0

cung cấp dịch vụ của bạn không tìm thấy vì

android:enabled="true" 

là mất tích trong biểu hiện của bạn.

Nhưng sau đó nó sẽ không hoạt động hoặc đọc câu trả lời khác của CW.

+0

Tôi đã thử cách này. và tôi vẫn gặp lỗi tương tự – Johny19

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