【发布时间】:2019-06-10 18:39:56
【问题描述】:
我创建了一个服务来在前台服务中运行 SignalR。当我启动服务时,我没有收到任何错误,但永远不会调用 OnCreate 和 OnStartCommand。是的,我检查了清单中的 ForeGround_Service。我用来启动服务的代码是:
public void StartForegroundServiceComapt<SignalRSrv>(Context context, Bundle args = null) where SignalRSrv : Service
{
var intent = new Intent(context, typeof(SignalRSrv));
if (args != null)
{
intent.PutExtras(args);
}
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
context.StartForegroundService(intent);
}
else
{
context.StartService(intent);
}
}
服务代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Acr.UserDialogs;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Support.V4.App;
using Android.Views;
using Android.Widget;
using Java.Lang;
using Java.Util.Concurrent;
using Microsoft.AspNet.SignalR.Client;
using Microsoft.AspNet.SignalR.Client.Transports;
namespace MyService_Android
{
public class SignalRSrv : Service
{
private bool InstanceFieldsInitialized = false;
private void InitializeInstanceFields()
{
mBinder = new LocalBinder(this);
}
private Handler mHandler; // to display Toast message
private IBinder mBinder; // Binder given to clients
private SignalRSingleton mInstance;
internal const string MY_ACTION = "MY_ACTION";
public SignalRSrv()
{
if (!InstanceFieldsInitialized)
{
InitializeInstanceFields();
InstanceFieldsInitialized = true;
}
}
public override void OnCreate()
{
base.OnCreate();
mInstance = SignalRSingleton.getInstance();
mHandler = new Handler(Looper.MainLooper);
}
public override void OnDestroy()
{
mInstance.mHubConnection.Stop();
base.OnDestroy();
}
public override IBinder OnBind(Intent intent)
{
Bundle bundlee = intent.GetBundleExtra("TheBundle");
startSignalR();
return mBinder;
}
private void startSignalR()
{
mInstance.setmHubConnection();
mInstance.setHubProxy();
try
{
// Connect the client to the hup
mInstance.mHubConnection.Start();
// Set the event handler
mInstance.WireUp();
}
catch (System.Exception e) when (e is InterruptedException || e is ExecutionException)
{
//opps
var x = 1;
return;
}
}
public async void showMessage(string message)
{
var result = await UserDialogs.Instance.ConfirmAsync(new ConfirmConfig
{
Message = "Text Message from OML: " + System.Environment.NewLine + message,
OkText = "Ok",
});
if (result)
{
// do something
var x = message;
}
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
if (intent.Action.Equals(Constants.ACTION_START_SERVICE))
{
if (InstanceFieldsInitialized)
{
//Log.Info(TAG, "OnStartCommand: The service is already running.");
}
else
{
//Log.Info(TAG, "OnStartCommand: The service is starting.");
startSignalR();
RegisterForegroundService();
InstanceFieldsInitialized = true;
}
}
else if (intent.Action.Equals(Constants.ACTION_STOP_SERVICE))
{
StopSelf();
InstanceFieldsInitialized = false;
}
else if (intent.Action.Equals(Constants.ACTION_RESTART))
{
}
// This tells Android not to restart the service if it is killed to reclaim resources.
return StartCommandResult.Sticky;
}
void RegisterForegroundService()
{
var notification = new NotificationCompat.Builder(this)
.SetContentTitle(Resources.GetString(Resource.String.app_name))
.SetContentText(Resources.GetString(Resource.String.notification_text))
.SetSmallIcon(Resource.Drawable.alert_box)
.SetOngoing(true)
.Build();
// Enlist this instance of the service as a foreground service
StartForeground(Constants.SERVICE_RUNNING_NOTIFICATION_ID, notification);
}
}
public class LocalBinder : Binder
{
private readonly SignalRSrv outerInstance;
public LocalBinder(SignalRSrv outerInstance)
{
this.outerInstance = outerInstance;
}
public virtual SignalRSrv Service
{
get
{
// Return this instance of SignalRService so clients can call public methods
return outerInstance;
}
}
}
我在调试器中设置了断点,它们永远不会被命中。 RegisterForegroundService() 中设置的图标从不显示(并不奇怪,因为服务未实例化)。
【问题讨论】:
-
嗨,在代码运行之前你是什么意思?
-
SignalR 代码经过独立测试并且工作正常。现在我想将它作为服务运行。
-
好吧,你在Service的OnCreate()中设置断点,看看是否可以工作。
-
我做到了,但他们从来没有打过。
标签: c# xamarin.android visual-studio-2017