BillyBoFilly
Lurker
Just signed up to this awesome forum! I was wondering if you guys could help me with this problem I'm having...
I have created a Notification and in my SharedPreferences I enabled notifications, but I can't see any notifications popping up in my JobScheduler classes.
NotificationClass...
Now this is the activity where I set my SettingsActivity. Here is where I schedule the Job. In the onSharedPreferences() method is where I am defining the Job and wanting it to display.
Any help would be greatly appreciated!
I have created a Notification and in my SharedPreferences I enabled notifications, but I can't see any notifications popping up in my JobScheduler classes.
NotificationClass...
Code:
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
public class NotificationJobService extends JobService {
/**
* This is called by the system once it determines it is time to run the job.
* [USER=315340]@param[/USER] jobParameters Contains the information about the job
* @return Boolean indicating whether or not the job was offloaded to a separate thread.
* In this case, it is false since the notification can be posted on the main thread.
*/
[USER=1021285]@override[/USER]
public boolean onStartJob(JobParameters jobParameters) {
//Set up the notification content intent to launch the app when clicked
PendingIntent contentPendingIntent = PendingIntent.getActivity
(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "my_channel")
.setContentTitle(getString(R.string.pref_title_notification))
.setContentText(getString(R.string.pref_summary_notification))
.setContentIntent(contentPendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setAutoCancel(true);
manager.notify(0, builder.build());
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME, System.currentTimeMillis(), contentPendingIntent);
return false;
}
/**
* Called by the system when the job is running but the conditions are no longer met.
* In this example it is never called since the job is not offloaded to a different thread.
* [USER=315340]@param[/USER] jobParameters Contains the information about the job
* @return Boolean indicating whether the job needs rescheduling
*/
[USER=1021285]@override[/USER]
public boolean onStopJob(JobParameters jobParameters) {
return true;
}
}
Now this is the activity where I set my SettingsActivity. Here is where I schedule the Job. In the onSharedPreferences() method is where I am defining the Job and wanting it to display.
Code:
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.PreferenceManager;
public class SettingsActivity extends AppCompatActivity implements
SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = SettingsActivity.class.getSimpleName();
private static final int JOB_ID = 88;
[USER=1021285]@override[/USER]
protected void onCreate([USER=1996173]@nullable[/USER] Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment()).commit();
}
[USER=1021285]@override[/USER]
protected void onResume() {
super.onResume();
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(this);
}
[USER=1021285]@override[/USER]
protected void onPause() {
super.onPause();
PreferenceManager.getDefaultSharedPreferences(this)
.unregisterOnSharedPreferenceChangeListener(this);
}
[USER=1021285]@override[/USER]
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
final String notifyKey = getString(R.string.pref_key_notification);
if (key.equals(notifyKey)) {
boolean on = sharedPreferences.getBoolean(notifyKey, false);
JobScheduler jobScheduler =
(JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
}
}
/**
* loading setting resource
*/
public static class SettingsFragment extends PreferenceFragmentCompat {
[USER=1021285]@override[/USER]
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preference);
}
}
}