我有一个后台服务,如果存在新数据,它会从服务器请求新数据,它会更新sharedpreferences
我的 firebase 作业中的通知显示命令,该命令显示 0、20 秒之间的通知。
通知每 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;
}
}