感谢任何帮助。
我在清单中有一个广播接收器,当时是android:enabled =" true"接收广播并启动服务。当它是android:enabled =" false"没有接收广播,服务也没有开始。
这是你在使用PackageManger时所期望的,当我尝试在我的偏好活动中使用它时,在android:enabled =" true"的情况下,服务一直被调用。或者android:enabled =" false"该服务永远不会被调用。在这两种情况下,如果选中复选框都无关紧要。
我认为问题在于如何构造首选项活动,因为我尝试过的真/假逻辑被搞砸了。我没有得到任何错误,这使得人们相信它是如何构建的。真的想要一些帮助 解决这个问题。提前感谢您提供的任何帮助。
这是首选项XML和Preference Java。
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="CheckBox"
android:id="@+id/CheckBox1"
android:title=" StartUp"
android:summary="Change The CheckBox Status here, it will be saved in
Preference" />
</PreferenceScreen>
以下是Java
package com.collabera.labs.sai;
import java.util.Calendar;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
mport android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.CheckBox;
public class ShowPreferences extends PreferenceActivity implements
OnSharedPreferenceChangeListener{
boolean CheckBox;
CheckBox cb;
PreferenceManager pm;
SharedPreferences sp;
Editor editor;
private String CHECKBOX;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.bootpreference);
cb = (CheckBox) findViewById(R.id.CheckBox1);
loadPref();}
private void loadPref() {
// TODO Auto-generated method stub
SharedPreferences sp =
PreferenceManager.getDefaultSharedPreferences(this);
sp.getBoolean("CHECKBOX", false);}
private void savePrefs(String key, boolean value) {
SharedPreferences sp =
PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putBoolean(CHECKBOX, true);
edit.commit();
}
public void onClick(View v) {
// TODO Auto-generated method stub
savePrefs("CHECKBOX", cb.isChecked());
if (cb.isChecked());
}
public void pendingIntent() {
// TODO Auto-generated method stub
}
public void onSharedPreferenceChanged(SharedPreferences
sharedPreferences,
String key, boolean cbValue) {
// TODO Auto-generated method stub
if(cbValue){
cb.setChecked(true);
Intent myIntent = new Intent(getBaseContext(),
BroadcastServiceReceiver.class);
PendingIntent pendingIntent
= PendingIntent.getBroadcast(getBaseContext(),
0, myIntent, 0);
AlarmManager alarmManager
= (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
long interval = 60 * 1000; //
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), interval, pendingIntent);
PackageManager pm = getPackageManager();
ComponentName compName =
new ComponentName(getApplicationContext(),
BroadcastServiceReceiver.class);
pm.setComponentEnabledSetting(
compName,
PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0);
}else{
cb.setChecked(false);
Intent myIntent = new Intent(getBaseContext(),
BroadcastServiceReceiver.class);
PendingIntent pendingIntent
= PendingIntent.getBroadcast(getBaseContext(),
0, myIntent, 0);
AlarmManager alarmManager
= (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.cancel(pendingIntent);
PackageManager pm = getPackageManager();
ComponentName compName =
new ComponentName(getApplicationContext(),
BroadcastServiceReceiver.class);
pm.setComponentEnabledSetting(
compName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences
sharedPreferences,
String key) {
// TODO Auto-generated method stub
savePrefs("CHECKBOX", cb.isChecked());
if (cb.isChecked());
}
}
答案 0 :(得分:0)
原来我不需要Package Manager。 只需要正确构建广播接收器。
在此链接中找到。
Shared preferences not setting boolean inside boot_received broadcast receiver
感谢任何有兴趣的人。
麦克