android为什么我的服务run()不起作用

时间:2016-07-28 12:27:38

标签: android android-service

我有一个Android服务,我开始使用类中的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

MyClass的

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;

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

1 个答案:

答案 0 :(得分:1)

问题在于单独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);
            }
        }
    }
}