-1

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

编辑 根据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();
    }
}

我收到了这些新错误:

03-10 00:16:23.035: E/AndroidRuntime(25773): FATAL EXCEPTION: main
03-10 00:16:23.035: E/AndroidRuntime(25773): java.lang.RuntimeException: Unable to stop service com.example.coverflow.data.downloadManagers@42c042f8: java.lang.IllegalArgumentException: Receiver not registered: com.example.coverflow.data.downloadManagers$progressReceiver@42d09278
03-10 00:16:23.035: E/AndroidRuntime(25773):    at android.app.ActivityThread.handleStopService(ActivityThread.java:2578)
03-10 00:16:23.035: E/AndroidRuntime(25773):    at android.app.ActivityThread.access$2000(ActivityThread.java:140)
03-10 00:16:23.035: E/AndroidRuntime(25773):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
03-10 00:16:23.035: E/AndroidRuntime(25773):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 00:16:23.035: E/AndroidRuntime(25773):    at android.os.Looper.loop(Looper.java:137)
03-10 00:16:23.035: E/AndroidRuntime(25773):    at android.app.ActivityThread.main(ActivityThread.java:4898)
03-10 00:16:23.035: E/AndroidRuntime(25773):    at java.lang.reflect.Method.invokeNative(Native Method)
03-10 00:16:23.035: E/AndroidRuntime(25773):    at java.lang.reflect.Method.invoke(Method.java:511)
03-10 00:16:23.035: E/AndroidRuntime(25773):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-10 00:16:23.035: E/AndroidRuntime(25773):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-10 00:16:23.035: E/AndroidRuntime(25773):    at dalvik.system.NativeStart.main(Native Method)
03-10 00:16:23.035: E/AndroidRuntime(25773): Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.example.coverflow.data.downloadManagers$progressReceiver@42d09278
03-10 00:16:23.035: E/AndroidRuntime(25773):    at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:654)
03-10 00:16:23.035: E/AndroidRuntime(25773):    at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1277)
03-10 00:16:23.035: E/AndroidRuntime(25773):    at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:385)
03-10 00:16:23.035: E/AndroidRuntime(25773):    at com.example.coverflow.data.downloadManagers.onDestroy(downloadManagers.java:47)
03-10 00:16:23.035: E/AndroidRuntime(25773):    at android.app.ActivityThread.handleStopService(ActivityThread.java:2561)
03-10 00:16:23.035: E/AndroidRuntime(25773):    ... 10 more
4

1 回答 1

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);
于 2013-03-09T19:47:00.143 回答