我正在尝试在我的申请中填写this tutorial。由于我有一个PCL App.cs,我试图将该代码添加到我的MainActivity.cs.
但是,当我构建项目时,似乎我的MainActivity
丢失或我的清单不正确。
的 MainActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Plugin.Messaging;
using Plugin.Permissions;
using Plugin.Permissions.Abstractions;
using System.Threading.Tasks;
namespace Divco.Droid
{
[Activity(Label = "Divco.Droid", Icon = "@drawable/logo_size_icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public event EventHandler<ServiceConnectedEventArgs> LocationServiceConnected = delegate { };
protected static LocationServiceConnection locationServiceConnection;
public static MainActivity Current
{
get { return current; }
}
private static MainActivity current;
public LocationService LocationService
{
get
{
if (locationServiceConnection.Binder == null)
throw new Exception("Service not bound yet");
// note that we use the ServiceConnection to get the Binder, and the Binder to get the Service here
return locationServiceConnection.Binder.Service;
}
}
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
Xamarin.FormsMaps.Init(this, bundle);
CrossMessaging.Current.Settings().Phone.AutoDial = true;
LoadApplication(new App());
MainActivity.StartLocationService();
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
}
//public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
//{
// PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
//}
public static void StartLocationService()
{
// create a new service connection so we can get a binder to the service
locationServiceConnection = new LocationServiceConnection(null);
// this event will fire when the Service connectin in the OnServiceConnected call
locationServiceConnection.ServiceConnected += (object sender, ServiceConnectedEventArgs e) => {
Console.WriteLine("Service Connected");
};
// Starting a service like this is blocking, so we want to do it on a background thread
new Task(() => {
// Start our main service
Console.WriteLine("App", "Calling StartService");
Android.App.Application.Context.StartService(new Intent(Android.App.Application.Context, typeof(LocationService)));
// bind our service (Android goes and finds the running service by type, and puts a reference
// on the binder to that service)
// The Intent tells the OS where to find our Service (the Context) and the Type of Service
// we're looking for (LocationService)
Intent locationServiceIntent = new Intent(Android.App.Application.Context, typeof(LocationService));
Console.WriteLine("App", "Calling service binding");
// Finally, we can bind to the Service using our Intent and the ServiceConnection we
// created in a previous step.
Android.App.Application.Context.BindService(locationServiceIntent, locationServiceConnection, Bind.AutoCreate);
}).Start();
}
public static void StopLocationService()
{
// Check for nulls in case StartLocationService task has not yet completed.
Console.WriteLine("App", "StopLocationService");
// Unbind from the LocationService; otherwise, StopSelf (below) will not work:
if (locationServiceConnection != null)
{
Console.WriteLine("App", "Unbinding from LocationService");
Android.App.Application.Context.UnbindService(locationServiceConnection);
}
// Stop the LocationService:
if (Current.LocationService != null)
{
Console.WriteLine("App", "Stopping the LocationService");
Current.LocationService.StopSelf();
}
}
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="9" android:versionName="0.1" package="com.divcodelivery.divco">
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_SURFACE_FLINGER" />
<application android:label="Divco" android:icon="@drawable/logo_size_icon"></application>
</manifest>
以下是我尝试构建手机时的结果:
am start -n“com.divcodelivery.divco / md58047ec786dcfdff107cc37976c308625.MainActivity” 开始:意图{cmp = com.divcodelivery.divco / md58047ec786dcfdff107cc37976c308625.MainActivity} 错误类型3 错误:活动类{com.divcodelivery.divco / md58047ec786dcfdff107cc37976c308625.MainActivity}不存在。
无法启动应用:设备找不到名为的组件:com.divcodelivery.divco / md58047ec786dcfdff107cc37976c308625.MainActivity