2011-12-12 15 views
5

मैं एक ऐसा एप्लिकेशन बनाने की कोशिश कर रहा हूं जो स्वचालित रूप से किसी विशिष्ट सर्वर से एपीके डाउनलोड करता है और इसे सिस्टम पर इंस्टॉल करता है। स्थापना के लिए मेरा कोड निम्न जैसा दिखता है, लेकिन काम नहीं करता है।किसी अन्य प्रोग्राम से एपीके इंस्टॉल करें

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);    

क्या मुझे स्थापना के लिए Manifest.xml में कोई अधिकार देने की आवश्यकता है? मुझे पता है कि सवाल पहले पूछे गए हैं, लेकिन किसी भी उत्तर ने मुझे अब तक मदद नहीं की है।

+1

जैसा कि मैंने अपने उत्तर में उल्लेख किया है, "पैकेज-संग्रह" का उपयोग करें और "पैकेज_आर्किव" नहीं। उम्मीद है कि यह ActivityNotFoundException हल करता है। – Karthik

उत्तर

9

यह वही है मैं अपने मामले में क्या,

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

और इन अनुमतियों हैं ..

<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

अभी भी काम नहीं करते हैं, यह अपवाद प्राप्त करें: * ई/एंड्रॉइड रनटाइम (2216): java.lang.RuntimeException: गतिविधि शुरू करने में असमर्थ ComponentInfo {foo.bar/foo.bar.FooActivity}: android.content.ActivityNotFoundException: कोई गतिविधि नहीं मिली इरादा संभाल {act = android.intent.action.VIEW डेटा = फ़ाइल: ///mnt/sdcard/Demo.apk टाइप = एप्लिकेशन/vnd.android.package_archive flg = 0x10000000} * – user932865

+0

<उपयोग-अनुमति एंड्रॉइड: name = " android.permission.INSTALL_PACKAGES "/> अब उपयोग नहीं किया जा रहा है, क्योंकि यह सिस्टम अनुप्रयोगों के लिए आश्वस्त है। – CoolMind

3

मैं भी कर

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

और मेरे इस

की तरह स्थापित lookd
intent.setDataAndType(Uri.parse("file://"+path), "application/vnd.android.package-archive"); 

मेरी path एक स्ट्रिंग, आप अपने कोड में इस

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

करने की ज़रूरत है अपने च

+0

फ़ाइल: // मेरा दिन बचाएं, धन्यवाद – djdance

0

की तरह, आप "package_archive", यह "पैकेज-संग्रह" होना चाहिए उल्लेख किया है।
आपको निम्न अनुमतियों की आवश्यकता होगी।

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

सभी मदद के लिए धन्यवाद, यह आखिरकार काम करता है। मैं अपना कामकाजी कोड साझा करता हूं और 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> 

// फ्रेड्रिक

+1

हाय फ्रेड्रिक, मुझे इस पर अपने प्रयोगकर्ता को साझा करने के लिए धन्यवाद देना चाहिए। मैंने सटीक निर्देशों का पालन किया है, लेकिन मुझे "PArser त्रुटि मिल रही है। पैकेज को पार्स करने में कोई समस्या है।" मैंने कई मंचों का जिक्र करने की कोशिश की लेकिन यह बहुत मददगार नहीं था। किसी भी तरह की मदद की सराहना की जाती है। – ayachama

+0

न्यूबी, क्या आपने इस मुद्दे पर कोई प्रगति की है? –

+0

@Newbie क्या आप दोनों सॉफ़्टवेयर के लिए समान हस्ताक्षर का उपयोग कर रहे हैं? और आप किस एंड्रॉइड संस्करण का उपयोग कर रहे हैं? – user932865

1

अपने targetSdkVersion बराबर या 24 से अधिक है, तो आप के लिए एंड्रॉयड एन और नए FileProvider कार्यान्वयन का उपयोग करने की जरूरत है एंड्रॉइड संस्करण।

// 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); 
    } 
} 

जोड़ें प्रदाता FileProviderAndroidManifest एक्सएमएल संसाधनों के तहत करने के लिए

<?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> 

और बनाने provider_paths.xml:

यहाँ पूरे कार्यान्वयन है।

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

FileProvider विन्यास के बारे में अधिक जानकारी आप here और here पढ़ सकते हैं।

सभी क्रेडिट @just_user पर जाएं क्योंकि मेरा उत्तर his reply पर आधारित है।

संबंधित मुद्दे