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.

Community
  • 1
  • 1
quilkin
  • 874
  • 11
  • 31

1 Answers1

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);
  • 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 08 '17 at 15:03