0

我没有成功注册或使用我的broadCastReceiver.

这是我的代码:

   public class ReciverChangeProgress extends BroadcastReceiver{

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        android.util.Log.e("ReciverChagne", "IReciver");
    }

}

public ReciverChangeProgress reciverChangeProgress;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.e("ReciverChange","onCreate");
    setContentView(R.layout.activity_progress);
    progressBar= (ProgressBar) findViewById(R.id.progressBar1);
    Log.e("ReciverChange","setProgress");
    progressBar.setProgress(UploadService.lastPercent);
    IntentFilter intentFilter=new IntentFilter("commoshe");
    registerReceiver(reciverChangeProgress,intentFilter);
    android.util.Log.e("ReciverChagne", "register");
    sendBroadcast(new Intent("commoshe"));
    android.util.Log.e("ReciverChagne", "Use");

}

当我运行 Activity 时,这是 LogCat: ReciverChange, onCreate ReciverChange, setProgresS ReciverChange, regiseter ReciverChange, use

但是 onReceive 函数从未调用过。

谢谢您的帮助 :)

4

2 回答 2

1

我成功了。

我忘了这个:

reciverChangeProgress=new ReciverChangeProgress();

我们需要将新对象设置为我们的 broadcaseReceiver 的新实例。

于 2013-01-06T16:02:54.287 回答
0
reciverChangeProgress=new ReciverChangeProgress();

onReceive()永远不会被调用。因为从未发送过广播。您刚刚注册接收 BroadCastReceiver

要发送广播,请尝试使用正确的这些方法IntentFilter

Intent intent = new Intent("commoshe");
sendBroadcast(intent);

希望它可以帮助你!

于 2013-01-06T16:03:58.737 回答