Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: BroadcastReceiver on application install / uninstall

I want to install an apk file and set a broadcast-receiver in order to catch information concerning install status.

I have prepared a BroadcastReceiver class :

public class newPackageReceiver extends BroadcastReceiver {

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

}

In the main activity, I first register a new receiver object, then instanciate button for application install.

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

In my OnclickListener class, i put :

@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 (...) {
        ...
    }

}

With the above code (I have shrunk it), when button is clicked, installer is displayed and application is perfectly installed, but receiver class(newPackageReceiver) is never called. Registering (registerReceiver) is done in the onCreate method and unregisterReceiver is called in the onDestroy method, so it shoud be valid. Do you know why ?

Thank you for reading !

like image 995
johann Avatar asked Jan 16 '13 02:01

johann


1 Answers

You need to add the data scheme to your intent filter.

filter.addDataScheme("package");

Also, ACTION_PACKAGE_INSTALL was never in use.

like image 103
Andy McSherry Avatar answered Nov 16 '22 03:11

Andy McSherry