我有这样的问题 - "我的广播"没有收到任何信息。我做错了什么?
代码示例
我的广播注册活动:
主要活动
DriverCheckBroadcast broadcast = new DriverCheckBroadcast(this);
IntentFilter intFilt = new IntentFilter("MY_BROADCAST_CHECK");
// регистрируем (включаем) BroadcastReceiver
registerReceiver(broadcast, intFilt);
startService(new Intent(this, DriverCheckService.class));
将包裹放入广播的方法
@Override
public void onResponse(Response<Example> response, Retrofit retrofit) {
Example result = response.body();
LatLng driverlatlang = new LatLng(result.getGpsLatitude(),result.getGpsLongitude());
intent.putExtra("address",customizeText(result.getAddress()));
intent.putExtra("latlng",driverlatlang);
intent.putExtra("model",result.getModel().getName());
intent.putExtra("brand",result.getModel().getIdBrand());
status = result.getStatusName();
service.sendBroadcast(intent);
}
在广播中获得包裹的方法
@Override
public void onReceive(Context context, Intent intent) {
activity.driverlatlang = intent.getParcelableExtra("latlang");
activity.model.setText((CharSequence) intent.getParcelableExtra("model"));
activity.brand.setText((CharSequence) intent.getParcelableExtra("brand"));
activity.address.setText((CharSequence) intent.getParcelableExtra("address"));
}
答案 0 :(得分:0)
您需要在发送广播时设置操作。
试试这个:
Comma-separated list of tags to apply to the user. Currently supported by the management plugin:
management
User can access the management plugin
policymaker
User can access the management plugin and manage policies and parameters for the vhosts they have access to.
monitoring
User can access the management plugin and see all connections and channels as well as node-related information.
administrator
User can do everything monitoring can do, manage users, vhosts and permissions, close other user's connections, and manage policies and parameters for all vhosts.
Note that you can set any tag here; the links for the above four tags are just for convenience.
因为您的接收器方面键入:Intent intent = new Intent();
intent.setAction("MY_BROADCAST_CHECK");
但是当您发送广播时您没有给出任何信息,这就是您的接收方无法获取任何信息的原因。
答案 1 :(得分:0)
创建意图并以这种方式发送广播
Intent i = new Intent("a");
Bundle bundle = new Bundle();
bundle.putString(USER_ID, user_id);
i.putExtras(bundle);
mContext.sendBroadcast(i);
意向声明过滤器
IntentFilter filter = new IntentFilter();
filter.addAction("a");
filter.addAction("b");
filter.addAction("c");
接收广播
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
try {
if(intent.getAction().equals("a"))
goToPage(new a());
if(intent.getAction().equals("b"))
{
// you can call other event and function here
Fragment fragment = new b();
Bundle bundle = intent.getExtras();
fragment.setArguments(bundle);
goToPage(fragment);
}
if(intent.getAction().equals("c"))
{
// you can call other event and function here
Fragment fragment = new c();
Bundle bundle = intent.getExtras();
fragment.setArguments(bundle);
goToPage(fragment);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, filter);
答案 2 :(得分:-1)
您应该使用intent
从Bundle
接收数据:
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle extras = intent.getExtras();
if (extras != null) {
Object value1=extras.get("latlang");
Object value2=extras.get("model");
Object value3=extras.get("brand");
Object value4=extras.get("address");
}
}
然后settext
相应的值。