如何从调用活动类调用Service类的stopservice()方法

Har*_*hna 38 android

我试图从我的活动中调用我的服务类的stopService()方法.但我不知道如何从我的活动类访问stopservice方法.我有下面的代码,但它不工作...........

这是HomeScreen类:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        enablecheck = (CheckBox)findViewById(R.id.enablecheck);            

        enablecheck.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(enablecheck.isChecked()){    
                    startService(new Intent(HomeScreen.this, AutoService.class));
                }else
                {
                    stopService(new Intent(HomeScreen.this, AutoService.class));

                }                       
            }

        });

    }
Run Code Online (Sandbox Code Playgroud)
This is Service Class:
Run Code Online (Sandbox Code Playgroud)
public class AutoService extends Service {

    private static final String TAG = "AutoService";
    private Timer timer;    
    private TimerTask task;

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

    @Override
    public void onCreate() {
        Toast.makeText(this, "Auto Service Created", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate");     

        int delay = 5000; // delay for 5 sec.
        int period = 5000; // repeat every sec.

        timer = new Timer();

        timer.scheduleAtFixedRate(task = new TimerTask(){
            public void run() 
            {
                System.out.println("done");
            }   
        }, delay, period);


    }


    @Override
    public boolean stopService(Intent name) {
        // TODO Auto-generated method stub
        timer.cancel();
        task.cancel();
        return super.stopService(name);

    }


}
Run Code Online (Sandbox Code Playgroud)

任何建议都非常值得赞赏.谢谢并问候Mintu

Jor*_*sys 43

事实上,要停止服务,我们必须使用该方法,stopService()并且您正在以正确的方式进行:

开始服务:

  Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class);
  startService(myService);
Run Code Online (Sandbox Code Playgroud)

停止服务:

  Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class);
  stopService(myService);
Run Code Online (Sandbox Code Playgroud)

如果你调用stopService(),那么调用onDestroy()服务中stopService()方法(不是方法):

  @Override
    public void onDestroy() {
      timer.cancel();
      task.cancel();    
        Log.i(TAG, "onCreate() , service stopped...");
    }
Run Code Online (Sandbox Code Playgroud)

你必须实现这个onDestroy()方法!

这是一个完整的示例,包括如何启动/停止服务.


Jur*_*uri 34

我实际上使用了与你相同的代码.我在清单中的服务注册如下

<service android:name=".service.MyService" android:enabled="true">
            <intent-filter android:label="@string/menuItemStartService" >
                <action android:name="it.unibz.bluedroid.bluetooth.service.MY_SERVICE"/>
            </intent-filter>
        </service>
Run Code Online (Sandbox Code Playgroud)

在服务类中,我创建了一个相应的常量字符串,用于标识服务名称,如:

public class MyService extends ForeGroundService {
    public static final String MY_SERVICE = "it.unibz.bluedroid.bluetooth.service.MY_SERVICE";
   ...
}
Run Code Online (Sandbox Code Playgroud)

从相应的活动我称之为

startService(new Intent(MyService.MY_SERVICE));
Run Code Online (Sandbox Code Playgroud)

并停止它

stopService(new Intent(MyService.MY_SERVICE));
Run Code Online (Sandbox Code Playgroud)

它完美地运作.尝试检查您的配置,如果您没有发现任何异常,请尝试调试您的stopService是否正确调用.


Rob*_*ond 5

取消选中该复选框后,它似乎应该停止服务。日志中是否有例外?stopService返回一个布尔值,指示它是否能够停止该服务。

如果要通过Intents启动服务,则可能要扩展IntentService而不是Service。当该类没有更多工作要做时,它将自行停止该服务。

汽车服务

class AutoService extends IntentService {
     private static final String TAG = "AutoService";
     private Timer timer;    
     private TimerTask task;

     public onCreate() {
          timer = new Timer();
          timer = new TimerTask() {
            public void run() 
            {
                System.out.println("done");
            }
          }
     }

     protected void onHandleIntent(Intent i) {
        Log.d(TAG, "onHandleIntent");     

        int delay = 5000; // delay for 5 sec.
        int period = 5000; // repeat every sec.


        timer.scheduleAtFixedRate(timerTask, delay, period);
     }

     public boolean stopService(Intent name) {
        // TODO Auto-generated method stub
        timer.cancel();
        task.cancel();
        return super.stopService(name);
    }

}
Run Code Online (Sandbox Code Playgroud)


nee*_*vek 5

@朱里

如果为您的服务添加IntentFilters,则表示您要将服务公开给其他应用程序,那么其他应用程序可能会意外停止该服务。