即使应用程序已关闭,也会每小时在后台运行服务

时间:2018-01-23 15:07:49

标签: c# android xamarin.android

我正在尝试运行服务来检查网络状态。我需要每小时检查一次,即使应用程序已从最近的应用程序托盘强制关闭。我已经创建了服务,它可以加载并运行一次。不确定如何每小时和关闭后运行它。

服务

using System;
using System.IO;
using Android.App;
using Android.Content;
using Android.Net;
using Android.OS;
using Android.Widget;

namespace example.Services
{
    [Service(Label = "NetworkService")]
    [IntentFilter(new String[] { "com.example.NetworkService" })]
    public class NetworkService : Service
    {
    IBinder binder;

    public override StartCommandResult OnStartCommand(Android.Content.Intent intent, StartCommandFlags flags, int startId)
    {

        // start your service logic here
        string filename = "network.txt";
        var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        var filePath = Path.Combine(documentsPath, filename);

        if(!File.Exists(filePath))
            File.Create(filePath);

        ConnectivityManager connectivityManager = (ConnectivityManager)GetSystemService(ConnectivityService);
        NetworkInfo networkInfo = connectivityManager.ActiveNetworkInfo;
        bool isOnline = networkInfo.IsConnected;

        if(isOnline)
        {
            File.WriteAllText(filePath, "Connected");
        }
        else
        {
            File.WriteAllText(filePath, "Not Connected");
        }
            return StartCommandResult.Sticky;
        }

        public override IBinder OnBind(Intent intent)
        {
            binder = new NetworkServiceBinder(this);
            return binder;
        }
    }

    public class NetworkServiceBinder : Binder
    {
        readonly NetworkService service;

        public NetworkServiceBinder(NetworkService service)
        {
            this.service = service;
        }

        public NetworkService GetNetworkService()
        {
        return service;
        }
    }
}

清单

<service android:enabled="true" android:name="com.example.NetworkService" />

MainActivity

using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;

namespace whitesrecycling.Activities
{
    [Activity(Label = "HomeActivity")]
    public class HomeActivity : Activity
    {
        protected override void OnStart()
        {
            base.OnStart();

            StartService(new Intent(this, typeof(Services.NotifcationService)));
            StartService(new Intent(this, typeof(Services.NetworkService)));
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.Home);
            var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
            SetActionBar(toolbar);
            ActionBar.Title = "Example";

        }
    }
}

1 个答案:

答案 0 :(得分:0)

就像@pskink所说,您可以使用AlarmManager,也可以使用JobScheduler

以下是AlarmManager的用法,以下是JobScheduler的使用。

一些建议:

  • 如果Api <19,请使用0x100005db0 <+548>: ldr q0, [x9, x12] 0x100005db4 <+552>: ldr q1, [x10, x12] 0x100005db8 <+556>: cmeq.4s v2, v0, v4 0x100005dbc <+560>: bsl.16b v2, v1, v0

  • 如果Api> 21,请使用SELECT * FROM players WHERE username IN ('a', 'b') group by username

在Android中,您无法始终保持应用程序的活力,因为它会消耗很多电量。