带有活动的广播接收器服务反馈不起作用
Posted
技术标签:
【中文标题】带有活动的广播接收器服务反馈不起作用【英文标题】:Broadcast Receiver service feedback with activity not work 【发布时间】:2018-11-27 03:37:23 【问题描述】:我为带有活动的服务反馈创建了一个广播接收器。但这不起作用。调试器 -- 从服务调用 sendBroadcast 后,跳过此 (LocalBroadcastManager.getInstance(this).sendBroadcast(RTReturn);) 行并且不会导致 Activity
This is Activity
package com.example.my;
BroadcastReceiver br;
public static final String RECEIVE_JSON =
"com.example.my.RECEIVE_JSON";
public class cama extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
xcxc();
xcx();
public void xcxc()
br = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
if(intent.getAction().equals(RECEIVE_JSON))
String serviceJsonString = intent.getStringExtra("json");
Toast toast = Toast.makeText(getApplicationContext(),
"Test!", Toast.LENGTH_SHORT);
toast.show();
;
public void xcx()
LocalBroadcastManager bManager;
bManager = LocalBroadcastManager.getInstance(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(RECEIVE_JSON);
bManager.registerReceiver(br, intentFilter);
@Override
public void onDestroy()
super.onDestroy();
if (br != null)
unregisterReceiver(br);
这是服务
package com.example.my;
public class MyService extends Service
@Override
public void onCreate()
do_anything();
public void do_anything()
Intent RTReturn = new Intent(cama.RECEIVE_JSON);
RTReturn.putExtra("json", "ole");
LocalBroadcastManager.getInstance(this).sendBroadcast(RTReturn);
【问题讨论】:
提供完整的活动广播代码 这个完整的鳕鱼在 Activity 中带有 BroadcastReceiver 您是如何在onCreate()
中创建xcxc()
的??
受保护的 void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_cama); xcxc();
显示你的清单文件...
【参考方案1】:
只在onCreate(Bundle savedInstanceState)
内部使用:
LocalBroadcastManage
mlocal = LocalBroadcastManager.getInstance(getApplicationContext());
mlocal.sendBroadcast(YOUR Reciever)
【讨论】:
以上是关于带有活动的广播接收器服务反馈不起作用的主要内容,如果未能解决你的问题,请参考以下文章