0

I have an activity with two BroadcastReceiver. on ICS emulator both completeReceiver and clickReceiver work, while on my phone with JB only the first one works.

I really can't imagine WHY. Thanks for any help.

(what I tried was to change ShareActivity.this with context in the AlertDialog.Buider: but same result).

public class ShareActivity extends Activity {
    // stuff
    @Override
    protected void onStart() {
        super.onStart();
        registerReceiver(completeReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        registerReceiver(clickReceiver, new IntentFilter(DownloadManager.ACTION_NOTIFICATION_CLICKED));
        Log.v(DEBUG_TAG, "_onStart");
    }

    // other stuff

    BroadcastReceiver completeReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
                long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -2);
                if (enqueue != -1 && id != -2 && id == enqueue) {
                    Query query = new Query();
                    query.setFilterById(id);
                    dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
                    Cursor c = dm.query(query);
                    if (c.moveToFirst()) {
                        int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                        int status = c.getInt(columnIndex);
                        if (status == DownloadManager.STATUS_SUCCESSFUL) {
                        AlertDialog.Builder helpBuilder = new AlertDialog.Builder(ShareActivity.this);
                        helpBuilder.setIcon(android.R.drawable.ic_dialog_info);
                        helpBuilder.setTitle(getString(R.string.information));
                        helpBuilder.setMessage(getString(R.string.download_complete_dialog_msg1) + titleRaw + getString(R.string.download_complete_dialog_msg2));
                        helpBuilder.setPositiveButton(getString(R.string.download_complete_dialog_positive), new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int which) {

                                Intent v_intent = new Intent();
                                v_intent.setAction(android.content.Intent.ACTION_VIEW);
                                v_intent.setDataAndType(videoUri, "video/*");
                                startActivity(v_intent);
                            }
                        });

                        helpBuilder.setNegativeButton(getString(R.string.dialogs_negative), new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int which) {
                                    // cancel
                            }
                        });

                        AlertDialog helpDialog = helpBuilder.create();
                        if (! ((Activity) context).isFinishing()) {
                                helpDialog.show();
                        }
                    }
                }
            }
        }
    };

    BroadcastReceiver clickReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                    long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -2);
                    if (enqueue != -1 && id != -2 && id == enqueue) {
                            Query query = new Query();
                            query.setFilterById(id);
                            dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
                            Cursor c = dm.query(query);
                            if (c.moveToFirst()) {
                                    int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                                    int status = c.getInt(columnIndex);
                                    if (status == DownloadManager.STATUS_RUNNING ||
                                            status == DownloadManager.STATUS_PAUSED ||
                                            status == DownloadManager.STATUS_PENDING) {
                                AlertDialog.Builder helpBuilder = new AlertDialog.Builder(ShareActivity.this);
                                helpBuilder.setIcon(android.R.drawable.ic_dialog_info);
                                helpBuilder.setTitle(getString(R.string.cancel_download_dialog_title));
                                helpBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int which) {
                                    dm.remove(enqueue);
                                    Log.d(DEBUG_TAG, "download cancelled");
                            }
                        });

                        helpBuilder.setNegativeButton(getString(R.string.dialogs_negative), new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int which) {
                                    // cancel
                            }
                        });

                        AlertDialog helpDialog = helpBuilder.create();
                        if (! ((Activity) context).isFinishing()) {
                                helpDialog.show();
                        }
                    }
                }
            }
        }
    };
}
dentex
  • 3,223
  • 4
  • 28
  • 47

1 Answers1

0

the problem it's related to the CyanogenMod I'm using. specifically package CM updater should be involved. at phone boot and until the updater app has never ran, the receiver works. after the first time the updater is accessed, my app's receiver above stops working.

this is from the cmupdater receiver:

receiver:

package com.cyanogenmod.updater.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.cyanogenmod.updater.UpdatesSettings;

public class NotificationClickReceiver extends BroadcastReceiver{
     private static String TAG = "NotificationClickReceiver";

     @Override
     public void onReceive(Context context, Intent intent) {

         // Bring the main app to the foreground
         Intent i = new Intent(context, UpdatesSettings.class);
         i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP |
                 Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
         context.startActivity(i);
     }
 }

manifest:

    <receiver android:name="com.cyanogenmod.updater.receiver.NotificationClickReceiver">
        <intent-filter>
            <action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"/>
            <category android:name="android.intent.category.HOME"/>
        </intent-filter>
    </receiver>

I changed my code to bring the system download manager to the front, when clicking on a download from the notification bar. if cmupdater is running, it is called instead. then, nothing is called. this led me to think a about the probable link between them.

maybe this is not a proper answer, because I don't have a solution. but that's it. all has been tested also on another device with the same ROM. while on emulator all works OK.

dentex
  • 3,223
  • 4
  • 28
  • 47
  • Evolved in: http://stackoverflow.com/questions/15068353/is-it-possible-that-two-broadcastreceiver-from-two-apps-based-on-the-same-broad Maybe this answer can be removed, basically because it leads to a new question... – dentex Feb 25 '13 at 13:44