Android: Check if service is running via. bindService

前端 未结 7 1456
心在旅途
心在旅途 2021-02-18 15:28

What would be the best way to check if an Android Service is running? I am aware of the ActivityManager API, but it seems like the use of the API is not advised for

相关标签:
7条回答
  • 2021-02-18 16:11

    in your activity u can simply check it by using instance of your service in my case i done it using

      button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
                Intent intent=new Intent(MainActivity.this, MyService.class);
                //startService(intent);
                bindService(intent,serviceConnection,BIND_AUTO_CREATE);
                if (myService!=null)
                {
                    myService.RecData(editText.getText().toString());
                }
            }
        });
    }
    
    ServiceConnection serviceConnection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Toast.makeText(MainActivity.this,"onServiceConnected called",Toast.LENGTH_SHORT).show();
            MyService.MyBinder myBinder= (MyService.MyBinder) iBinder;
            myService= myBinder.getServiceInstant();
            myService.SetActivity(MainActivity.this);
            myService.RecData(editText.getText().toString());
        }
    
    0 讨论(0)
  • 2021-02-18 16:14

    I had the same need and found that running .bindService with something else already bound, that it would cause errors. I did this right before running .bindService

    try{
        context.unbindService(fetch_connection);
    } catch (IllegalArgumentException e){
        System.out.println("Unbinding didn't work. little surprise");
    }
    
    0 讨论(0)
  • 2021-02-18 16:15

    I have the same issue; seems the best known solution is to create a public static boolean in WhateverService, set to true during onCreate (or onStartCommand, your choice) and false during onDestroy. You can then access it from any other class in the same apk. This may be racey though. There is no API within Android to check if a service is running (without side effects): See http://groups.google.com/group/android-developers/browse_thread/thread/8c4bd731681b8331/bf3ae8ef79cad75d

    I suppose the race condition comes from the fact that the OS may kill a remote service (in another process) at any time. Checking a local service (same process), as suggested above, seems race-free to me -- if the OS kills the service, it kills whatever checked its status too.

    0 讨论(0)
  • 2021-02-18 16:17

    See answer here:

    How to check if a service is running on Android?

    0 讨论(0)
  • 2021-02-18 16:24

    Another method:

    public static boolean isServiceRunning(Context ctx, String serviceClassName) {
        ActivityManager manager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (TrackingService.class.getName().equals(serviceClassName)) {
                return true;
            }
        }
        return false;
    }
    
    0 讨论(0)
  • 2021-02-18 16:31

    What do you plan on doing if the service is running/not running? Why not just call startService(). That will create it if it's not running, and if it is it will call its onStart() method.

    0 讨论(0)
提交回复
热议问题