2

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?

92Jacko
  • 523
  • 2
  • 10
  • 18
  • where do you register the receiver? – Ron Aug 25 '11 at 19:15
  • Is your service successfully started? – Ron Aug 25 '11 at 19:18
  • is that these two lines: `code`IntentFilter filter = new IntentFilter("STATUS_RETURN"); registerReceiver(MyBroadcastReceiver, filter); `code`?? if so, i have that inside a function (if thats what their called in java) which is called onStart() of my MainActivity – 92Jacko Aug 25 '11 at 19:22
  • and yes, my service does successfully start (i know this as my service saves a file) – 92Jacko Aug 25 '11 at 19:24
  • Seems you are passing the wrong pparameter to setclass method. Check my edit – Ron Aug 25 '11 at 19:41

4 Answers4

2

I guess mismatched "STATUS_REUTRN" and "STATUS_RETURN" have something to do with it?

Update

It seem that you have problems with Intent resolution (scroll down a screen). Intent can be one of:

  • Explicit intent - must have target class defined via setClass().

  • Implicit intent - must have enough info included to be resolved: type, action, category.

So if possible, add this to your broadcast intent:

intentToActivity.setClassx(this, com.example.myapp.MainActivity.class);
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • oh, that was silly of me xD thanks for pointing out that error, but its still not working ): – 92Jacko Aug 25 '11 at 18:37
  • Also, you use `intentToActivity = new ..` and on line later `intent.putExtra("status", "verified")`. Typo? – Peter Knego Aug 25 '11 at 18:53
  • i get an error when trying to use getClass() which suggests to replace it with getClassName(), which doesnt fix the problem :/ – 92Jacko Aug 25 '11 at 19:03
0

Change the visibility of the broadcast receiver to public.

start your service like

startService(new Intent(getApplicationContext(), com.example.myapp.licence.LicenceCheck.class));
Ron
  • 24,175
  • 8
  • 56
  • 97
0

I use AIDL to achieve this. It allows a way to transfer data between your service and activity. http://developer.android.com/guide/developing/tools/aidl.html

It's a lot to learn but that's the best way I know of to achieve this.

IncrediApp
  • 10,303
  • 2
  • 33
  • 24
0

After ages of testing, I've finally found the problem.

I was adding the string in the service to the wrong Intent; adding it accidently to a previously defined one, rather than the one sending a broadcast.

Original: intent.putExtra("status", "verified") ;

Correct: intentToActivity.putExtra("status", "verified");

Thanks again for all the help everyone

92Jacko
  • 523
  • 2
  • 10
  • 18
  • You ignored `Peter Knego`'s comments. He had pointed that out 13hrs ago. – Ron Aug 26 '11 at 07:56
  • Oh D: I never ignored it, I didn't understand what the comment was pointing out at first :/ I'll mark his as the correct answer – 92Jacko Aug 26 '11 at 10:42