Can We Install an APK From a ContentProvider?

后端 未结 4 1071
忘了有多久
忘了有多久 2020-12-08 03:57

I am working on a library to allow apps to self-update, for those that are being distributed outside of the Android Market.

My original plan was to include code that

4条回答
  •  广开言路
    2020-12-08 04:39

    I have this in one of my applications which allows me access to local storage (user preference selectable before you have a go at me ;) )

    import java.io.*;
    import android.content.*;
    import android.database.*;
    import android.net.*;
    import android.os.*;
    import android.preference.PreferenceManager;
    import android.util.Log;
    
    public class LocalFileContentProvider extends ContentProvider {
       private static final String URI_PREFIX = "content://your.content.provider.as.per.manifest";
    
       public static String constructUri(String url) {
           Uri uri = Uri.parse(url);
           return uri.isAbsolute() ? url : URI_PREFIX + url;
       }
    
       @Override
       public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
    
           SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
    
           boolean allowLocal = app_preferences.getBoolean("allowLocalFiles", false);
    
            if (allowLocal) {    
                try {
                    File file = new File(uri.getPath());
    
                    if (file.isDirectory())
                        return null;
    
                    ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
                    return parcel;
                } catch (Exception e) {
                    return null;
                }
            } else {
                return null;
            }
    
       }
    
       @Override
       public boolean onCreate() {
           return true;
       }
    
       @Override
       public int delete(Uri uri, String s, String[] as) {
           throw new UnsupportedOperationException("Not supported by this provider");
       }
    
       @Override
       public String getType(Uri uri) {
           throw new UnsupportedOperationException("Not supported by this provider");
       }
    
       @Override
       public Uri insert(Uri uri, ContentValues contentvalues) {
           throw new UnsupportedOperationException("Not supported by this provider");
       }
    
       @Override
       public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {
           throw new UnsupportedOperationException("Not supported by this provider");
       }
    
       @Override
       public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {
           throw new UnsupportedOperationException("Not supported by this provider");
       }
    
    }
    

    My manifest contains

    
    

    and then to start the install (or action)

    Intent viewIntent = new Intent(Intent.ACTION_VIEW);
    viewIntent.setDataAndType(Uri.parse(url), mimeType);
    

提交回复
热议问题