获取所有正在运行的BroadcastReceiver

时间:2013-03-09 19:30:45

标签: android broadcastreceiver

我有一个简单的问题,是否可以让我的应用程序全部运行BroadcastReceivers

被修改 的 根据{{​​1}}回答:

DjHacktorReborn

我收到了这些新错误:

   @Override
    public void onDestroy()
    {
        super.onDestroy();
        progressReceiver mProgressReceiver = new progressReceiver();
        unregisterReceiver(mProgressReceiver);
    }

private void calculateNotficationBar()
{
    String currName = null;
    Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
    Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);
    IntentFilter mIntentFilter = new IntentFilter();
    progressReceiver mProgressReceiver = new progressReceiver();
    totalThreadCount = threadArray.length;
    for (int i = 0; i < totalThreadCount; i++)
        if (threadArray[i].getName().contains("DLRUNNING"))
        {
            String[] __tmp = threadArray[i].getName().split("\\|");
            currName = __tmp[0];
            Log.e("Thread is => ", currName);
        }
    mIntentFilter.addAction(currName);
    registerReceiver(mProgressReceiver, mIntentFilter); 
}

private class progressReceiver extends BroadcastReceiver 
{
    private int currentProgress = 0;

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        currentProgress += intent.getIntExtra("DOWNLOAD_UPDATED", 0);
        mNotification.contentView.setProgressBar(R.id.status_progress, 100
                , currentProgress / totalThreadCount, false);
        notificationManager.notify(42, mNotification);  
        if (currentProgress >= 100)
            notificationManager.cancelAll();
    }
}

1 个答案:

答案 0 :(得分:1)

这是如何为headSet插入动态设置广播接收器,同样可以为你想要的其他意图设置

mHeadsetBroadcast = new HeadsetBroadCast();
inf = new IntentFilter();
inf.addAction(Intent.ACTION_HEADSET_PLUG);
registerReceiver(mHeadsetBroadcast, inf);

private class HeadsetBroadCast extends BroadcastReceiver {
@Override
        public void onReceive(Context context, Intent intent) {}
}

OnDestroy()电话

 unregisterReceiver(mHeadsetBroadcast);