服务(android)无法启动

时间:2016-01-28 18:57:29

标签: android android-intent android-service

我是一名非常初级的Android开发人员,现在正在做一些服务实践。但是在做这种练习时我遇到了一些问题。

 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;

公共类CountService扩展了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;
    }
}

单击按钮时,服务无法运行。谁可以帮助我?谢谢。

感谢Perroloco的帮助。我忘了在mainfest文件中添加服务权限。

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

1 个答案:

答案 0 :(得分:1)

首先,您不需要两个ButtonListener实例。这应该足够了:

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 = 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;
...

现在,我认为您的代码是正确的,至少在启动服务时是这样。你应该查看你的服务代码。我会在onStartCommand方法中启动线程,而不是在onCreate中。 另外,您是否在清单中声明了您的服务?