Android绑定服务onStart不起作用

时间:2018-09-28 20:41:18

标签: java android android-service android-service-binding

我有一个服务(绑定),试图从MainActivity.java开始。以下是我的主要活动代码:

public class MainActivity extends AppCompatActivity {
    Handler m_handler;
    Runnable m_handlerTask ;
    private static ServiceConnection sConnection;
    static boolean  serviceBound = false;

    @Override
    protected void onStart() {
        super.onStart();
        // Bind to LocalService
        Intent intent = new Intent(this, DataDownloaderService.class);
        bindService(intent, sConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.NoActionBar);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            if (!Global.isMyServiceRunning(DataDownloaderService.class, this)) {
                sConnection = new ServiceConnection() {
                    @Override
                    public void onServiceConnected(ComponentName className, IBinder service) {
                        // We've bound to LocalService, cast the IBinder and get LocalService instance
                        DataDownloaderService.LocalBinder binder = (DataDownloaderService.LocalBinder) service;
                        Global.dService = binder.getService();
                        serviceBound = true;
                    }

                    @Override
                    public void onServiceDisconnected(ComponentName arg0) {
                        serviceBound = false;
                    }
                };
            }
        }
        catch(Exception ex ){
            String p="";
        }
    }

    @Override
    protected void onResume(){
        super.onResume();
        m_handler = new Handler();
        m_handlerTask = new Runnable()
        {
            @Override
            public void run() {

                RefreshNowPlaying();
                m_handler.postDelayed(m_handlerTask, 1000);

            }
        };
        m_handlerTask.run();           
        }
    }


    void RefreshNowPlaying(){
//Do something            
   }        
}

服务代码如下所示:

public class DataDownloaderService extends Service {

    private final IBinder dBinder = new LocalBinder();

    public class LocalBinder extends Binder {
        DataDownloaderService getService() {
            // Return this instance of LocalService so clients can call public methods
            return DataDownloaderService.this;
        }
    }

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

    @Override
    public void onCreate(){

    }

    @Override
    public void onStart(Intent intent, int startid){
        super.onStart(intent,startid);
        Global.DataDownloaderServiceStatus = "STARTED";
    }

    @Override
    public void onDestroy(){
        Global.DataDownloaderServiceStatus = "STOPPED";
        super.onDestroy();
    }
}

我还将服务添加到了Application元素下的AndroidManifest.xml中。但是,我看到该服务的onStart从未触发过,也从未调用过MainActivity中的重写onServiceConnected。我是Anrdoid开发和Java的新手。有人可以给我一些指示吗?我注意到通过放置断点而不是onStart击中了服务的onCreate。

1 个答案:

答案 0 :(得分:0)

onStart()仅在通过context.startService()启动服务时才会被调用,请参见here