I'm using Android Studio 1.3, I have an IntentService
setup that gathers some data and sends it out via a LocalBroadcastManager
as so:
IntentService
public class cService extends IntentService {
public cService(){
super("cService");
}
@Override
protected void onHandleIntent(Intent intent) {
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
Intent broadcastIntent = new Intent("action.getTestData");
broadcastIntent.putExtra("data", response.toString());
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcastIntent);
}
}
The above code runs, but the Broadcast is never captured.
AndroidManifest.xml
<service android:name=".services.cService"
android:exported="false">
</service>
<receiver android:name=".services.Receiver">
</receiver>
Receiver which extends BroadcastReceiver (Never gets called)
public class Receiver extends BroadcastReceiver
{
protected MainActivity mainActivityContext;
public Receiver() {}
@Override
public void onReceive(Context context, Intent intent) {
final String data = intent.getStringExtra("data");
mainActivityContext.runOnUiThread(new Runnable() {
@Override
public void run() {
TextView txtData = (TextView)mainActivityContext.findViewById(R.id.textView2);
txtData.setText(data);
}
});
}
}
MainActivity onCreate()
public class MainActivity extends Activity implements View.OnClickListener{
TextView txtData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Dummy text field that should populate with something upon
//onReceive() executing...
txtData = (TextView)findViewById(R.id.textView2);
txtData.setText("Ready.");
IntentFilter filter = new IntentFilter("action.getTestData");
filter.addDataScheme("string");
BroadcastReceiver c = new Receiver();
LocalBroadcastManager.getInstance(this).registerReceiver(c,filter);
}
What am I doing wrong here that's causing the Broadcast to not be received? I'm thinking it has to do with my IntentFilter
?
You may try setting
android:exported=true
in the<service>
block of your XML. According to the docs:http://developer.android.com/guide/topics/manifest/service-element.html#exported
Hope this helps.
I managed to fix the problem I had by incorporating
BroadcastReceiver
withinonResume()
of myMainActivity
. My goal here was to simply capture that data once theService
completed retrieving all network data.I hope this helps someone...
I then have another classe called
WebServiceReceiver
, this class will handle the result from the webservice, it will prepare the data and then create the intent that ouronResume()
Override method will filter and read in.Things started working for me once I replaced
with
above. Of course, this only applies if you're using a local service, internal to your app.
Try this: