Start/Stop service by checking/unchecking CheckBoxPreference

自闭症网瘾萝莉.ら 提交于 2019-12-10 10:26:23

问题


i'm writting an app that start or stop service by checking or unchecking CheckBoxPreference i tried to find some information such as sample code or tutorials. but i didn'f find yet. how to start or stop service by checking or unchecking CheckBoxPreference which is in PreferenceActivity? i want to start service when "service_toggle" key is true and stop service when "service_toggle" key is false Are there any sample code or tutorial about Start/Stop service by checking/unchecking CheckBoxPreference?

Here is my code :


Settings.java

package kr.hybdms.sidepanel;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;

import com.actionbarsherlock.app.SherlockPreferenceActivity;
import kr.hybdms.sidepanel.R;

public class Settings extends SherlockPreferenceActivity implements OnPreferenceClickListener{

      @SuppressWarnings("deprecation")
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);



        Preference pAppName = (Preference)findPreference("blog_intent");
        Preference pAppVersion = (Preference)findPreference("appinfo_intent");

        pAppName.setOnPreferenceClickListener(this);
        pAppVersion.setOnPreferenceClickListener(this);
    }

    @Override
    public boolean onPreferenceClick(Preference preference)
    {

        if(preference.getKey().equals("blog_intent"))
        {
            Intent blog = new Intent(Intent.ACTION_VIEW);
            blog.setData(Uri.parse("http://hybdms.blogspot.kr"));
            startActivity(blog);
        }

        else if(preference.getKey().equals("appinfo_intent"))
        {
            Intent appinfo = new Intent(Settings.this, Appinfo.class); 
             startActivity(appinfo);
        }
        return false;
    }

}

settings.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference android:key="service_toggle" android:title="@string/pref_service_toggle" android:summary="@string/pref_service_toggle_sub" android:defaultValue="false"/>
    <CheckBoxPreference android:key="autostart_toggle" android:title="@string/pref_auto" android:summary="@string/pref_auto_sub" android:defaultValue="false" android:selectable="false" android:enabled="false"/>
    <Preference android:key="guide_intent" android:selectable="false" android:summary="@string/pref_guide_sub" android:title="@string/pref_guide" android:enabled="false"/>
    <Preference android:key="blog_intent" android:summary="@string/pref_blog_sub" android:title="@string/pref_blog"/>
    <Preference android:key="appinfo_intent" android:summary="@string/pref_appinfo_sub" android:title="@string/pref_appinfo"/>

</PreferenceScreen>

TouchDetectService.java

package kr.hybdms.sidepanel;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.graphics.PixelFormat;

public class TouchDetectService extends Service {
    private ImageView mTouchDetector;                           
    private WindowManager.LayoutParams mParams;     
    private WindowManager mWindowManager;       

    private OnTouchListener mViewTouchListener = new OnTouchListener() {
        @Override public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()) {

                case MotionEvent.ACTION_MOVE:

                    Intent lsp = new Intent(getBaseContext(), LeftSidePanel.class);
                    lsp.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getApplication().startActivity(lsp);
                    break;
            }

            return true;
        }
    };

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

    @Override
    public void onCreate() {
        super.onCreate();

            mTouchDetector = new ImageView(this);                                         
            mTouchDetector.setImageResource(R.drawable.detector);
            mTouchDetector.setOnTouchListener(mViewTouchListener);            

            mParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,  
                PixelFormat.TRANSLUCENT);                                        
            mParams.gravity = Gravity.LEFT | Gravity.CENTER;                  

            mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);  
            mWindowManager.addView(mTouchDetector, mParams);      permission 
        }

    @Override
    public void onDestroy() {
        if(mWindowManager != null) {        
            if(mTouchDetector != null) mWindowManager.removeView(mTouchDetector);
        }
        super.onDestroy();
    }

回答1:


CheckBoxPreference checkboxPref = (CheckBoxPreference)getPreferenceManager().findPreference("service_toggle");

checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {            

    public boolean onPreferenceChange(Preference preference, Object newValue) {

        boolean myValue = (Boolean) newValue;

        if(myValue)
            startService(new Intent(Settings.this, TouchDetectService.class));
        else
            stopService(new Intent(Settings.this, TouchDetectService.class));

        return true;
    }
}); 



回答2:


public class Settings extends SherlockPreferenceActivity implements OnPreferenceClickListener, OnPreferenceChangeListener {

      @SuppressWarnings("deprecation")
      @Override
      public void onCreate(Bundle savedInstanceState) {
        .........

        CheckBoxPreference chkServiceToogle = (CheckBoxPreference)findPreference("service_toggle");
        chkServiceToogle.setOnPreferenceChangeListener(this);
    }

    @Override
    public boolean onPreferenceChange(Preference preference, String key)
    {
        if (key.equals("service_toggle")) {
            if(sharedPreferences.getBoolean(key, false)) {
                Intent intent = new Intent(getApplicationContext(), TouchDetectService.class);
                startService(intent);
            } else {
                Intent intent = new Intent(getApplicationContext(), TouchDetectService.class);
                stopService(intent);
            }
        }
    }
........
}


来源:https://stackoverflow.com/questions/15049621/start-stop-service-by-checking-unchecking-checkboxpreference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!