안녕하세요 android. MainActivity에서 Service
을 멈추고 싶습니다. 그러나 나는 그것을 얻지 못하고있다. stopService()
을 부를 때는 Toast
메시지 만 표시한다. 나는 서비스가 여전히 뒷골목에서 돌아가고 있음을 관찰했다. 서비스를 중지하는 방법. 여기 내 샘플 코드입니다.내 서비스가 파괴되지 않습니다 - 서비스를 중지하는 방법
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Method to start the service
public void startService(View view) {
startService(new Intent(getBaseContext(), MyService.class));
}
// Method to stop the service
public void stopService(View view) {
stopService(new Intent(getBaseContext(), MyService.class));
}
}
public class MyService extends Service {
@Override
public IBinder onBind(Intent arg0) {
return null;
}
static int i=0;
private static final String Tag="MyService";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread() {
public void run() {
while (true) {
Log.v(Tag,"Thread"+i);
}
}
}.start()
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
}
이 주제가 도움이되는지 확인하십시오 : http://stackoverflow.com/questions/2176375/service-wont-stop-when-stopservice-method-is-called – PedroHawk