如何在xamarin forms ios平台中实现后台服务(使用共享项目)
我使用Xamarin表单中的共享项目来实现我的应用程序android和ios平台,因为我已经在android平台中实现了后台服务,但在ios中不适合我,下面的代码在android平台中实现的方式与在ios中实现的方式相同如何在xamarin forms ios平台中实现后台服务(使用共享项目),xamarin,xamarin.ios,xamarin.android,xamarin.forms,Xamarin,Xamarin.ios,Xamarin.android,Xamarin.forms,我使用Xamarin表单中的共享项目来实现我的应用程序android和ios平台,因为我已经在android平台中实现了后台服务,但在ios中不适合我,下面的代码在android平台中实现的方式与在ios中实现的方式相同 [Application] class FFA : Application { public static Context AppContext; public FFA(IntPtr javaReference, JniHandleOwnership tran
[Application]
class FFA : Application
{
public static Context AppContext;
public FFA(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public override void OnCreate()
{
base.OnCreate();
AppContext = this.ApplicationContext;
StartPushService();
}
public static void StartPushService()
{
AppContext.StartService(new Intent(AppContext, typeof(FFAService)));
if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
{
PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(FFAService)), 0);
AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
alarm.Cancel(pintent);
}
}
public static void StopPushService()
{
AppContext.StopService(new Intent(AppContext, typeof(FFAService)));
PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(FFAService)), 0);
AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
alarm.Cancel(pintent);
}
对于服务端,我必须显示本地推送通知
[Service]
public class FFAService : Service
{
public static Context AppContext;
private static readonly int ButtonClickNotificationId = 1000;
private int count = 1;
public override void OnCreate()
{
base.OnCreate();
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
DoStuff();
return StartCommandResult.RedeliverIntent;
}
public override IBinder OnBind(Intent intent)
{
return null;
}
public override void OnTaskRemoved(Intent rootIntent)
{
Intent restartService = new Intent(ApplicationContext, typeof(FFAService));
restartService.SetPackage(PackageName);
PendingIntent restartServicePI = PendingIntent.GetService(ApplicationContext, 1, restartService, PendingIntentFlags.OneShot);
AlarmManager alarmService = (AlarmManager)ApplicationContext.GetSystemService(AlarmService);
alarmService.Set(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + 100, restartServicePI);
}
public void DoStuff()
{
var timer = new System.Threading.Timer((e) =>
{
Bundle valuesForActivity = new Bundle();
valuesForActivity.PutInt("count", count);
Intent resultIntent = new Intent(this, typeof(SecondActivity));
resultIntent.PutExtras(valuesForActivity);
TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this);
stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(SecondActivity)));
stackBuilder.AddNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);
var connectivityManager = (ConnectivityManager)ApplicationContext.GetSystemService(Context.ConnectivityService);
var activeNetworkInfo = connectivityManager.ActiveNetworkInfo;
var sqliteFilename = $"{Common.DatabaseName}.db3";
var path = Path.Combine(Common.folder, sqliteFilename);
SQLite.SQLiteConnection connection = new SQLite.SQLiteConnection(path);
var val = connection.Query<Table>("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'TicketMessage'", new object[] { });
if (val.Count >= 1)
{
var reslt = connection.Query<Ticket>("SELECT * FROM TicketMessage where Sync = ?", new object[] { 0 });
//connection.Execute("update TicketMessage set Sync = 1 where Sync = 0");
count = reslt.Count;
}
if (activeNetworkInfo != null && activeNetworkInfo.IsConnectedOrConnecting)
{
//if (count > 1)
//{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.SetAutoCancel(true)
.SetContentIntent(resultPendingIntent)
.SetContentTitle("Outbox Details")
.SetNumber(count)
.SetSmallIcon(Resource.Drawable.notification)
.SetContentText(string.Format("Network connection available. Ourbox have {0} items.", count));
NotificationManager notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
notificationManager.Notify(ButtonClickNotificationId, builder.Build());
//}
}
else
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.SetAutoCancel(true)
.SetContentIntent(resultPendingIntent)
.SetContentTitle("Outbox Details")
.SetSmallIcon(Resource.Drawable.notification)
.SetContentText(string.Format("No network connection available. Ourbox have {0} items.", count));
NotificationManager notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
notificationManager.Notify(ButtonClickNotificationId, builder.Build());
}
}, null, 0, 60000);
}
[服务]
公共类服务:服务
{
公共静态语境;
私有静态只读int ButtonClickNotificationId=1000;
私有整数计数=1;
public override void OnCreate()
{
base.OnCreate();
}
公共覆盖StartCommandResult OnStartCommand(意图、StartCommandFlags标志、int-startId)
{
DoStuff();
返回StartCommandResult.RedeliverIntent;
}
公共覆盖iBind OnBind(意图)
{
返回null;
}
公共覆盖void OnTaskRemoved(Intent rootIntent)
{
Intent restartService=newintent(ApplicationContext,typeof(FFAService));
SetPackage(PackageName);
PendingEvent restartServicePI=PendingEvent.GetService(ApplicationContext,1,restartService,PendingEventFlags.OneShot);
AlarmManager alarmService=(AlarmManager)ApplicationContext.GetSystemService(alarmService);
alarmService.Set(AlarmType.ElapsedRealtime,SystemClock.ElapsedRealtime()+100,restartServicePI);
}
公共空间
{
var timer=新系统.线程.计时器((e)=>
{
Bundle values实用性=新Bundle();
valuesForActivity.PutInt(“count”,count);
意向结果=新意向(此,类型为(第二个活动));
结果:PutExtras(值实用性);
TaskStackBuilder stackBuilder=TaskStackBuilder.Create(此);
AddParentStack(Java.Lang.Class.FromType(typeof(SecondActivity)));
stackBuilder.AddNextIntent(resultIntent);
PendingEvent ResultPendingEvent=stackBuilder.GetPendingEvent(0,(int)PendingEventFlags.UpdateCurrent);
var connectivityManager=(connectivityManager)ApplicationContext.GetSystemService(Context.ConnectivityService);
var activeNetworkInfo=connectivityManager.activeNetworkInfo;
var sqliteFilename=$“{Common.DatabaseName}.db3”;
var path=path.Combine(Common.folder,sqliteFilename);
SQLite.SQLiteConnection连接=新的SQLite.SQLiteConnection(路径);
var val=connection.Query(“从sqlite_master中选择名称,其中类型='table',名称='TicketMessage',新对象[]{});
如果(val.Count>=1)
{
var reslt=connection.Query(“从TicketMessage中选择*其中Sync=?”,新对象[]{0});
//执行(“更新ticketmessageset Sync=1,其中Sync=0”);
count=reslt.count;
}
if(activeNetworkInfo!=null&&activeNetworkInfo.IsConnectedOrConnecting)
{
//如果(计数>1)
//{
NotificationCompat.Builder=新建NotificationCompat.Builder(此)
.SetAutoCancel(真)
.SetContentIntent(结果结束内容)
.SetContentTitle(“发件箱详细信息”)
.SetNumber(计数)
.SetSmallIcon(Resource.Drawable.notification)
.SetContentText(string.Format(“网络连接可用。Ourbox有{0}项。”,count));
NotificationManager NotificationManager=(NotificationManager)GetSystemService(Context.NotificationService);
notificationManager.Notify(ButtonClickNotificationId,builder.Build());
//}
}
其他的
{
NotificationCompat.Builder=新建NotificationCompat.Builder(此)
.SetAutoCancel(真)
.SetContentIntent(结果结束内容)
.SetContentTitle(“发件箱详细信息”)
.SetSmallIcon(Resource.Drawable.notification)
.SetContentText(string.Format(“没有可用的网络连接。Ourbox有{0}个项目。”,count));
NotificationManager NotificationManager=(NotificationManager)GetSystemService(Context.NotificationService);
notificationManager.Notify(ButtonClickNotificationId,builder.Build());
}
},空,0,60000);
}
请帮助我,这对我更有帮助,提前谢谢。iOS服务不像Android那样工作。我担心你不会得到比阅读iOS服务如何工作更好的答案,对不起,是的,我发现它不工作,但我们可以通过位置管理器或音频服务来实现,有没有像我在Android中所做的那样的实现。请帮助请告诉我。谢谢你的回复!!!你能用几句话描述一下你想在后台服务中做什么,而不是我试图弄清楚你做了什么。请记住,如果你指定你的服务是做位置或音频,但你不这样做,苹果会拒绝你的应用。是的,我明白了,使用服务我想检查网络连接将本地数据推送到服务器,即使用户关闭了应用程序,我们也必须在它返回后台时进行检查。您将无法完全按照指定在iOS上执行此操作。您有2个选项。1.非常不可靠。使用后台提取。完全不可预测。iOS将在您认为应用程序需要时唤醒您的应用程序uld。您可以阅读有关此功能的更多信息。2.使用来自服务器的远程推送通知来唤醒您的应用程序。您可以定期这样做。