我正在通過選中或取消選中來編寫啓動或停止服務的應用程序CheckBoxPreference
我試圖找到一些信息,如示例代碼或教程。但我還沒找到。 如何通過選中或取消選中啓動或停止服務CheckBoxPreference
這是PreferenceActivity
?我想在「service_toggle」鍵爲true時啓動服務,在「service_toggle」鍵爲false時停止服務。是否有任何關於啓動/停止服務的示例代碼或教程:選中/取消選中CheckBoxPreference
?通過選中/取消選中CheckBoxPreference來啓動/停止服務
這裏是我的代碼:
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();
}
好像你已經有代碼來檢測人攻上的偏好。只要看看它的價值,並調用startService或stopService。 – 2013-02-24 07:28:25
@GabeSechan但是我想要啓動或停止服務取決於「service_toggle」CheckBoxPreference值 – sukso 2013-02-24 07:46:40
不要這樣做。用戶[這] [1]方法。 [1]:http://stackoverflow.com/questions/12214673/how-to-apply-condition-onpreferenceclick-in-android – JanithaR 2013-02-24 08:23:12