启动和停止服务使用复选框

时间:2014-06-03 22:47:11

标签: android android-service

我有两个问题..

1。我开发了一个Android应用程序,我有: 活动A. 活动B. 在活动B中,我有4个togglebuttons。如果我单击ToggleButton1,我在后台运行服务。 (如果togglebutton1正在运行我的服务) 但是如果服务正在运行并且我在活动A中返回并且稍后打开活动B,则togglebutton1关闭但服务仍在运行....为什么?

2. 我想运行的不仅仅是服务,还有如何使用ID或其他东西杀死服务?

这是我的代码 - 班级服务:

 @Override
   public void onCreate() {

        Toast.makeText(this,"Service created",Toast.LENGTH_SHORT).show();
   }

   @Override
   public int onStartCommand(Intent intenc, int flags, int idArranque) {

       new FTask().execute();

       return START_STICKY;
   }

   @Override
   public void onDestroy() {

       Toast.makeText(this,"Service stopped",Toast.LENGTH_SHORT).show();
   }

   @Override
   public IBinder onBind(Intent intencion) {

       return null;
   }

这里有togglebuttons:

btn1.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if(btnAtterraDecolla.isChecked()){

                Intent i = new Intent(DettagliVolo.this, MyAct.class);

                startService(i);

            }else{

                stopService(new Intent(DettagliVolo.this,MyAct.class));
            }
         }
    });
....more togglebuttons similar

这是一个ToggleButton

提前谢谢!

编辑:

        btnAtterraDecolla.setChecked(
            getSharedPreferences("btnAtterraDecolla", MODE_PRIVATE)
                    .getBoolean("state", false)
    );

    btnAtterraDecolla.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if(isChecked){

                Intent i = new Intent(DettagliVolo.this, RunAsyncTask.class);


                i.putExtra("AoP", AoP);                 

                pintent = PendingIntent.getService(DettagliVolo.this, 0, i, 0);
                alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                alarm.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 10*1000, pintent);


            }else{

                alarm.cancel(pintent);
                stopService(new Intent(DettagliVolo.this,RunAsyncTask.class));

            }
            getSharedPreferences("btnAtterraDecolla", MODE_PRIVATE)
            .edit().putBoolean("state", isChecked)
            .commit();
        } 
    });

LOGCAT:http://paste.ubuntu.com/7589070/ 在这里崩溃:

                    alarm.cancel(pintent);

RE-编辑:

public class DettagliVolo extends Activity {

boolean AoP;

AlarmManager alarm;
PendingIntent pintent;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.dettaglivolo);

    Intent intent = getIntent();


    this.AoP = intent.getBooleanExtra("AoP", true );

    TextView AtterraDecolla = (TextView)findViewById(R.id.textView7);


    final ToggleButton btnAtterraDecolla = (ToggleButton)findViewById(R.id.toggleButton3);


    btnAtterraDecolla.setChecked(
            getSharedPreferences("btnAtterraDecolla", MODE_PRIVATE)
                    .getBoolean("state", false)
    );

    btnAtterraDecolla.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            Intent i = new Intent(DettagliVolo.this, RunAsyncTask.class);
            pintent = PendingIntent.getService(DettagliVolo.this, 0, i, 0);
            alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

            if(isChecked){


                i.putExtra("AoP", AoP);                 

                alarm.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 10*1000, pintent);

            }else{

                alarm.cancel(pintent);
                stopService(new Intent(DettagliVolo.this,RunAsyncTask.class));

            }
            getSharedPreferences("btnAtterraDecolla", MODE_PRIVATE)
            .edit().putBoolean("state", isChecked)
            .commit();
        } 
    });

1 个答案:

答案 0 :(得分:0)

您正在启动和停止两项不同的服务

new Intent(DettagliVolo.this, MyAct.class);
new Intent(DettagliVolo.this,FlightService.class);
问题更新后

编辑

当您打开一个新活动时,将绘制布局。说实话,任何触发onCreate的事件都会强制绘制布局。您可以保持切换按钮的状态。我建议你使用SharedPreferences,怎么做?

在活动的第一次启动时,我们假设该服务尚未启动,单击该复选框将触发服务的启动。

  • 使用OnCheckedChangeListener( ... )代替OnClickListener,它在语义上更合适。

  • 在给布局充气后,在活动B的onCreate方法中,检查此切换按钮的状态。

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

    ToggleButton toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);

    toggleButton1.setChecked(
            getSharedPreferences("TOGGLE_BUTTON_1", MODE_PRIVATE)
                    .getBoolean("state", false)
    );

    toggleButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked)
                startService(null);
            else
                stopService(null);

            getSharedPreferences("TOGGLE_BUTTON_1", MODE_PRIVATE)
                    .edit().putBoolean("state", isChecked)
                    .commit();
        }
    });
}