函数onReceive从未调用过

时间:2013-01-06 15:45:57

标签: android

我没有成功注册或使用我的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函数从未调用。

感谢您的帮助:)

2 个答案:

答案 0 :(得分:1)

我成功了。

我忘了这个:

reciverChangeProgress=new ReciverChangeProgress();

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

答案 1 :(得分:0)

reciverChangeProgress=new ReciverChangeProgress();
永远不会调用

onReceive()。因为BroadCast从未发送过。您刚刚注册接收BroadCastReceiver

要发送BroadCast,请使用正确的IntentFilter

尝试这些方法
Intent intent = new Intent("commoshe");
sendBroadcast(intent);

希望它可以帮到你!