android 为什么我的服务 运行() 不工作

android why my service run() not working

我有一个 Android 服务,我开始使用来自 class:

的 Intent
Intent i = new Intent(getActivity(), MyService.class);
i.putExtra("Arrived_To_Destination","yes");
getActivity().startService(i);

我的服务代码:

public class MyService extends Service {

    String arrival_event;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("hi","Inside3");
        arrival_event = intent.getStringExtra("Arrived_To_Destination");
        Log.d("hi","Inside0" + arrival_event);
        WorkerThread dialogthread = new WorkerThread(arrival_event);
        Log.d("hi","Inside1");
        dialogthread.start();
        Log.d("hi","Inside2");
        return Service.START_STICKY;
    }

    private class WorkerThread extends Thread {
        String arrival_event;

        public WorkerThread(String arrival_event) {
        this.arrival_event = arrival_event;
        }

        @Override
        public void run() {
            Log.d("hi","Inside4" + arrival_event);
            try {
                Log.d("hi","Inside5" + arrival_event);
                if(arrival_event.equalsIgnoreCase("yes")) {
                    Log.d("hi","Inside6");
                    MyFragment mf = new MyFragment();
                    Log.d("hi","Inside7");
                    mf.showDialog();
                }

            } catch (Exception e) {
                logger.error("Error: ", e);
            }
        }
    }
}

服务启动后,我得到日志 -

Inside 0 
Inside 1
Inside 2
Inside 3
Inside 4

我的班级

private void updateLocation(Child currentchild, Location location) {
if (distancetodestination <= 100) {
                    // the destination reached; 100 is the 100metres, so if the child is within 100m, the arrival event is sent
                    logger.info("Child arrived");

                    isAtDesination = true;
                    child_arrived = true;
                    MyApi.sendArrival(child);

                   }
                }
}

问题是我没有进入 if 语句。在 MyClass 中,我已将 child_arrived 声明为 public static boolean child_arrived;

child 到达后,我将值设置为 true。为什么 MyService 没有执行 if 语句。

问题是两个代码分开 threads 这就是为什么不能以这种方式进行通信的原因,您可以在启动服务时将标志与意图一起传递,或者将标志值保存在 shared preferences 中,然后在此处检查。

Intent intent = new Intent(this, MyService.class);
intent.putExtra("Flag","0")
startService(intent);

MyService.java

public class MyService extends Service {

    int flag;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("hi","Inside0");

        flag = intent.getStringextra("Flag");

        WorkerThread dialogthread = new WorkerThread(flag);
        Log.d("hi","Inside1");
        dialogthread.start();
        Log.d("hi","Inside2");
        return Service.START_STICKY;
    }
 private class WorkerThread extends Thread {

        int flag;

        public WorkerThread(int flag) {
              this.flag = flag;
        }

        @Override
        public void run() {
            Log.d("hi","Inside3");
            try {
                Log.d("hi","Inside4");
                if(flag  == 0) {
                    Log.d("hi","Inside5");
                    MyFragment mf = new MyFragment();
                    Log.d("hi","Inside6");
                    mf.showDialog();
                }

            } catch (Exception e) {
                logger.error("Error : ", e);
            }
        }
    }
}