Basically, my problem is that i need to send data from my service (started with Intent) to my main activity. (this is my first attempt at using a BroadcastReceiver)
I have two apps, com.example.myapp (contains main activity) and com.example.myapp.licence (contains background service)
What i currently have is as follows:
com.example.myapp > MainActivity:
// Start background service
Intent intent = new Intent();
intent.setClassName("com.example.myapp.licence", "com.example.myapp.licence.LicenceCheck");
startService(intent);
// Setup BroadcastReceiver
private BroadcastReceiver MyBroadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String status = extras.getString("status");
}
};
// Start reciever
IntentFilter filter = new IntentFilter("STATUS_RETURN");
registerReceiver(MyBroadcastReceiver, filter);
com.example.myapp.licence > LicenceCheck:
Intent intentToActivity = new Intent("STATUS_RETURN");
intent.putExtra("status", "verified");
sendBroadcast(intentToActivity);
can anyone please suggest what i need to change to get this BroadcastReceiver to work?
EDIT: Edited to correct a typo ..problem still exists
EDIT 2: It now seems now that the BroadcastReciever is being used, but theres a problem with the following line (as it force closes on it):
String status = extras.getString("status");
EDIT 3: Above problem fixed by surrounding that line with the following IF statment:
if (extras != null)
but "extras" seems to always be null, anyone know why?