2011-12-12 33 views
5

Tôi đang cố gắng tạo ứng dụng tự động tải xuống một gói ứng dụng từ một máy chủ cụ thể và cài đặt nó trên hệ thống. Mã của tôi để cài đặt trông giống như sau, nhưng không hoạt động.Cài đặt apk từ một chương trình khác

File f = new File("/mnt/sdcard/download/", "Demo.apk"); 
Log.i("Demo", "f "+f.getAbsoluteFile()); 
Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(Uri.fromFile(f), "application/vnd.android.package_archive"); 
intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK); 
m_context.startActivity(intent);    

Tôi có cần cung cấp bất kỳ quyền nào trong Manifest.xml để cài đặt không? Tôi biết câu hỏi đó đã được hỏi trước đây, nhưng không có câu trả lời nào đã giúp tôi cho đến nay.

+1

Như tôi đã đề cập trong câu trả lời của mình, hãy sử dụng "gói lưu trữ" chứ không phải "package_archive". Hy vọng điều này giải quyết được ActivityNotFoundException. – Karthik

Trả lời

9

này những gì tôi làm trong trường hợp của tôi,

Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(Uri.fromFile(new File(path+"/<application_name>.apk")), "application/vnd.android.package-archive"); 
startActivity(intent); 

Và đây là những điều khoản ..

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.INSTALL_PACKAGES" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+5

vẫn không hoạt động, ngoại trừ trường hợp này: * E/AndroidRuntime (2216): java.lang.RuntimeException: Không thể bắt đầu hoạt động ComponentInfo {foo.bar/foo.bar.FooActivity}: android.content.ActivityNotFoundException: Không tìm thấy Hoạt động nào xử lý Intent {act = android.intent.action.VIEW dat = tệp: ///mnt/sdcard/Demo.apk typ = application/vnd.android.package_archive flg = 0x10000000} * – user932865

+0

hiện không được sử dụng vì nó có ý nghĩa đối với các ứng dụng hệ thống. – CoolMind

3

tôi cũng làm

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

và tôi cài đặt lookd như

này
intent.setDataAndType(Uri.parse("file://"+path), "application/vnd.android.package-archive"); 

tôi path là một String, như f bạn

+0

file: // lưu ngày của tôi, nhờ – djdance

0

Bạn cần phải làm điều này

Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(Uri.fromFile(f), "application/vnd.android.package-archive"); 

Trong code của bạn, bạn đã đề cập đến "package_archive", nó phải là "gói-archive".
Bạn sẽ cần các quyền sau đây.

<uses-permission android:name="android.permission.INSTALL_PACKAGES"></uses-permission> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
7

Cảm ơn sự giúp đỡ của bạn, cuối cùng cũng giúp nó hoạt động. Tôi chia sẻ mã làm việc của tôi và làm việc Manifest.xml.

package test.installer; 
import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.util.Log; 

public class InstallToolActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.i("Demo", "onCreate"); 
    Intent intent = new Intent(Intent.ACTION_VIEW); 
    intent.setDataAndType(Uri.parse("file://"+"/mnt/sdcard/HelloWorld.apk"), "application/vnd.android.package-archive"); 
    intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(intent); 
    } 
} 

Manifext.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="test.installer" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk android:minSdkVersion="11" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.INSTALL_PACKAGES"/> 
<uses-permission android:name="android.permission.RESTART_PACKAGES"/> 


<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:label="@string/app_name" 
     android:name=".InstallToolActivity" > 
     <intent-filter > 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

// Fredrik

+1

Hi Fredrik, tôi nên cảm ơn bạn đã chia sẻ kinh nghiệm của bạn về điều này. Tôi đã làm theo hướng dẫn chính xác tương tự, nhưng tôi nhận được "lỗi PArser. Có một vấn đề phân tích cú pháp gói." Tôi đã cố gắng giới thiệu nhiều diễn đàn nhưng nó không phải là sự giúp đỡ tuyệt vời. Giúp đỡ của bất kỳ loại được đánh giá cao. – ayachama

+0

Bạn mới có thực hiện bất kỳ tiến bộ nào về vấn đề này không? –

+0

@Newbie Bạn đang sử dụng chữ ký giống nhau cho cả hai phần mềm? Bạn đang sử dụng phiên bản Android nào? – user932865

1

Nếu bạn targetSdkVersion là bằng hoặc cao hơn 24, thì bạn cần phải sử dụng FileProvider thực hiện dành cho Android N và mới hơn Phiên bản Android.

Dưới đây là toàn bộ thực hiện:

// utility method 
private void openAppInstaller(Context context, File toInstall) { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
     Uri apkUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", toInstall); 
     Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE); 
     intent.setData(apkUri); 
     intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
     context.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); 
     context.startActivity(intent); 
    } 
} 

Thêm nhà cung cấp FileProvider để AndroidManifest

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    ... 
    <application 
     ... 
     <provider 
      android:name="android.support.v4.content.FileProvider" 
      android:authorities="${applicationId}.provider" 
      android:exported="false" 
      android:grantUriPermissions="true"> 
      <meta-data 
       android:name="android.support.FILE_PROVIDER_PATHS" 
       android:resource="@xml/provider_paths"/> 
     </provider> 
    </application> 
</manifest> 

Và tạo provider_paths.xml dưới nguồn xml.

<?xml version="1.0" encoding="utf-8"?> 
<paths> 
    <external-path name="external_files" path="."/> 
</paths> 

Thông tin thêm về cấu hình FileProvider bạn có thể đọc herehere.

Tất cả tín dụng đi đến @just_user vì câu trả lời của tôi dựa trên his reply.

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