0

我有两个活动BroadcastReceiver。在ICS模拟器上都可以工作,completeReceiverclickReceiver在我的手机上JB只有第一个可以工作。

我真的无法想象为什么。谢谢你的帮助。

(我尝试的是在: 中进行更改ShareActivity.this,但结果相同)。contextAlertDialog.Buider

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

1 回答 1

0

它与我正在使用的 CyanogenMod 相关的问题。特别是包 CM 更新程序应该参与。在手机启动时,直到更新程序应用程序从未运行,接收器才能工作。第一次访问更新程序后,我上面的应用程序的接收器停止工作。

这是来自 cmupdater 接收器:

接收者:

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

显现:

    <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.

于 2013-02-24T21:56:53.700 回答