我正在尝试使用广播接收器从服务中发送一个字符串。到达某个位置时,我想发送广播接收器,但广播接收器无法发送任何内容,而且我在 Logcat 中也没有收到任何错误。此外,我也无法在活动或服务中收到任何错误。
以下是我在服务类中的代码:-
public class MyLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
inte.setAction("hello");
inte.putExtra("StringFromService", genre);
inte.addCategory(Intent.CATEGORY_DEFAULT);
sendBroadcast(inte);
}
另一个班级的接收者:-
public class XYZ extends ListActivity {
public BroadcastReceiver myBR= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String x= intent.getAction();
Log.d("INside BroadcastReceiver", "inside" + x);
if(x.equals("hello")){
Toast.makeText(XYZ.this,"hello", Toast.LENGTH_LONG).show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.xyz);
registerReceiver(myBR, new IntentFilter("hello"));
}
}