quilkin
quilkin

Reputation: 1447

Xamarin.Android: starting a new intent

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.

Upvotes: 5

Views: 9642

Answers (1)

Matheus Souza
Matheus Souza

Reputation: 116

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);

Upvotes: 9

Related Questions