Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 981
quilkin Avatar asked Jun 08 '17 10:06

quilkin


1 Answers

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);
like image 177
Matheus Souza Avatar answered Oct 04 '22 20:10

Matheus Souza