我有一个MainClass,它注册一个BroadcastReceiver并重写它的onReceive()方法
BroadcastReceiver broadcastReceiver=new BroadcastReceiver(){
@Override
public void onReceive(Context context,Intent intent){
Log.w("MyApp","RICEVUTO MESSAGGIO BROADCAST: "+intent.getStringExtra("com.example.avis.NUOVANEWS"));
Log.w("MyApp","Intent TOSTRING "+intent.toString());
}
};
this.getApplicationContext().registerReceiver(broadcastReceiver, new IntentFilter("nuovanewsavis"));在NewsService类中,当发现新消息时,我广播了一条新消息。这是密码
Intent intento=new Intent("nuovanewsavis");
intent.setAction("nuovanewsavis");
intent.putExtra("com.example.avis.NUOVANEWS", "Nuova notizia");
this.sendBroadcast(intento);MainClass无法获得额外的信息:如果我试图通过getStringExtra读取它,就会得到一个null。我做错什么了吗?
发布于 2014-04-01 15:43:37
我认为有一个typo...your使用的是intent,而不是intento。
Intent intento = new Intent("nuovanewsavis");
intento.setAction("nuovanewsavis");
intento.putExtra("com.example.avis.NUOVANEWS", "Nuova notizia");
this.sendBroadcast(intento);https://stackoverflow.com/questions/22790552
复制相似问题