5

I was targeting android 4.4 and was starting a service like this

start.Click += delegate {
    StartService(new Intent("com.xamarin.LocationService"));
    start.Enabled = false;
    stop.Enabled = true;
};

and it all worked fine. Now I am targeting 6.0 and find from this thread that this isn't secure, and I should do this:

Intent serviceIntent = new Intent(context,MyService.class);
context.startService(serviceIntent);

but I cannot work out what the arguments for 'new Intent()' should be. The class name is 'LocationActivity' but if I do this

serviceIntent = new Intent(this, typeof(LocationActivity));
context.startService(serviceIntent);

it compiles OK but the service doesn't actually start.

The marked answer in that thread also suggests this

Intent bi = new Intent("com.android.vending.billing.InAppBillingService.BIND");
bi.setPackage("com.android.vending");

but if I try that I find that 'Intent does not contain a definition for setPackage'.

So, can someone help me with a solution here? Thanks in advance.

1 Answer 1

9

If you want to start an activity you need to use StartActivity and not StartService Then use this but change from StartService to StartActivity.

From

serviceIntent = new Intent(this, typeof(LocationActivity));
context.startService(serviceIntent);

To

serviceIntent = new Intent(this, typeof(LocationActivity));
context.StartActivity(serviceIntent);
1
  • Ah! I do want to start a service, not an activity. So my argument should the type of the service I want to start, not the type of the activity it's being started from!. So your answer led me indirectly to the solution: new Intent(this, typeof(LocationService))
    – quilkin
    Jun 8, 2017 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.