마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

스위치 버튼으로 서비스가 실행이 안되네요

0 추천
package com.example.app1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

public class menu extends Activity {
 Intent intent;
 private Switch swc;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.menu);

  intent = new Intent(this, action.class);

  swc = (Switch)findViewById(R.id.switch1);
  swc.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener(){

   @Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecking) {
    String str = String.valueOf(isChecking);
    
    if(isChecking){
     intent = new Intent (menu.this, action.class );
     startService(intent);
     moveTaskToBack(true);
    }
    
    else{
     intent = new Intent (menu.this, action.class );
     stopService(intent);
    }    
   }
  });
 }

 public void go_info (View v) {
  Intent myintent = new Intent(this, option.class);
  startActivity(myintent);
 }
 


}

코드는 이런 형식인데 버튼으로 구현할 때는 서비스가 실행이 잘 되었는데 스위치는 on해도 어플이 꺼지기만 해도 서비스 실행이 안되네요... 방법있나요 ㅜ

익명사용자 님이 2014년 11월 12일 질문
2014년 11월 12일 수정

1개의 답변

0 추천
context=this;

 

 

context.startActivity(,)...

 

...

context 에 어플래케이션 컨텍스트를 받아와야합니당.^^!
익명사용자 님이 2016년 7월 15일 답변
...