안녕하세요 요새 한참 안드로이드펍을 오가며 열심히 공부하고있는 학생입니다.

 

다름이아니라

 

제가 개발중인 프로젝트에 토글버튼이 있는데

 

토글버튼이 onDestroy 상태로가면 토글버튼이 다시 활성화되지않은 해제상태로 초기화됩니다.

 

그리하여 프레퍼런스를 사용해보자고 했으나,

 

프레퍼런스를 어떻게 활용하여 토글버튼을 영구적으로 변환하게 할 수 있는지 궁금증이듭니다.

 

고수님들 ㅠㅠ 잘부탁드리겠습니다.

 

아래는 소스입니다..

 

 

 

---------------소스

 

package camera.example;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.ToggleButton;

public class CameraActivity extends Activity {
    /** Called when the activity is first created. */
 
 ToggleButton tglButton;
 Intent intent;
 SharedPreferences sPreferences;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        tglButton = (ToggleButton)findViewById(R.id.toggleButton1);
        intent = new Intent(getApplicationContext(), CameraService.class);
       
        sPreferences = this.getSharedPreferences("camera.example", Context.MODE_PRIVATE);

        tglButton.setOnClickListener(new View.OnClickListener() {
   
   public void onClick(View v) {
    // TODO Auto-generated method stub
    if(tglButton.isChecked() == true)
    {
     AlertDialog.Builder aDialog = new AlertDialog.Builder(CameraActivity.this);
     aDialog.setTitle("주의!");
     aDialog.setIcon(R.drawable.causion);
     aDialog.setMessage("보안 서비스를 시작하시면 인터넷 및 카메라 기능을 사용하실수 없게됩니다. 그래도 사용하시겠습니까?");
     aDialog.setPositiveButton("네", new DialogInterface.OnClickListener() {
      
      public void onClick(DialogInterface dialog, int which) {
       // TODO Auto-generated method stub
       startService(intent); // 서비스 시작
       Toast.makeText(getApplicationContext(), "보안 서비스를 시작합니다.", Toast.LENGTH_SHORT).show();
      }
     });
     aDialog.setNegativeButton("아니오", new DialogInterface.OnClickListener() {
      
      public void onClick(DialogInterface dialog, int which) {
       // TODO Auto-generated method stub
       tglButton.setChecked(false);
      }
     });
     aDialog.show();

    }
    else if(tglButton.isChecked() == false)
    {
     stopService(intent); // 서비스 종료
     
     Toast.makeText(getApplicationContext(), "보안 서비스를 종료합니다.", Toast.LENGTH_SHORT).show();

    }
   }
  });
    }