JobScheduler Won't Run in the Background - android

I am trying to run an accounting calculator in the background using JobScheduler, but somehow, it won't do the calculation. The program runs smoothly, thats why it confuses me why it doesn't work. I appreciate your help.
Here's my Job Service class:
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class MyJobService extends JobService {
private JobParameters params;
int num1a;
int num1b;
int num2a;
int num2b;
int num3a;
int num3b;
int num4a;
int num4b;
int num5a;
int num5b;
int sum1;
int sum2;
int sum3;
int sum4;
int sum5;
SharedPreferences preferences;
SharedPreferences.Editor editor;
#Override
public boolean onStartJob(JobParameters params) {
this.params = params;
Toast.makeText(getApplicationContext(), "Start", Toast.LENGTH_LONG).show();
new MonthlyTask().execute();
return true;
}
#Override
public boolean onStopJob(JobParameters params) {
return false;
}
private class MonthlyTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPostExecute(Void xVoid) {
super.onPostExecute(xVoid);
jobFinished(params, false);
Toast.makeText(getApplicationContext(), "Finish", Toast.LENGTH_LONG).show();
}
#Override
protected Void doInBackground(Void... params) {
preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor = preferences.edit();
try {
num1a = preferences.getInt("expense1", 0);
num1b = preferences.getInt("subtotal1", 0);
num2a = preferences.getInt("expense2", 0);
num2b = preferences.getInt("subtotal2", 0);
num3a = preferences.getInt("expense3", 0);
num3b = preferences.getInt("subtotal3", 0);
num4a = preferences.getInt("expense4", 0);
num4b = preferences.getInt("subtotal4", 0);
num5a = preferences.getInt("expense5", 0);
num5b = preferences.getInt("subtotal5", 0);
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
sum1 = num1a + num1b;
sum2 = num2a + num2b;
sum3 = num3a + num3b;
sum4 = num4a + num4b;
sum5 = num5a + num5b;
editor.putInt("subtotal1", sum1);
editor.putInt("subtotal2", sum2);
editor.putInt("subtotal3", sum3);
editor.putInt("subtotal4", sum4);
editor.putInt("subtotal5", sum5);
return null;
}
}
}
Here's my Main Activity
public class MainActivity extends AppCompatActivity implements android.view.View.OnClickListener{
//Do stuffs
public void onClick(View view) {
if (view == findViewById(R.id.btnSave)) {
JobInfo.Builder builder = new JobInfo.Builder(1, new ComponentName(getPackageName(),
MyJobService.class.getName()));
//run job service after every 5 seconds, which put back to 15 mins...but thats fine
builder.setPeriodic(5000);
builder.setPersisted(true);
jobScheduler.schedule(builder.build());
finish();
}}

I am trying to run an accounting calculator in the background using JobScheduler
This example is really strange.
but somehow, it won't do the calculation
Um, well, if by "the calculation", you mean addition, it is doing the addition. However, you then throw away the results. You edit() the SharedPreferences without calling apply() or commit() to save the changes.

Related

How to stop IntentService Android

I have build an IntentService in Android.
So if I received a pushNotification message, I muse stopped this service.
public class DosimeterDataSensingService extends IntentService {
private static final String TAG = "DosimeterDataSensingService";
public static boolean isStarted = false;
private Context mContext;
private int mStatus;
private BluetoothDevice mDevice;
private BluetoothGatt mConnGatt;
private boolean notificationsEnabled;
private long dosimeterScanningTime;
private boolean isThreadStarted = false;
private List<DosimeterDataRequest.DataBean> dosimeterDataList;
private List<DosimeterDataRequest.DataBean> dosimeterDataListMqtt;
private DosimeterMqttParcel dosimeterMqttParcel = new DosimeterMqttParcel();
private DosimeterDataRequest dosimeterDataRequest;
private String macAddress;
private boolean isRecordingEnabled = false;
private String dose = "";
private int battery = -1;
private Boolean syncRadiactionWS = false;
private Boolean syncRadiactionMQTT = false;
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* #param name Used to name the worker thread, important only for debugging.
*/
public DosimeterDataSensingService(String name) {
super(name);
}
public DosimeterDataSensingService() {
super(null);
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
protected void onHandleIntent(#Nullable Intent intent) {
Log.d("dosimetro", "ON HANDLE INTENT");
if(intent!=null){
String action = intent.getStringExtra(PreferenceHandler.ACTION_STOP);
Log.d("dosimetro", action!=null ? action : "no action");
if(action!=null && action.equals(PreferenceHandler.ACTION_STOP)){
Log.d("dosimetro", "fermo il servizio");
String syncScanTime = PreferenceHandler.readString(getApplicationContext(), PreferenceHandler.DOSIMETER_SCANNING_TIME, null);
Log.d("dosimetro", syncScanTime!=null ? syncScanTime : "nullo");
String syncRotTime = PreferenceHandler.readString(getApplicationContext(), PreferenceHandler.DOSIMETER_ROTATION_TIME, null);
Log.d("dosimetro", syncRotTime!=null ? syncRotTime : "nullo");
super.stopSelf();
Log.d("dosimetro", "ho stoppato");
onDestroy();
return;
}
}
}
#Override
public void onCreate() {
super.onCreate();
Paper.init(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = createNotificationChannel();
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
.setSmallIcon(R.drawable.logoxhdpi)
.setCategory(Notification.CATEGORY_SERVICE)
.setContentTitle("Cardio App")
.setContentText("Getting data from Dosimeter")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
Notification notification = mBuilder.build();
startForeground((int) (System.currentTimeMillis() + 1), notification);
isStarted = true;
if (PreferenceHandler.readString(this, PreferenceHandler.TYPE_USER, null).equals("2")) {
checkDosage();
}
List<GetAssignedDevicesListResponse.Parameters> preferenzeParametri= Paper.book().read(PreferenceHandler.PARAMETRI_VITALI, new ArrayList<>());
if(preferenzeParametri!=null && preferenzeParametri.size()>0){
for (GetAssignedDevicesListResponse.Parameters p: preferenzeParametri) {
if(p.getIdParameter() == PreferenceHandler.ID_RADIACTION){
//VERIFICO COME L'RR DEVE ESSERE SINCRONIZZATO
syncRadiactionWS = p.getSyncWs()!=null ? p.getSyncWs() : false;
syncRadiactionMQTT = p.getSyncMqtt()!=null ? p.getSyncMqtt() : false;
Log.e("DOSIMETER", "syncRadiactionWS true");
}
}
}else{
Log.e("DOSIMETER", "paperi init false");
}
checkBattery();
Log.i("SCANNINGTIME", "SYNC WS STARTED");
syncRadiactionLevel();
syncMqtt();
}
#Override
public void onDestroy() {
Log.d("DOSIMETRO", "ondestroy");
isStarted = false;
disconnectDosimeter();
super.onDestroy();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
#SuppressLint("LongLogTag")
public void disconnectDosimeter() {
if (mConnGatt != null) {
isThreadStarted = false;
if ((mStatus != BluetoothProfile.STATE_DISCONNECTING)
&& (mStatus != BluetoothProfile.STATE_DISCONNECTED)) {
mConnGatt.disconnect();
mConnGatt.close();
mConnGatt = null;
mStatus = BluetoothProfile.STATE_DISCONNECTED;
}
}
try {
Method m = mDevice.getClass().getMethod("removeBond", (Class[]) null);
m.invoke(mDevice, (Object[]) null);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
Log.v("Dosimeter", "isRecordingEnabled" + isRecordingEnabled);
Log.v("Dosimeter", "Disconnecteddd");
}
/**
* Enable recording of values
*/
private void startRecordingData() {
String dosimeterRotation = PreferenceHandler.readString(getApplicationContext(), PreferenceHandler.DOSIMETER_ROTATION_TIME, null);
Log.i("ROTATIONTIME", dosimeterRotation);
long dosimeterRotationTime = 0L;
if (dosimeterRotation != null) {
dosimeterRotationTime = Long.parseLong(dosimeterRotation);
new Handler().postDelayed(() -> {
isRecordingEnabled = true;
isThreadStarted = false;
}, dosimeterRotationTime);
}
}
}
To stop the service I m using this code:
Intent i = new Intent(this, DosimeterDataSensingService.class);
i.putExtra(PreferenceHandler.ACTION_STOP,PreferenceHandler.ACTION_STOP);
stopService(new Intent(this, DosimeterDataSensingService.class));
From my log I can see that the system call
super.stopSelf();
onDestroy();
method but the IntentService works always.
You need not call stopSelf() or stopService() for IntentService.
As per the description mentioned in Docs:
Because most of the started services don't need to handle multiple requests simultaneously (which can actually be a dangerous multi-threading scenario), it's best that you implement your service using the IntentService class.
The IntentService class does the following:
It creates a default worker thread that executes all of the intents that are delivered to onStartCommand(), separate from your application's main thread.
Creates a work queue that passes one intent at a time to your onHandleIntent() implementation, so you never have to worry about multi-threading.
Stops the service after all of the start requests are handled, **so you never have to call stopSelf().**
Provides a default implementation of onBind() that returns null.
Provides a default implementation of onStartCommand() that sends the intent to the work queue and then to your onHandleIntent() implementation.
If the service is still running may be some intents are running.
Hope this helps.

Setting a timeout and finishing an ActivityforResult activity

I'd like to know if there's some way to set a timeout for an Intent started via startActivityForResult, so when the time is passed some actions can be performed with the activity of the mentioned intent (in my case finishing it).
There doesn't seem to be any direct way to set a timeout directly to the Intent, but this doesn't look too much to worry about, as I guess I could create a CountDownTimer that in onFinish() would call the code to finish the intent.
Problem is I don't see a way to finish that ActivityForResult.
Is there any way to do this?
Well, I finally got to solve the problem, indeed it wasn't very difficult.
For my particular case of INTENT_PICK the following code is valid to stop the activity after 2 minutes:
final int RQS_PICKCONTACT = 1;
[...]
Intent intentPickContact = new Intent(Intent.ACTION_PICK, uriContact);
startActivityForResult(intentPickContact, RQS_PICKCONTACT);
mcd = new CountDownTimer(120000, 10000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
try
{
finishActivity(RQS_PICKCONTACT);
}
catch (Exception ex)
{
}
}
}.start();
In my case I wanted to limit the time a user was doing a task so I created my own timer class.
public class Timer {
private static final String TAG = "Timer" ;
int timeout;
Context mContext;
boolean compleatedTask;
String timeoutKey;
CountDownTimer countDown;
public Timer(Context mContext, int timeout, String timeoutKey){
this.timeout = timeout;
this.mContext = mContext;
this.timeoutKey = timeoutKey;
}
public void startTimer() {
this.countDown = new CountDownTimer(this.timeout, 1000) {
public void onTick(long millisUntilFinished) {
Log.i(TAG, "OnTick, context: " + mContext + ", milisUntilFinished: " + millisUntilFinished + ", compleatedTask: " + compleatedTask);
if(compleatedTask)
cancel();
}
public void onFinish() {
Log.i(TAG, "OnFinish, context: " + mContext + ", compleatedTask: " + compleatedTask);
try
{
if(!compleatedTask){
Intent intent = new Intent(mContext, UnsuccessfullPosTaskActivity.class);
intent.putExtra("error", StateMachine.databaseAccess.getDictionaryTranslation(timeoutKey, StateMachine.language));
mContext.startActivity(intent);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
};
countDown.start();
}
public void setCompleatedTask(boolean compleatedTask){
this.compleatedTask = compleatedTask;
if(compleatedTask)
countDown.onFinish();
}
}
Then in your activity
int timeout = 1000;
Timer timer = new Timer(Activity.this, timeout);
timer.startTimer();
//do stuff
if(conditionToStop)
timer.setCompleatedTask(true);
And if you are using recycle views and you want to stop the counter when they click an option just send the timer object to your custom recycle view adapter.

Android Jobservice being destroyed

I put up a Jobservice to do some things. For test purpose I let it run every thirty seconds. To find the problem I reduced the service down to do nothing(!).
I know, that a job can be stopped by the system; if the onStopJob method returns true it will be restarted. This happens and works well, although I am wondering, how often and at which time (in the middle of the night, while the phone is just laying on the table!).
To check all these, I am using SharedPreferences, keeping track of stops and restarts.
I found out however, that the job is not only stopped, but sometimes completely being destroyed.
I then put up a restart routine in the OnDestroy method. This does not work. The job is being restarted, but immediately being destroyed again.
So I have some questions:
Why is it destroyed in the first place?
And why isnt it possible to restart it?
And what can I do to make it more stable?
Tried on a Galaxy S4 under Android 5.0.1 and XPeria Z2 under 6.0
Compiled against MinSDK 5.0, TargetSDK 6.0.
Here is the code of my JobSchedulerService:
public class JobSchedulerService extends JobService implements
DataApi.DataListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener
{
private boolean stoppedByUser = false;
public DateFormat formatter;
String stoptimeText;
public GregorianCalendar apptTime;
public Context myCnt;
#Override
public boolean onStartJob(JobParameters params){
doJob(this);
jobFinished(params, false );
return true;
}
public void doJob(Context context){
myCnt = context;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(myCnt);
SharedPreferences.Editor editor = prefs.edit();
stoptimeText = prefs.getString("stopped","not yet stopped");
editor.putString("started", "Job was running at " + timeNow());
editor.apply();
// do something here
}
#Override
public boolean onStopJob(JobParameters params){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
stoppedByUser = prefs.getBoolean("stoppedByUser", false);
SharedPreferences.Editor editor = prefs.edit();
stoptimeText += "Job stopped autom. at " + timeNow();
editor.putString("stopped", stoptimeText);
editor.apply();
if (stoppedByUser){
stoppedByUser = false;
editor.putBoolean("stoppedByUser", false);
stoptimeText += "OnStopJob called by user-stop at " + timeNow();
editor.putString("stopped", stoptimeText);
editor.apply();
return false;
}
else return true;
}
#Override
public void onDestroy() {
super.onDestroy();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
stoppedByUser = prefs.getBoolean("stoppedByUser", false);
SharedPreferences.Editor editor = prefs.edit();
stoptimeText += "Job was destroyed at "+ timeNow();
editor.putString("stopped", stoptimeText);
editor.apply();
if (!stoppedByUser){
JobInfo.Builder builder = new JobInfo.Builder(1,
new ComponentName(getPackageName(), JobSchedulerService.class.getName()));
builder.setPeriodic(30 * 1000);
builder.setPersisted(true);
stoptimeText += "Job rest. after dstr. at " + timeNow();
editor.putString("stopped", stoptimeText);
editor.putString("started", "Auto update started after destroy! Check if really running!");
editor.apply();
}
else {
stoptimeText += "OnDestroy called by user-stop at " + timeNow();
editor.putString("stopped", stoptimeText);
editor.apply();
}
}
public String timeNow(){
apptTime = (GregorianCalendar) GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"));
formatter = new SimpleDateFormat("EEE, dd./HH:mm", Locale.ENGLISH);
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
return formatter.format(apptTime.getTimeInMillis());
}
#Override
public void onDataChanged(DataEventBuffer dataEvents) {
}
#Override
public void onConnected(Bundle connectionHint) {
}
#Override
public void onConnectionFailed(ConnectionResult arg0) {
}
#Override
public void onConnectionSuspended(int arg0) {
}
}
And here is my MainActivity (shortened):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myCnt = this;
initGoogleApiClient();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
if (tabLayout != null)
tabLayout.setupWithViewPager(viewPager);
mJobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
//.......
public void startJob(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("started", "auto updates started by user!");
editor.putBoolean("stoppedByUser", false);
editor.apply();
JobSchedulerService jss = new JobSchedulerService(); // used to "first run" the job, so there is no waiting time for the user
jss.doJob(myCnt);
JobInfo.Builder builder = new JobInfo.Builder(1,
new ComponentName(getPackageName(), JobSchedulerService.class.getName()));
builder.setPeriodic(30 * 1000);
builder.setPersisted(true);
if (mJobScheduler.schedule(builder.build()) <= 0) {
Toast.makeText(MainActivity.this, "Failure starting Jobservice!", Toast.LENGTH_LONG).show();
}
}
After long research I found the following to be the problem:
In my
// do something here
I had an Async Task. However, according to Google, this is only working in the UI Thread (see: https://developer.android.com/reference/android/os/AsyncTask.html).
Having this in my JobSchedulerService caused it to be destroyed after some time.
I am now using a (regular) separate thread to "do something" in the Service, and it works.

Updating UI from Service in Android

I have an App that Monitors room noise levels, I initially got the Code from Github, in the original code, the programmer was monitoring noise levels from Main Activity and displaying the results in textviews, but I want to monitor using a service, I have implemented everything and its working but the textviews seem to be lagging behind, lets say I make a bit of noise and the noise level reach 5, it sticks at 5 even when there is no noise in the room, but in the original app, it was so sensitive that it would go back to 0 or another value depending on the noise levels, I do not know where I have gone wrong but below is my code:
Main Activity
public class StartingPoint extends Activity {
private String volumeBars;
private String volumeLevel;
private TextView volumeBarView;
private TextView volumeLevelView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(getBaseContext(), "Loading...", Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_starting_point);
//starting Service
startService(new Intent(this, VolumeListerner.class));
volumeBarView = (TextView) findViewById(R.id.volumeBars);
volumeLevelView = (TextView) findViewById(R.id.volumeLevel);
}
#Override
public void onResume() {
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter("UI_UPDATER"));
super.onResume();
// Sound based code
}
#Override
public void onPause() {
super.onPause();
}
public void updateTextView() {
volumeBarView.setText(volumeBars);
volumeLevelView.setText(volumeLevel);
return;
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
volumeBars = intent.getStringExtra("VolumeBars");
volumeLevel = intent.getStringExtra("volumeLevel");
Log.d("receiver", "Got message: " + volumeBars + " : " + volumeLevel);
updateTextView();
}
};
Service:
public class VolumeListerner extends Service {
private static String volumeVisual = "";
private static int volumeToSend;
private Handler handler;
private SoundMeter mSensor;
/** interface for clients that bind */
IBinder mBinder;
/** indicates whether onRebind should be used */
boolean mAllowRebind;
/** The service is starting, due to a call to startService() */
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
soundLevelCheck();
return super.onStartCommand(intent, flags, startId);
}
private void soundLevelCheck()
{
mSensor = new SoundMeter();
try {
mSensor.start();
Toast.makeText(getBaseContext(), "Sound sensor initiated.", Toast.LENGTH_SHORT).show();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
// Get the volume from 0 to 255 in 'int'
double volume = 10 * mSensor.getTheAmplitude() / 32768;
volumeToSend = (int) volume;
volumeVisual = "";
for( int i=0; i<volumeToSend; i++){
volumeVisual += "|";
updateUI();
}
handler.postDelayed(this, 250); // amount of delay between every cycle of volume level detection + sending the data out
}
};
// Is this line necessary? --- YES IT IS, or else the loop never runs
// this tells Java to run "r"
handler.postDelayed(r, 250);
}
private void updateUI()
{
Intent intent = new Intent( "UI_UPDATER" );
intent.putExtra("VolumeBars", "Volume Bars: " + String.valueOf(volumeVisual));
intent.putExtra("volumeLevel","Volume Levels: " + String.valueOf(volumeToSend));
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
I recommmand you to use an enhanced event bus with emphasis on Android support. you have a choice between :
1- Otto
2- Event Bus

FileObserver not reacting to CREATE

I'm trying to make an app that will react to screenshots folder and do a toast for beginning,
I'm a new developer and this is my first time using file observer so I can only guess I've made a lot of mistakes.
The problem is there is no toast or log upon taking a screenshot.
Here is the code in my observer class:
public class listeningInit extends FileObserver {
private static final String TAG = "File listener";
public String absolutePath;
public listeningInit(String path) {
super(path, FileObserver.ALL_EVENTS);
absolutePath = path;
}
#Override
public void onEvent(int event, String path) {
if ((FileObserver.CREATE & event)!=0) {
Log.v(TAG, absolutePath + "/" + path + " is created\n");
Context context = getContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "Folder action!", duration);
toast.show();
}
}
private Context getContext() {
// TODO Auto-generated method stub
return null;
}
and here's the code on activity that does .startWatching
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listeningInit startObs = new listeningInit("/Pictures/Screenshots/");
startObs.startWatching();
//Checking if this is a first run
Boolean firstRun = false;
SharedPreferences run = getSharedPreferences("MYPREFS", 0);
firstRun = run.getBoolean("fr", true);
//if true launch tutorial activity
if(firstRun == true){
Intent k = new Intent(MainActivity.this, Tutorial.class);
startActivity(k);
}
}
There's no errors thrown by the code, it's just not responding and I don't have a slight clue on why that might be.

Categories

Resources