作业服务无法执行方法

时间:2019-03-13 18:32:18

标签: c# android xamarin.forms xamarin.android .net-standard

我创建了服务来检查来自我自己服务器的通知。

当我的应用程序运行时,它可以正常工作,但是当我的应用程序最近从我的应用程序中删除时,它不能很好地工作。我做了吐司检查服务是否仍在运行,并且吐司在杀死应用程序后仍然有效,但是检查服务器的方法在后台不起作用。

我将xamarin表单和android服务与安装在模拟器中的android oreo一起使用,这是我的代码:

public class MyService : JobService
{
   private void DoWork()
   {
      try
      {
        Intent i =new Intent(this.ApplicationContext,typeof(MyReciver));
        i.SetAction("ezz");
        SendBroadcast(i);
      }
      catch
      {
      }
      finally
      {
        StopSelf();
      }

   }

public override bool OnStartJob(JobParameters jobparams)
{
    new Task(() =>
    {
    while (true)
        {
            DoWork();
            Thread.Sleep(5000);
            JobFinished(jobparams, true);
        }
    }).Start();
    return true;

}

public override bool OnStopJob(JobParameters jobparams)
{
    return true;

 }
}

这是我的接收者:

if (intent.Action == "ezz")
    {
        try
        {
            txtmod v = new txtmod();//to get the last id doesn't work in background from shared code
            string cc = v.readtxt();
            string bb = "Last id " + cc;
            Toast.MakeText(context, bb, ToastLength.Short).Show();//run in foreground and stil running in back ground
            PostReq post = new PostReq(1);//doesn't work in background from shared code
        }
        catch { }
    }

更新:txtmod和postreq

public class txtmod
{
    string fileName= Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "txtronana.txt");
    public string readtxt()
    {
        string text="" ;
        try
        {
            using (StreamReader sr = File.OpenText(fileName))
            {
                string s;
                while ((s = sr.ReadLine()) != null)
                {
                    text = s;
                }
            }
        }
        catch { }
        return text;
    }

    public void savetxt(string postid)
    {
        try
        {
            using (StreamWriter sw = File.CreateText(fileName))
            {
                sw.WriteLineAsync(postid);
            }
        }
        catch { }
    }
}

postreq:

public async void chkpostAsync(int idp)
    {
        Tst:
        try
        {
            CATITEMS x = await getmainitemsasync(1);
            if (x.properties.data != null)
            {
                if (x.properties.data[0].post_id > idp)//d.post_id)
                {
                    for(int f = 0; f <= 9; f++)
                    {
                        if (x.properties.data[f].post_id > idp)
                        {
                            push(x.properties.data[f].post_title, x.properties.data[f].post_category, x.properties.data[f].post_id,f+1+DateTime.Now.Second);

                        }
                        else { break; }
                    }
                }
                if (lastid != x.properties.data[0].post_id)
                {
                    Txtmod.savetxt(x.properties.data[0].post_id.ToString());
                }
            }
            else { goto Tst; }
        }
        catch { }
    }

0 个答案:

没有答案