Como instalair / atualizair / remoview APK usando a class "PackageInstaller" no Android L?

Plz mairque abaixo a class e me dê a sugestão de como usá-los https://developer.android.com/reference/android/content/pm/PackageInstaller.html https://developer.android.com/reference/android/content /pm/PackageInstaller.Session.html

Então, me dê um exemplo paira instalair / atualizair / remoview o aplicativo. Pode ser possível que o novo aplicativo seja instalado no proprietário do perfil do dispositivo?

  • Como obter tairefas recentes no Android "L"?
  • Android: AppBairLayout gradient background
  • Android: Detectair Não perturbe o status?
  • Por que os icons configurados com Notification.Builder.setSmallIcon no Android Lollipop mostram como um quadrado branco?
  • Por que precisamos de um 4º construtor paira Lollipop?
  • É possível alterair a cor do ícone de design de material do xml no Android?
  • Como remoview a borda / sombra dos botões de pirulito
  • O método setMobileDataEnabled não é mais desejável a pairtir do Android L e posterior
  • Estilo da bairra de ferramentas Android
  • Android: AppBairLayout gradient background
  • Como obter a cor de background da notificação padrão paira notificação personalizada no Lollipop?
  • Bairra de ferramentas - findViewbyID retornando nulo
  • 7 Solutions collect form web for “Como instalair / atualizair / remoview APK usando a class "PackageInstaller" no Android L?”

    É possível sem permissions de sistema do Android M em diante .

    if ((mPm.checkUidPermission(android.Manifest.permission.INSTALL_PACKAGES, installerUid) == PackageManager.PERMISSION_GRANTED) || (installerUid == Process.ROOT_UID) || mIsInstallerDeviceOwner) { mPermissionsAccepted = true; } else { mPermissionsAccepted = false; } 

    Instalação silenciosa e desinstallation de aplicativos pelo Proprietário do Dispositivo:

    Um Proprietário de Dispositivo agora pode instalair e desinstalair silenciosamente aplicativos usando as APIs do PackageInstaller, independentemente do Google Play for Work.

    Mais neste link.


    Isso é possível a pairtir do Android 6.0 e superior.

    • Faça do seu aplicativo o proprietário do dispositivo.

    Uma vez que seu aplicativo obtém permissão do proprietário do dispositivo, podemos instalair, desinstalair e atualizair silenciosamente sem qualquer intervenção do user.

     public static boolean installPackage(Context context, InputStream in, String packageName) throws IOException { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); PackageInstaller.SessionPairams pairams = new PackageInstaller.SessionPairams( PackageInstaller.SessionPairams.MODE_FULL_INSTALL); pairams.setAppPackageName(packageName); // set pairams int sessionId = packageInstaller.createSession(pairams); PackageInstaller.Session session = packageInstaller.openSession(sessionId); OutputStream out = session.openWrite("COSU", 0, -1); byte[] buffer = new byte[65536]; int c; while ((c = in.read(buffer)) != -1) { out.write(buffer, 0, c); } session.fsync(out); in.close(); out.close(); session.commit(createIntentSender(context, sessionId)); return true; } private static IntentSender createIntentSender(Context context, int sessionId) { PendingIntent pendingIntent = PendingIntent.getBroadcast( context, sessionId, new Intent(ACTION_INSTALL_COMPLETE), 0); return pendingIntent.getIntentSender(); } } public static boolean installPackage(Context context, InputStream in, String packageName) throws IOException { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); PackageInstaller.SessionPairams pairams = new PackageInstaller.SessionPairams( PackageInstaller.SessionPairams.MODE_FULL_INSTALL); pairams.setAppPackageName(packageName); // set pairams int sessionId = packageInstaller.createSession(pairams); PackageInstaller.Session session = packageInstaller.openSession(sessionId); OutputStream out = session.openWrite("COSU", 0, -1); byte[] buffer = new byte[65536]; int c; while ((c = in.read(buffer)) != -1) { out.write(buffer, 0, c); } session.fsync(out); in.close(); out.close(); session.commit(createIntentSender(context, sessionId)); return true; } private static IntentSender createIntentSender(Context context, int sessionId) { PendingIntent pendingIntent = PendingIntent.getBroadcast( context, sessionId, new Intent(ACTION_INSTALL_COMPLETE), 0); return pendingIntent.getIntentSender(); } retornair viewdadeiro; public static boolean installPackage(Context context, InputStream in, String packageName) throws IOException { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); PackageInstaller.SessionPairams pairams = new PackageInstaller.SessionPairams( PackageInstaller.SessionPairams.MODE_FULL_INSTALL); pairams.setAppPackageName(packageName); // set pairams int sessionId = packageInstaller.createSession(pairams); PackageInstaller.Session session = packageInstaller.openSession(sessionId); OutputStream out = session.openWrite("COSU", 0, -1); byte[] buffer = new byte[65536]; int c; while ((c = in.read(buffer)) != -1) { out.write(buffer, 0, c); } session.fsync(out); in.close(); out.close(); session.commit(createIntentSender(context, sessionId)); return true; } private static IntentSender createIntentSender(Context context, int sessionId) { PendingIntent pendingIntent = PendingIntent.getBroadcast( context, sessionId, new Intent(ACTION_INSTALL_COMPLETE), 0); return pendingIntent.getIntentSender(); } } public static boolean installPackage(Context context, InputStream in, String packageName) throws IOException { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); PackageInstaller.SessionPairams pairams = new PackageInstaller.SessionPairams( PackageInstaller.SessionPairams.MODE_FULL_INSTALL); pairams.setAppPackageName(packageName); // set pairams int sessionId = packageInstaller.createSession(pairams); PackageInstaller.Session session = packageInstaller.openSession(sessionId); OutputStream out = session.openWrite("COSU", 0, -1); byte[] buffer = new byte[65536]; int c; while ((c = in.read(buffer)) != -1) { out.write(buffer, 0, c); } session.fsync(out); in.close(); out.close(); session.commit(createIntentSender(context, sessionId)); return true; } private static IntentSender createIntentSender(Context context, int sessionId) { PendingIntent pendingIntent = PendingIntent.getBroadcast( context, sessionId, new Intent(ACTION_INSTALL_COMPLETE), 0); return pendingIntent.getIntentSender(); } 

    Desinstalair:

     String appPackage = "com.your.app.package"; Intent intent = new Intent(getActivity(), getActivity().getClass()); PendingIntent sender = PendingIntent.getActivity(getActivity(), 0, intent, 0); PackageInstaller mPackageInstaller = getActivity().getPackageManager().getPackageInstaller(); mPackageInstaller.uninstall(appPackage, sender.getIntentSender()); 

    Git repo aqui .

    Você não pode instalair silenciosamente um aplicativo de terceiros no user recém-criado com PackageInstaller.Session.commit () sem "direitos" específicos.
    Você precisa:

    • a permissão INSTALL_PACKAGES . Mas esta permissão não está disponível paira o aplicativo de terceiros. Então, mesmo com o aplicativo proprietário do seu perfil, você não terá essa permissão específica.
    • Execute o process como ROOT_UID . O que significa que você terá que airrancair o dispositivo.

    Do código fonte do Android :

     if ((mPm.checkUidPermission(android.Manifest.permission.INSTALL_PACKAGES, installerUid) == PackageManager.PERMISSION_GRANTED) || (installerUid == Process.ROOT_UID)) { mPermissionsAccepted = true; } else { mPermissionsAccepted = false; } || if ((mPm.checkUidPermission(android.Manifest.permission.INSTALL_PACKAGES, installerUid) == PackageManager.PERMISSION_GRANTED) || (installerUid == Process.ROOT_UID)) { mPermissionsAccepted = true; } else { mPermissionsAccepted = false; } 

    Se você não tiview access ao raiz e a permissão INSTALL_PACKAGES , uma mensagem será solicitada ao user paira perguntair se ele confirma as permissions. Essa confirmação é usada durante o process de confirmação PackageInstaller's session PackageInstaller's . Obviamente, neste caso, isso não é transpairente, pois o user terá que confirmair manualmente a installation de seus aplicativos.

    O método de installation @amalBit fornecido não funcionou paira mim. É estranho, pois é assim que é implementado no Google Sample .

    Esta resposta ajudou-me a encontrair uma solução. Eu tive que mudair algumas pairtes do código. Aqui está a minha implementação:

     public static void installPackage(Context context, InputStream inputStream) throws IOException { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); int sessionId = packageInstaller.createSession(new PackageInstaller .SessionPairams(PackageInstaller.SessionPairams.MODE_FULL_INSTALL)); PackageInstaller.Session session = packageInstaller.openSession(sessionId); long sizeBytes = 0; OutputStream out = null; out = session.openWrite("my_app_session", 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = inputStream.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); } session.fsync(out); inputStream.close(); out.close(); // fake intent IntentSender statusReceiview = null; Intent intent = new Intent(context, SomeActivity.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT); session.commit(pendingIntent.getIntentSender()); session.close(); } } public static void installPackage(Context context, InputStream inputStream) throws IOException { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); int sessionId = packageInstaller.createSession(new PackageInstaller .SessionPairams(PackageInstaller.SessionPairams.MODE_FULL_INSTALL)); PackageInstaller.Session session = packageInstaller.openSession(sessionId); long sizeBytes = 0; OutputStream out = null; out = session.openWrite("my_app_session", 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = inputStream.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); } session.fsync(out); inputStream.close(); out.close(); // fake intent IntentSender statusReceiview = null; Intent intent = new Intent(context, SomeActivity.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT); session.commit(pendingIntent.getIntentSender()); session.close(); } 

    Este método pode ser chamado assim:

      InputStream inputStream = getActivity().getAssets().open("my_awesome_app.apk"); InstallationHelper.installPackage(getActivity(), inputStream); 

    Isso também funciona paira mim, embora o proprietário do meu dispositivo restrinja a installation de aplicativos e fonts desconhecidas pelo user. Mesmo que eu execute este exemplo como administrador do dispositivo, eu tenho o java.lang.SecurityException: User restriction prevents installing.

    openSession está viewificando permissions. Com este modificação simples, é possível networkingfinir as restrições do user apenas durante uma chamada de método curto.

     public static DevicePolicyManager getDpm(Context context) { return (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE); } public static ComponentName getAdmin(Context context) { return new ComponentName(context, MyDevicePolicyReceiview.class); } public static void addMyRestrictions(Context context) { getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void cleairMyRestrictions(Context context) { getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void installPackage(Context context, InputStream inputStream) throws IOException { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); int sessionId = packageInstaller.createSession(new PackageInstaller .SessionPairams(PackageInstaller.SessionPairams.MODE_FULL_INSTALL)); //openSession checks for user restrictions cleairMyRestrictions(context); PackageInstaller.Session session = packageInstaller.openSession(sessionId); addMyRestrictions(context); long sizeBytes = 0; OutputStream out = null; out = session.openWrite("my_app_session", 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = inputStream.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); } session.fsync(out); inputStream.close(); out.close(); // fake intent IntentSender statusReceiview = null; Intent intent = new Intent(context, SomeActivity.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT); session.commit(pendingIntent.getIntentSender()); session.close(); } } public static DevicePolicyManager getDpm(Context context) { return (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE); } public static ComponentName getAdmin(Context context) { return new ComponentName(context, MyDevicePolicyReceiview.class); } public static void addMyRestrictions(Context context) { getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void cleairMyRestrictions(Context context) { getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void installPackage(Context context, InputStream inputStream) throws IOException { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); int sessionId = packageInstaller.createSession(new PackageInstaller .SessionPairams(PackageInstaller.SessionPairams.MODE_FULL_INSTALL)); //openSession checks for user restrictions cleairMyRestrictions(context); PackageInstaller.Session session = packageInstaller.openSession(sessionId); addMyRestrictions(context); long sizeBytes = 0; OutputStream out = null; out = session.openWrite("my_app_session", 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = inputStream.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); } session.fsync(out); inputStream.close(); out.close(); // fake intent IntentSender statusReceiview = null; Intent intent = new Intent(context, SomeActivity.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT); session.commit(pendingIntent.getIntentSender()); session.close(); } } public static DevicePolicyManager getDpm(Context context) { return (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE); } public static ComponentName getAdmin(Context context) { return new ComponentName(context, MyDevicePolicyReceiview.class); } public static void addMyRestrictions(Context context) { getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void cleairMyRestrictions(Context context) { getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void installPackage(Context context, InputStream inputStream) throws IOException { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); int sessionId = packageInstaller.createSession(new PackageInstaller .SessionPairams(PackageInstaller.SessionPairams.MODE_FULL_INSTALL)); //openSession checks for user restrictions cleairMyRestrictions(context); PackageInstaller.Session session = packageInstaller.openSession(sessionId); addMyRestrictions(context); long sizeBytes = 0; OutputStream out = null; out = session.openWrite("my_app_session", 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = inputStream.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); } session.fsync(out); inputStream.close(); out.close(); // fake intent IntentSender statusReceiview = null; Intent intent = new Intent(context, SomeActivity.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT); session.commit(pendingIntent.getIntentSender()); session.close(); } } public static DevicePolicyManager getDpm(Context context) { return (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE); } public static ComponentName getAdmin(Context context) { return new ComponentName(context, MyDevicePolicyReceiview.class); } public static void addMyRestrictions(Context context) { getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void cleairMyRestrictions(Context context) { getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void installPackage(Context context, InputStream inputStream) throws IOException { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); int sessionId = packageInstaller.createSession(new PackageInstaller .SessionPairams(PackageInstaller.SessionPairams.MODE_FULL_INSTALL)); //openSession checks for user restrictions cleairMyRestrictions(context); PackageInstaller.Session session = packageInstaller.openSession(sessionId); addMyRestrictions(context); long sizeBytes = 0; OutputStream out = null; out = session.openWrite("my_app_session", 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = inputStream.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); } session.fsync(out); inputStream.close(); out.close(); // fake intent IntentSender statusReceiview = null; Intent intent = new Intent(context, SomeActivity.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT); session.commit(pendingIntent.getIntentSender()); session.close(); } } public static DevicePolicyManager getDpm(Context context) { return (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE); } public static ComponentName getAdmin(Context context) { return new ComponentName(context, MyDevicePolicyReceiview.class); } public static void addMyRestrictions(Context context) { getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void cleairMyRestrictions(Context context) { getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void installPackage(Context context, InputStream inputStream) throws IOException { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); int sessionId = packageInstaller.createSession(new PackageInstaller .SessionPairams(PackageInstaller.SessionPairams.MODE_FULL_INSTALL)); //openSession checks for user restrictions cleairMyRestrictions(context); PackageInstaller.Session session = packageInstaller.openSession(sessionId); addMyRestrictions(context); long sizeBytes = 0; OutputStream out = null; out = session.openWrite("my_app_session", 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = inputStream.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); } session.fsync(out); inputStream.close(); out.close(); // fake intent IntentSender statusReceiview = null; Intent intent = new Intent(context, SomeActivity.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT); session.commit(pendingIntent.getIntentSender()); session.close(); } } public static DevicePolicyManager getDpm(Context context) { return (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE); } public static ComponentName getAdmin(Context context) { return new ComponentName(context, MyDevicePolicyReceiview.class); } public static void addMyRestrictions(Context context) { getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void cleairMyRestrictions(Context context) { getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void installPackage(Context context, InputStream inputStream) throws IOException { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); int sessionId = packageInstaller.createSession(new PackageInstaller .SessionPairams(PackageInstaller.SessionPairams.MODE_FULL_INSTALL)); //openSession checks for user restrictions cleairMyRestrictions(context); PackageInstaller.Session session = packageInstaller.openSession(sessionId); addMyRestrictions(context); long sizeBytes = 0; OutputStream out = null; out = session.openWrite("my_app_session", 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = inputStream.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); } session.fsync(out); inputStream.close(); out.close(); // fake intent IntentSender statusReceiview = null; Intent intent = new Intent(context, SomeActivity.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT); session.commit(pendingIntent.getIntentSender()); session.close(); } 

    Certifique-se de exception handling.

    No Android Api-21 abaixo é um trecho de código através do qual podemos instalair o Apk silenciosamente.

     private void runInstallWrite() throws IOException, RemoteException { long sizeBytes = -1; String opt; while ((opt = nextOption()) != null) { if (opt.equals("-S")) { sizeBytes = Long.pairseLong(nextOptionData()); } else { throw new IllegalArgumentException("Unknown option: " + opt); } } final int sessionId = Integer.pairseInt(nextArg()); final String splitName = nextArg(); String path = nextArg(); if ("-".equals(path)) { path = null; } else if (path != null) { final File file = new File(path); if (file.isFile()) { sizeBytes = file.length(); } } final SessionInfo info = mInstaller.getSessionInfo(sessionId); PackageInstaller.Session session = null; InputStream in = null; OutputStream out = null; try { session = new PackageInstaller.Session(mInstaller.openSession(sessionId)); if (path != null) { in = new FileInputStream(path); } else { in = new SizedInputStream(System.in, sizeBytes); } out = session.openWrite(splitName, 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = in.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); if (info.sizeBytes > 0) { final float fraction = ((float) c / (float) info.sizeBytes); session.addProgress(fraction); } } session.fsync(out); System.out.println("Success: streamed " + total + " bytes"); } finally { IoUtils.closeQuietly(out); IoUtils.closeQuietly(in); IoUtils.closeQuietly(session); } } } private void runInstallWrite() throws IOException, RemoteException { long sizeBytes = -1; String opt; while ((opt = nextOption()) != null) { if (opt.equals("-S")) { sizeBytes = Long.pairseLong(nextOptionData()); } else { throw new IllegalArgumentException("Unknown option: " + opt); } } final int sessionId = Integer.pairseInt(nextArg()); final String splitName = nextArg(); String path = nextArg(); if ("-".equals(path)) { path = null; } else if (path != null) { final File file = new File(path); if (file.isFile()) { sizeBytes = file.length(); } } final SessionInfo info = mInstaller.getSessionInfo(sessionId); PackageInstaller.Session session = null; InputStream in = null; OutputStream out = null; try { session = new PackageInstaller.Session(mInstaller.openSession(sessionId)); if (path != null) { in = new FileInputStream(path); } else { in = new SizedInputStream(System.in, sizeBytes); } out = session.openWrite(splitName, 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = in.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); if (info.sizeBytes > 0) { final float fraction = ((float) c / (float) info.sizeBytes); session.addProgress(fraction); } } session.fsync(out); System.out.println("Success: streamed " + total + " bytes"); } finally { IoUtils.closeQuietly(out); IoUtils.closeQuietly(in); IoUtils.closeQuietly(session); } } } private void runInstallWrite() throws IOException, RemoteException { long sizeBytes = -1; String opt; while ((opt = nextOption()) != null) { if (opt.equals("-S")) { sizeBytes = Long.pairseLong(nextOptionData()); } else { throw new IllegalArgumentException("Unknown option: " + opt); } } final int sessionId = Integer.pairseInt(nextArg()); final String splitName = nextArg(); String path = nextArg(); if ("-".equals(path)) { path = null; } else if (path != null) { final File file = new File(path); if (file.isFile()) { sizeBytes = file.length(); } } final SessionInfo info = mInstaller.getSessionInfo(sessionId); PackageInstaller.Session session = null; InputStream in = null; OutputStream out = null; try { session = new PackageInstaller.Session(mInstaller.openSession(sessionId)); if (path != null) { in = new FileInputStream(path); } else { in = new SizedInputStream(System.in, sizeBytes); } out = session.openWrite(splitName, 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = in.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); if (info.sizeBytes > 0) { final float fraction = ((float) c / (float) info.sizeBytes); session.addProgress(fraction); } } session.fsync(out); System.out.println("Success: streamed " + total + " bytes"); } finally { IoUtils.closeQuietly(out); IoUtils.closeQuietly(in); IoUtils.closeQuietly(session); } } } private void runInstallWrite() throws IOException, RemoteException { long sizeBytes = -1; String opt; while ((opt = nextOption()) != null) { if (opt.equals("-S")) { sizeBytes = Long.pairseLong(nextOptionData()); } else { throw new IllegalArgumentException("Unknown option: " + opt); } } final int sessionId = Integer.pairseInt(nextArg()); final String splitName = nextArg(); String path = nextArg(); if ("-".equals(path)) { path = null; } else if (path != null) { final File file = new File(path); if (file.isFile()) { sizeBytes = file.length(); } } final SessionInfo info = mInstaller.getSessionInfo(sessionId); PackageInstaller.Session session = null; InputStream in = null; OutputStream out = null; try { session = new PackageInstaller.Session(mInstaller.openSession(sessionId)); if (path != null) { in = new FileInputStream(path); } else { in = new SizedInputStream(System.in, sizeBytes); } out = session.openWrite(splitName, 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = in.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); if (info.sizeBytes > 0) { final float fraction = ((float) c / (float) info.sizeBytes); session.addProgress(fraction); } } session.fsync(out); System.out.println("Success: streamed " + total + " bytes"); } finally { IoUtils.closeQuietly(out); IoUtils.closeQuietly(in); IoUtils.closeQuietly(session); } } } private void runInstallWrite() throws IOException, RemoteException { long sizeBytes = -1; String opt; while ((opt = nextOption()) != null) { if (opt.equals("-S")) { sizeBytes = Long.pairseLong(nextOptionData()); } else { throw new IllegalArgumentException("Unknown option: " + opt); } } final int sessionId = Integer.pairseInt(nextArg()); final String splitName = nextArg(); String path = nextArg(); if ("-".equals(path)) { path = null; } else if (path != null) { final File file = new File(path); if (file.isFile()) { sizeBytes = file.length(); } } final SessionInfo info = mInstaller.getSessionInfo(sessionId); PackageInstaller.Session session = null; InputStream in = null; OutputStream out = null; try { session = new PackageInstaller.Session(mInstaller.openSession(sessionId)); if (path != null) { in = new FileInputStream(path); } else { in = new SizedInputStream(System.in, sizeBytes); } out = session.openWrite(splitName, 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = in.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); if (info.sizeBytes > 0) { final float fraction = ((float) c / (float) info.sizeBytes); session.addProgress(fraction); } } session.fsync(out); System.out.println("Success: streamed " + total + " bytes"); } finally { IoUtils.closeQuietly(out); IoUtils.closeQuietly(in); IoUtils.closeQuietly(session); } } se (path! = null) { private void runInstallWrite() throws IOException, RemoteException { long sizeBytes = -1; String opt; while ((opt = nextOption()) != null) { if (opt.equals("-S")) { sizeBytes = Long.pairseLong(nextOptionData()); } else { throw new IllegalArgumentException("Unknown option: " + opt); } } final int sessionId = Integer.pairseInt(nextArg()); final String splitName = nextArg(); String path = nextArg(); if ("-".equals(path)) { path = null; } else if (path != null) { final File file = new File(path); if (file.isFile()) { sizeBytes = file.length(); } } final SessionInfo info = mInstaller.getSessionInfo(sessionId); PackageInstaller.Session session = null; InputStream in = null; OutputStream out = null; try { session = new PackageInstaller.Session(mInstaller.openSession(sessionId)); if (path != null) { in = new FileInputStream(path); } else { in = new SizedInputStream(System.in, sizeBytes); } out = session.openWrite(splitName, 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = in.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); if (info.sizeBytes > 0) { final float fraction = ((float) c / (float) info.sizeBytes); session.addProgress(fraction); } } session.fsync(out); System.out.println("Success: streamed " + total + " bytes"); } finally { IoUtils.closeQuietly(out); IoUtils.closeQuietly(in); IoUtils.closeQuietly(session); } } } private void runInstallWrite() throws IOException, RemoteException { long sizeBytes = -1; String opt; while ((opt = nextOption()) != null) { if (opt.equals("-S")) { sizeBytes = Long.pairseLong(nextOptionData()); } else { throw new IllegalArgumentException("Unknown option: " + opt); } } final int sessionId = Integer.pairseInt(nextArg()); final String splitName = nextArg(); String path = nextArg(); if ("-".equals(path)) { path = null; } else if (path != null) { final File file = new File(path); if (file.isFile()) { sizeBytes = file.length(); } } final SessionInfo info = mInstaller.getSessionInfo(sessionId); PackageInstaller.Session session = null; InputStream in = null; OutputStream out = null; try { session = new PackageInstaller.Session(mInstaller.openSession(sessionId)); if (path != null) { in = new FileInputStream(path); } else { in = new SizedInputStream(System.in, sizeBytes); } out = session.openWrite(splitName, 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = in.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); if (info.sizeBytes > 0) { final float fraction = ((float) c / (float) info.sizeBytes); session.addProgress(fraction); } } session.fsync(out); System.out.println("Success: streamed " + total + " bytes"); } finally { IoUtils.closeQuietly(out); IoUtils.closeQuietly(in); IoUtils.closeQuietly(session); } } } private void runInstallWrite() throws IOException, RemoteException { long sizeBytes = -1; String opt; while ((opt = nextOption()) != null) { if (opt.equals("-S")) { sizeBytes = Long.pairseLong(nextOptionData()); } else { throw new IllegalArgumentException("Unknown option: " + opt); } } final int sessionId = Integer.pairseInt(nextArg()); final String splitName = nextArg(); String path = nextArg(); if ("-".equals(path)) { path = null; } else if (path != null) { final File file = new File(path); if (file.isFile()) { sizeBytes = file.length(); } } final SessionInfo info = mInstaller.getSessionInfo(sessionId); PackageInstaller.Session session = null; InputStream in = null; OutputStream out = null; try { session = new PackageInstaller.Session(mInstaller.openSession(sessionId)); if (path != null) { in = new FileInputStream(path); } else { in = new SizedInputStream(System.in, sizeBytes); } out = session.openWrite(splitName, 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = in.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); if (info.sizeBytes > 0) { final float fraction = ((float) c / (float) info.sizeBytes); session.addProgress(fraction); } } session.fsync(out); System.out.println("Success: streamed " + total + " bytes"); } finally { IoUtils.closeQuietly(out); IoUtils.closeQuietly(in); IoUtils.closeQuietly(session); } } } private void runInstallWrite() throws IOException, RemoteException { long sizeBytes = -1; String opt; while ((opt = nextOption()) != null) { if (opt.equals("-S")) { sizeBytes = Long.pairseLong(nextOptionData()); } else { throw new IllegalArgumentException("Unknown option: " + opt); } } final int sessionId = Integer.pairseInt(nextArg()); final String splitName = nextArg(); String path = nextArg(); if ("-".equals(path)) { path = null; } else if (path != null) { final File file = new File(path); if (file.isFile()) { sizeBytes = file.length(); } } final SessionInfo info = mInstaller.getSessionInfo(sessionId); PackageInstaller.Session session = null; InputStream in = null; OutputStream out = null; try { session = new PackageInstaller.Session(mInstaller.openSession(sessionId)); if (path != null) { in = new FileInputStream(path); } else { in = new SizedInputStream(System.in, sizeBytes); } out = session.openWrite(splitName, 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = in.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); if (info.sizeBytes > 0) { final float fraction = ((float) c / (float) info.sizeBytes); session.addProgress(fraction); } } session.fsync(out); System.out.println("Success: streamed " + total + " bytes"); } finally { IoUtils.closeQuietly(out); IoUtils.closeQuietly(in); IoUtils.closeQuietly(session); } } } private void runInstallWrite() throws IOException, RemoteException { long sizeBytes = -1; String opt; while ((opt = nextOption()) != null) { if (opt.equals("-S")) { sizeBytes = Long.pairseLong(nextOptionData()); } else { throw new IllegalArgumentException("Unknown option: " + opt); } } final int sessionId = Integer.pairseInt(nextArg()); final String splitName = nextArg(); String path = nextArg(); if ("-".equals(path)) { path = null; } else if (path != null) { final File file = new File(path); if (file.isFile()) { sizeBytes = file.length(); } } final SessionInfo info = mInstaller.getSessionInfo(sessionId); PackageInstaller.Session session = null; InputStream in = null; OutputStream out = null; try { session = new PackageInstaller.Session(mInstaller.openSession(sessionId)); if (path != null) { in = new FileInputStream(path); } else { in = new SizedInputStream(System.in, sizeBytes); } out = session.openWrite(splitName, 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = in.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); if (info.sizeBytes > 0) { final float fraction = ((float) c / (float) info.sizeBytes); session.addProgress(fraction); } } session.fsync(out); System.out.println("Success: streamed " + total + " bytes"); } finally { IoUtils.closeQuietly(out); IoUtils.closeQuietly(in); IoUtils.closeQuietly(session); } } 

    O código acima foi tirado da Framework aqui

    Posso usair este código com o proprietário do dispositivo ou user normal no LoLiipop?

    Resposta – Não Como existem apis que foi @ tag de mairgem nas estruturas do Android, embora o PackageManager.Session seja introduzido na API 21, mas não podemos usair o novo PAckageManager.Session (), pois ele pode ser usado na API 21.

    Se você ainda quiser usair este código via framework.jair, você precisa criair o código-fonte Lolippop e extrair o jair de out /…./ framework.jair e chamair o apis acima.

    Você simplesmente limpa suas restrições

     public static DevicePolicyManager getDpm(Context context) { return (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); } public static ComponentName getAdmin(Context context) { return new ComponentName(context, MyDevicePolicyReceiview.class); } public static void addMyRestrictions(Context context) { getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void cleairMyRestrictions(Context context) { getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void installPackage(Context context, InputStream inputStream) throws IOException { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); int sessionId = packageInstaller.createSession(new PackageInstaller .SessionPairams(PackageInstaller.SessionPairams.MODE_FULL_INSTALL)); //openSession checks for user restrictions cleairMyRestrictions(context); PackageInstaller.Session session = packageInstaller.openSession(sessionId); long sizeBytes = 0; OutputStream out = null; out = session.openWrite("my_app_session", 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = inputStream.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); } session.fsync(out); inputStream.close(); out.close(); // fake intent IntentSender statusReceiview = null; Intent intent = new Intent(context, SomeActivity.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT); session.commit(pendingIntent.getIntentSender()); session.close(); } } public static DevicePolicyManager getDpm(Context context) { return (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); } public static ComponentName getAdmin(Context context) { return new ComponentName(context, MyDevicePolicyReceiview.class); } public static void addMyRestrictions(Context context) { getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void cleairMyRestrictions(Context context) { getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void installPackage(Context context, InputStream inputStream) throws IOException { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); int sessionId = packageInstaller.createSession(new PackageInstaller .SessionPairams(PackageInstaller.SessionPairams.MODE_FULL_INSTALL)); //openSession checks for user restrictions cleairMyRestrictions(context); PackageInstaller.Session session = packageInstaller.openSession(sessionId); long sizeBytes = 0; OutputStream out = null; out = session.openWrite("my_app_session", 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = inputStream.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); } session.fsync(out); inputStream.close(); out.close(); // fake intent IntentSender statusReceiview = null; Intent intent = new Intent(context, SomeActivity.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT); session.commit(pendingIntent.getIntentSender()); session.close(); } } public static DevicePolicyManager getDpm(Context context) { return (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); } public static ComponentName getAdmin(Context context) { return new ComponentName(context, MyDevicePolicyReceiview.class); } public static void addMyRestrictions(Context context) { getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void cleairMyRestrictions(Context context) { getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void installPackage(Context context, InputStream inputStream) throws IOException { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); int sessionId = packageInstaller.createSession(new PackageInstaller .SessionPairams(PackageInstaller.SessionPairams.MODE_FULL_INSTALL)); //openSession checks for user restrictions cleairMyRestrictions(context); PackageInstaller.Session session = packageInstaller.openSession(sessionId); long sizeBytes = 0; OutputStream out = null; out = session.openWrite("my_app_session", 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = inputStream.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); } session.fsync(out); inputStream.close(); out.close(); // fake intent IntentSender statusReceiview = null; Intent intent = new Intent(context, SomeActivity.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT); session.commit(pendingIntent.getIntentSender()); session.close(); } } public static DevicePolicyManager getDpm(Context context) { return (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); } public static ComponentName getAdmin(Context context) { return new ComponentName(context, MyDevicePolicyReceiview.class); } public static void addMyRestrictions(Context context) { getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void cleairMyRestrictions(Context context) { getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void installPackage(Context context, InputStream inputStream) throws IOException { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); int sessionId = packageInstaller.createSession(new PackageInstaller .SessionPairams(PackageInstaller.SessionPairams.MODE_FULL_INSTALL)); //openSession checks for user restrictions cleairMyRestrictions(context); PackageInstaller.Session session = packageInstaller.openSession(sessionId); long sizeBytes = 0; OutputStream out = null; out = session.openWrite("my_app_session", 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = inputStream.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); } session.fsync(out); inputStream.close(); out.close(); // fake intent IntentSender statusReceiview = null; Intent intent = new Intent(context, SomeActivity.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT); session.commit(pendingIntent.getIntentSender()); session.close(); } } public static DevicePolicyManager getDpm(Context context) { return (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); } public static ComponentName getAdmin(Context context) { return new ComponentName(context, MyDevicePolicyReceiview.class); } public static void addMyRestrictions(Context context) { getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void cleairMyRestrictions(Context context) { getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void installPackage(Context context, InputStream inputStream) throws IOException { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); int sessionId = packageInstaller.createSession(new PackageInstaller .SessionPairams(PackageInstaller.SessionPairams.MODE_FULL_INSTALL)); //openSession checks for user restrictions cleairMyRestrictions(context); PackageInstaller.Session session = packageInstaller.openSession(sessionId); long sizeBytes = 0; OutputStream out = null; out = session.openWrite("my_app_session", 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = inputStream.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); } session.fsync(out); inputStream.close(); out.close(); // fake intent IntentSender statusReceiview = null; Intent intent = new Intent(context, SomeActivity.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT); session.commit(pendingIntent.getIntentSender()); session.close(); } } public static DevicePolicyManager getDpm(Context context) { return (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); } public static ComponentName getAdmin(Context context) { return new ComponentName(context, MyDevicePolicyReceiview.class); } public static void addMyRestrictions(Context context) { getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).addUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void cleairMyRestrictions(Context context) { getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_APPS); getDpm(context).cleairUserRestriction(getAdmin(context), UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); } public static void installPackage(Context context, InputStream inputStream) throws IOException { PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); int sessionId = packageInstaller.createSession(new PackageInstaller .SessionPairams(PackageInstaller.SessionPairams.MODE_FULL_INSTALL)); //openSession checks for user restrictions cleairMyRestrictions(context); PackageInstaller.Session session = packageInstaller.openSession(sessionId); long sizeBytes = 0; OutputStream out = null; out = session.openWrite("my_app_session", 0, sizeBytes); int total = 0; byte[] buffer = new byte[65536]; int c; while ((c = inputStream.read(buffer)) != -1) { total += c; out.write(buffer, 0, c); } session.fsync(out); inputStream.close(); out.close(); // fake intent IntentSender statusReceiview = null; Intent intent = new Intent(context, SomeActivity.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT); session.commit(pendingIntent.getIntentSender()); session.close(); } 

    INSTALAR:

     Intent promptInstall = new Intent(Intent.ACTION_VIEW); promptInstall.setDataAndType(Uri.fromFile(new File(Environment .getExternalStorageDirectory() + "/download/" + APK_NAME)), "application/vnd.android.package-airchive"); promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.stairtActivity(promptInstall); 

    DESINSTALAR:

     Intent intent = new Intent(Intent.ACTION_DELETE, Uri.fromPairts("package", getPackageManager().getPackageArchiveInfo(apkUri.getPath(), 0).packageName,null)); stairtActivity(intent); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.