我有一个后台服务,如果新数据存在,它会从服务器请求新数据,它会更新我的firebase作业中的sharedpreferences
,并且有一个notify show命令,显示0,20秒之间的通知。
Notiifcation每20秒显示一次,这意味着我的工作正常,但我的onSharedPreferenceChanged
没有收听为工作服务所做的更改。
是否有任何方法可以侦听来自Job的sharedPreferences更改。
家庭活动
public class HomeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
,SharedPreferences , SharedPreferences.OnSharedPreferenceChangeListener
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
testUtils.myjob(this);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
......
defaultSetup();
}
private void defaultSetup() {
SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(this);
shared.registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(this);
shared .registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(this);
shared.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// if (key.equals(Preferences.LIST)) {
// Toast.makeText(this, "cool", Toast.LENGTH_LONG).show();
// }
// if (key.equals(Preferences.CHAT_LIST)) {
Log.e("Cool","cooooooooooooool");
Toast.makeText(this, "cool", Toast.LENGTH_LONG).show();
// }
}
@Override
public Map<String, ?> getAll() {
return null;
}
@Nullable
@Override
public String getString(String key, @Nullable String defValue) {
return null;
}
@Nullable
@Override
public Set<String> getStringSet(String key, @Nullable Set<String> defValues) {
return null;
}
@Override
public int getInt(String key, int defValue) {
return 0;
}
@Override
public long getLong(String key, long defValue) {
return 0;
}
@Override
public float getFloat(String key, float defValue) {
return 0;
}
@Override
public boolean getBoolean(String key, boolean defValue) {
return false;
}
@Override
public boolean contains(String key) {
return false;
}
@Override
public Editor edit() {
return null;
}
@Override
public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
}
@Override
public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
}
@Override
protected void onDestroy() {
super.onDestroy();
PreferenceManager.getDefaultSharedPreferences(this)
.unregisterOnSharedPreferenceChangeListener(this);
}
}
Firebase作业服务
public class FirebaseJobService extends com.firebase.jobdispatcher.JobService{
@Override
public boolean onStartJob(com.firebase.jobdispatcher.JobParameters job) {
String url = ApiUtil.getNewMessage();
final Context context = this;
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONArray arr;
try {
arr = new JSONArray(response);
SharedPreferences pref1 = getSharedPreferences(Preferences.LIST, 0);
String chatList = pref1.getString(Preferences.CHAT_LIST, "[]");
arr = Utils.concatArray(arr, new JSONArray(chatList));
SharedPreferences.Editor editor = pref1.edit();
editor.putString(Preferences.CHAT_LIST, arr.toString());
editor.apply();
NotificationUtils.show(context);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestManager.getInstance(this).addToRequestQueue(stringRequest);
return false;
}
@Override
public boolean onStopJob(com.firebase.jobdispatcher.JobParameters job) {
return false;
}
}
答案 0 :(得分:1)
可能:
SharedPreferences pref1 = getSharedPreferences(Preferences.LIST, 0);
导致问题,因为为registerOnSharedPreferenceChangeListener
而不是SharedPreferences
设置getDefaultSharedPreferences
设置getSharedPreferences
。
注意:
getDefaultSharedPreferences()-
使用默认的首选项文件名
并且
getSharedPreferences-
检索并保存首选项文件'name
为了让它工作,请使用Preferences.CHAT_LIST
保存getDefaultSharedPreferences
,或在registerOnSharedPreferenceChangeListener
返回的实例的Activity中设置getSharedPreferences
。