2013-01-16 44 views
5

Desidero installare un file apk e impostare un destinatario broadcast per raccogliere informazioni sullo stato dell'installazione.Android: BroadcastReceiver su installazione/disinstallazione applicazione

ho preparato una classe BroadcastReceiver:

public class newPackageReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d("DEBUG"," test for application install/uninstall"); 
    } 

} 

Nell'attività principale, ho registro un nuovo oggetto ricevitore, quindi il pulsante instanciate per l'applicazione di installazione.

public void onCreate(Bundle savedInstanceState) { 
... 
IntentFilter filter = new IntentFilter(); 
     filter.addAction(Intent.ACTION_PACKAGE_ADDED); 
     filter.addAction(Intent.ACTION_PACKAGE_CHANGED); 
     filter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED); 
     filter.addAction(Intent.ACTION_PACKAGE_INSTALL); 
     filter.addAction(Intent.ACTION_PACKAGE_REMOVED); 
     filter.addAction(Intent.ACTION_PACKAGE_REPLACED); 
     filter.addAction(Intent.ACTION_PACKAGE_RESTARTED); 

     receiver = new newPackageReceiver(); 
     registerReceiver(receiver, filter); 
     ... 

dlButton.setText(R.string.dl_button); 
dlButton.setOnClickListener(new AppliDownloadOnClickListener(this)); 


@Override 
public void onDestroy(){ 
    unregisterReceiver(receiver); 
    super.onDestroy(); 
} 

Nella mia classe OnclickListener, ho messo:

@Override 
    public void onClick(View v) { 

    // actually, the below process is in an asyncTask 
    URL url; 
    Intent promptInstall; 

    try { 
     url = new URL(apkurl); 

     HttpURLConnection c = (HttpURLConnection) url.openConnection(); 
     c.setRequestMethod("GET"); 
     c.setDoOutput(true); 
     c.connect(); 

     String PATH = Environment.getExternalStorageDirectory()+ "/download/"; 
     File file = new File(PATH); 
     file.mkdirs(); 
     File outputFile = new File(file, "app.apk"); 
     FileOutputStream fos = new FileOutputStream(outputFile); 

     InputStream is = c.getInputStream(); 

     byte[] buffer = new byte[1024]; 
     int len1 = 0; 
     while ((len1 = is.read(buffer)) != -1) { 
      fos.write(buffer, 0, len1); 
     } 

     fos.close(); 
     is.close(); 

     promptInstall = new Intent(Intent.ACTION_VIEW); 
     promptInstall.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")), "application/vnd.android.package-archive"); 

     if (promptInstall != null) { 
      activity.startActivity(promptInstall); 
     } else { 
      ErrorDetails.displayToastMessage(activity,R.string.connection_error); 
     } 


    } catch (...) { 
     ... 
    } 

} 

Con il codice di cui sopra (ho ridotto di esso), quando fa clic sul pulsante, viene visualizzato di installazione e applicazione è installata perfettamente, ma di classe ricevente (newPackageReceiver) non viene mai chiamato. La registrazione (registerReceiver) viene eseguita nel metodo onCreate e unregisterReceiver viene chiamato nel metodo onDestroy, quindi è valido. Sai perché ?

Grazie per la lettura!

risposta

5

È necessario aggiungere lo data scheme al filtro intent.

filter.addDataScheme("package"); 

Inoltre, ACTION_PACKAGE_INSTALL non è mai stato utilizzato.

+0

Perfetto ~ Funziona! Grazie per lo schema di dati e informazioni su ACTION_PACKAGE_INSTALL ~ – johann

+0

Funziona bene, ma perché? – aotian16

Problemi correlati