问题描述
- 新手学Android的广播,为什么接收不到广播数据,急急急急
-
我是动态注册的 ,代码如下:
public class LoginActivity extends Activity implements OnClickListener{
public static String ACTION= "ok";
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
button = (Button) findViewById(R.id.bt);
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt:
//跳转的同时发送广播数据
Intent intent1 = new Intent(this, Activity01.class);
startActivity(intent1);
Intent intent = new Intent(ACTION);
intent.putExtra("info", "hello");
sendBroadcast(intent);
break;
default:
break;
}
}
}接收者如下:
public class Activity01 extends Activity {
private TextView tv;private MyBroadcast myBroad; public static String ACTION = "ok"; private String text; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity01); myBroad = new MyBroadcast(); IntentFilter filter = new IntentFilter(ACTION); registerReceiver(myBroad, filter); tv = (TextView) findViewById(R.id.tv); tv.setText(text); } public class MyBroadcast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { text = intent.getExtras().getString("info"); } } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); unregisterReceiver(myBroad); }
}
为什么我的第二个Activity里接收不到数据呢?求高手指点。
解决方案
广播发过去的时候Activity01的接受者还没注册呢~
解决方案二:
有高手指点下吗?谢谢啦!!!
解决方案三:
有人吗,急急急急急急!
时间: 2024-11-04 02:36:07