0

我有一个BroadcastReceiver内部服务:

public class NotificationClickService extends Service {

    private static final String DEBUG_TAG = "NotificationClickService";

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        registerReceiver(NotificationClickReceiver, new IntentFilter(DownloadManager.ACTION_NOTIFICATION_CLICKED));
    }

    @Override
      public void onDestroy() {
        unregisterReceiver(NotificationClickReceiver);
    }

    BroadcastReceiver NotificationClickReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(DEBUG_TAG, "NotificationClickReceiver: onReceive CALLED");

                Intent i = new Intent(android.app.DownloadManager.ACTION_VIEW_DOWNLOADS);
                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);
        }
    };
}

这将系统下载管理器带到了最前面。

在我的手机上,我正在运行基于 JellyBean 的 CyanogenMod 10.1。

但...

一旦系统应用 CMupdater 启动:

  • 如果CMupdater当前正在运行,则从我的服务的 BroadcastReceiver 调用它,而不是DownloadManager;
  • 如果CMupdater没有运行,但至少运行了一次,则根本不会调用我的接收器

如果我重新启动并且不运行更新程序,它会再次工作。所有测试也在我的平板电脑上使用相应的 CyanogenMod 10.1 版本进行了测试。

这是来自 CM 的接收器:

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>

android.intent.action.DOWNLOAD_NOTIFICATION_CLICKEDDownloadManager.ACTION_NOTIFICATION_CLICKED是我使用的意图的常量值。

4

2 回答 2

0

虽然我仍然无法解读您的问题,但我看不出您的接收器会如何被调用。您的意图过滤器中缺少类别注册。

尝试:

IntentFilter filter = new IntentFilter(DownloadManager.ACTION_NOTIFICATION_CLICKED); 
filter.addCategory(Intent.CATEGORY_HOME);
registerReceiver(NotificationClickReceiver, filter);

这应该确保您始终收到与清单注册接收器相同的广播。

于 2013-02-25T14:14:23.877 回答
0

所有匹配BroadcastRecievers都会收到通知,并带有相应的Context. 请注意,这可能以两种方式发生:

  1. 如果 Receiver 是在代码中本地创建并在某个对象内注册的,则仅当该对象存在时才会调用它。

  2. 如果在清单中声明了 Reciever ,则将创建该对象并onReceive()调用方法。

因此,您可以实现 Service 类BroadCastReciever,在 Manifest 文件中声明它,并从它的形式启动您的服务onRecieve()

于 2013-02-25T14:15:18.043 回答