Android,两个几乎完全相同的BroadcastReceiver:两者都在ICS模拟器上运行,在JB手机上只有一个......但是为什么?

时间:2013-02-22 11:42:32

标签: android broadcastreceiver

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

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

(我尝试的是在ShareActivity.this中使用context更改AlertDialog.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();
                        }
                    }
                }
            }
        }
    };
}

1 个答案:

答案 0 :(得分:0)

与我正在使用的CyanogenMod相关的问题。应该涉及特定包CM更新程序。在电话启动时,直到更新程序应用程序从未运行,接收器工作。在第一次访问更新程序后,我的app上面的接收器停止工作。

这是来自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>

当我点击通知栏中的下载内容时,我更改了代码以将系统下载管理器置于最前面。如果cmupdater正在运行,则会调用它。然后,什么也没有。这让我想到了他们之间可能存在的联系。

也许这不是一个正确的答案,因为我没有解决方案。但就是这样。所有这些都已在另一台具有相同ROM的设备上进行了测试。在模拟器上一切正常。