Android和长期运行的后台任务

时间:2019-01-09 13:17:52

标签: android xamarin background-process

我有一个Xamarin Android应用程序。我需要在此应用中开始长时间运行的任务。据我所知,我应该使用类Service。我创建了一个这样的类:

[Service]
public class BackgroundService : IntentService
{
    Handler mHandler;

    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        mHandler = new Handler();
        SomeTask();
        return 0;
    }

    private void SomeTask()
    {
        // Thread.CurrentThread.ManagedThreadId here is 1
        Thread.Sleep(5000);
        for (int i = 1; i <= 10; i++)
        {
            mHandler.Post(new Runnable(() =>
            {
                Toast.MakeText(this, $"Current thread ID in service: {Thread.CurrentThread.ManagedThreadId}", ToastLength.Short).Show();
            }));
        }
        StopSelf();                       
    }

    protected override void OnHandleIntent(Intent intent)
    {
    }
}

以及我如何在活动中使用该服务:

// And here Thread.CurrentThread.ManagedThreadId is 1
var serviceToStart = new Intent(this, typeof(BackgroundService));
StartService(serviceToStart);

我希望该服务在另一个后台线程中启动,但是活动中的Thread.CurrentThread.ManagedThreadId与类BackgroundService的方法Thread.CurrentThread.ManagedThreadId中的SomeTask相同。此外,当SomeTask运行时,我的应用程序没有响应。显然,这意味着该服务无法在后台线程中运行。 如何使服务在后台线程/进程中运行?

1 个答案:

答案 0 :(得分:0)

您应该使用OnHandleIntent方法弹出Toast并将new Handler()的代码更改为new Handler(Looper.MainLooper)

您可以参考以下演示。

enter image description here

IntentServiceDemo.cs

[Service]
public  class IntentServiceDemo : IntentService
{

    Handler mHandler;
    public IntentServiceDemo() : base("IntentServiceDemo")
    {

    }
    protected override void OnHandleIntent(Intent intent)
    {
        mHandler = new Handler(Looper.MainLooper);
        SomeTask();
    }

    private void SomeTask()
    {
        Thread.Sleep(1000);
        int id=System.Threading.Thread.CurrentThread.ManagedThreadId;
        for (int i = 1; i <= 10; i++)
        {
            mHandler.Post(new Runnable(() =>
            {

                Toast.MakeText(ApplicationContext, $"Current thread ID in service:"+ id, ToastLength.Short).Show();

            }));
        }
        StopSelf();
    }
}

MainActivity.cs

[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    //string TAG = "DemoService:";
  //  private Messenger messenger; // Instance variable for the Messenger
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.activity_main);
        int id = System.Threading.Thread.CurrentThread.ManagedThreadId;
        Toast.MakeText(this ,"Main thread"+ id, ToastLength.Short).Show();
        Button bt_service = FindViewById<Button>(Resource.Id.bt_service);

        bt_service.Click += (e, o) =>
        {
            Intent downloadIntent = new Intent(this, typeof(IntentServiceDemo));
            StartService(downloadIntent);

        };

    }

activity_main.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 <Button
  android:id="@+id/bt_service"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="startService"/>
</RelativeLayout>