我有一个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。 我做错了吗?
答案 0 :(得分:1)
我认为有一个错字......你在使用intent
时使用intento
代替 Intent intento = new Intent("nuovanewsavis");
intento.setAction("nuovanewsavis");
intento.putExtra("com.example.avis.NUOVANEWS", "Nuova notizia");
this.sendBroadcast(intento);
。
{{1}}