Alex Zheng
Alex Zheng

Reputation: 11

Service (android) can't start

I am a really junior android developer and do some practices on service right now. But I get some problems when doing this practice.

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWidget();
    registerListener();

}

public void getWidget()
{
    startService = (Button) findViewById(R.id.startService);
    shutdownService = (Button) findViewById(R.id.shutdownService);
}

public void registerListener()
{
    ButtonListener bl = new ButtonListener();
    startService.setOnClickListener(bl);
    shutdownService.setOnClickListener(bl);
}
class ButtonListener implements View.OnClickListener
{

    @Override
    public void onClick(View v) {

        Intent intent = null;
        switch(v.getId())
        {
            case R.id.startService:
                intent = new Intent(MainActivity.this,CountService.class);
                startService(intent);
                Log.v(TAG,"start service");
                break;
            case R.id.shutdownService:
                intent = new Intent(MainActivity.this,CountService.class);
                stopService(intent);
                Log.v(TAG,"stop service");
                break;

public class CountService extends Service{

boolean threadDisable ;
int count;

public IBinder onBind(Intent intent){
    return null;
}
public void onCreate(){
    super.onCreate();
    /**New thread, count increased by 1 every 1s*/
    new Thread(new Runnable(){
        public void run(){
            while(!threadDisable){
                try{
                    Thread.sleep(1000);
                }catch(InterruptedException e){

                }
                count++;
                Log.v("CountService","Count is"+count);
            }
        }
    }).start();
}
public void onDestroy(){
    super.onDestroy();
    /**service ends*/
    this.threadDisable = true;
}
public int getConunt(){
    return count;
}
class ServiceBinder extends Binder{
    public CountService getService(){
        return CountService.this;
    }
}

When I click the button, the service doesn't run. Who can help me with that? Thank you.

Thanks for Perroloco's help. I forgot to add service permission in mainfest files.

<service android:name=".CountService"></service>

Upvotes: 0

Views: 104

Answers (1)

Sebastian Breit
Sebastian Breit

Reputation: 6159

First of all, you don't need two instances of ButtonListener. This should be enough:

public void registerListener()
{
    ButtonListener bl = new ButtonListener();
    startService.setOnClickListener(bl);
    shutdownService.setOnClickListener(bl);
}

Also, the intent you use is equal in both cases, so:

class ButtonListener implements View.OnClickListener
{

    @Override
    public void onClick(View v) {

        intent = new Intent(MainActivity.this,CountService.class);
        switch(v.getId())
        {
            case R.id.startService:
                startService(intent);
                Log.v(TAG,"start service");
                break;
            case R.id.shutdownService:
                stopService(intent);
                Log.v(TAG,"stop service");
                break;
...

Now, I think your code is right, at least for starting the service. You should look at your service's code. I would start the thread in the onStartCommand method, and not in onCreate. Also, did you declare your service in the manifest?

Upvotes: 1

Related Questions