我的接收器无法正常工作。问题在于阶级和活动之间。即时通讯发送消息从班级发送到活动但不工作... 该课程将与两个活动进行交流,一次一个......
我的广播代码(活动内):
public class MainActivity extends Activity{
BroadcastReceiver receiver;
IntentFilter filter;
private void init() {
try {
...
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("HAS_CONECT")){
progressBar.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "IS CONECTED!", Toast.LENGTH_LONG).show();
}
else if(action.equals("ARRIVES")){
Toast.makeText(getApplicationContext(), "HAS ARRIVE!!!", Toast.LENGTH_LONG).show();
}
}
};
filter = new IntentFilter("HAS_CONECT");
registerReceiver(receiver, filter);
filter = new IntentFilter("ARRIVES");
registerReceiver(receiver, filter);
} catch (Exception e) {
System.out.println(e);
}
}
...
}
我的代码类:
public void adviseActivity(){
try {
Intent i = new Intent("MESSAGE");
i.putExtra("msg", "DATA ARRIVE");
i.setAction("ARRIVES");
context.sendBroadcast(i);
} catch (Exception e) {
System.out.print(e);
}
}
答案 0 :(得分:0)
问题在于您的onReceive
方法,您正在寻找错误的操作。您设置的操作为MESSAGE
,但您要查找的onReceive
操作为HAS_CONECT
和ARRIVES
。
更正了以下代码:
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("MESSAGE")){
progressBar.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "IS CONECTED!", Toast.LENGTH_LONG).show();
}
}