Way to share data with service in android - android

I have an application where user will choose whether he wants Vibrate or Silent mode. And from the Main Activity I am passing the data as part of Intent to the service (the service is always running).
In the onStartCommand method of service, I get the data for first time and everything works fine. But when I exit the application, after some time may be the service's onStartCommand method is again invoked with no data in the Intent (may be Launcher or android OS is doing it).
Since I am setting a local String variable with data from Intent, I get Null Exception when the onStartCommand method is invoked by OS.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(mode == null) {
mode = (String) intent.getExtras().get("mode");
}
return super.onStartCommand(intent, flags, startId);
}
Now my problem is how to get the data from activity for the first time and then refer to it in various member methods of service class.
I have tried making the mode variable as static but same issue. Looks as if OS will unload service class at its discretion and then load it back and invoke onStartCommand with plain Intent.

You must use a SharedPreference to save the state of the variable Vibrate or Silent mode. Here a possible solution:
SharedPreferences preferences = getApplicationContext()
.getSharedPreferences("preferences_name", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("mode", "silent"); // Silent Mode
// editor.putString("mode", "vibrate"); //Vibrate Mode
editor.commit();

Related

Intent is Null all the time in service

I have two Application A and B.In app B I have a service that I can run it from app A. I want to send data to app B with intent but always my intent is null!
I run app B's service from app A with this Code:
try {
String packageName = "app_B_package";
String appService = packageName + ".activity.InternetService";
Intent start = new Intent();
start.setComponent(new ComponentName(packageName, appService));
start.putExtra("LAUNCHER_COMMAND_CLOSE" , true);
G.context.startService(start);
} catch (Exception e) {
e.printStackTrace();
}
But when service of app B will run the intent is null. This is onStart of the service in app B:
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i("LOGO_OFFICE_IN", "onStart");
if (intent != null) {
if (intent.getExtras().getBoolean("LAUNCHER_COMMAND_CLOSE")) {
Tools.clearApplicationData(InternetService.this);
new AppStatus(InternetService.this).isAppRunning(getPackageName(), true);
}
}
}
Why my intent is null all the time? I can't find it out.
Thank you for your help.
It looks like your service is type fire-and-forget - it does one quick thing and should quit immediately because it's done. Correct?
1. Don't leave your idle service running
Documentation says
If a component starts the service by calling startService() (which results in a call to onStartCommand()), the service continues to run until it stops itself with stopSelf() or another component stops it by calling stopService().
so after your workload is done call stopSelf().
When your service is not running there's nothing to restart.
2. Use correct start mode
Unless you stop it, your service is by default automatically restarted after it's killed by system (because system needed resources). The default mode is called START_STICKY and does this:
This mode makes sense for things that will be explicitly started and stopped to run for arbitrary periods of time, such as a service performing background music playback.
Since your service is a quick one-time job, it makes no sense for it do be restarted later at an arbitrary time.
To let Android know, you should return START_NOT_STICKY from onStartCommand.
3. Use current API
Don't use onStart, it was deprecated 9 years ago. It doesn't support start modes mentioned above. Implement onStartCommand instead. Your service would look like this:
#Override
public void onStartCommand(Intent intent, int flags, int startId) {
// No super call.
Log.i("LOGO_OFFICE_IN", "onStart");
// Intent cannot be null.
if (intent.getExtras().getBoolean("LAUNCHER_COMMAND_CLOSE")) {
Tools.clearApplicationData(InternetService.this);
new AppStatus(InternetService.this).isAppRunning(getPackageName(), true);
}
stopSelf(); // Work is done, stop service.
return START_NOT_STICKY; // Don't restart if killed.
}
Now that I think of it, only step 1 is absolutely necessary. Anyway, get into habit of using current APIs and finding out how things work.

Application variables in service

I create the app with a foreground service. The service runing in another process and thread.
The Application class have an Array and have public method getArray. Service can access to Application and call method getArray.
App started, array fill and service started.
If app change the array, service return the array created at start app.
How i can update the array in the service?
on service:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(mThread != null)
mThread.interrupt();
mApplication = (Application) getApplication();
mTaskManager = mApplication.getTaskManager();
TaskArray lTasks = mTaskManager.getAll();
// always return same size
Log.d(mLogTag, "onStartCommand " + lTasks.size());
createThread();
mThread.start();
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
RESULT:
I was found the reason of getting a copy of applicaton object in my app. This is happening because getApplicationContext() return the context of the single, global Application object of the current process.
For each process created a copy of application object.
Get the context of Application class by using getApplicatonContext() in your service class by writing this line ((YourApplicatonClassName)getApplicationContext()).getArray() and set the new data value .
It is better, if possible, to share your code so that I can understand what you exactly want.
Thanks

Application starts new service when app is closed

I have an android app with service that has to track user's location even with closed application only if user set flag in app. So I start service in code after user clicks button and stop when he clicks it again with following code
alarm.changeAlarmStatus();
if(alarm.getAlarmStatus()) {
SharedPreferences.Editor ed = alarmPreferences.edit();
ed.putFloat("MARKER_LATITUDE", (float) theMarker.getPosition().latitude);
ed.putFloat("MARKER_LONGITUDE", (float) theMarker.getPosition().longitude);
ed.commit();
startService(myIntent);
}
else{
stopService(myIntent);
}
It seems to work fine. Service works and does what it should. But problem is if I close application through the task manager I see in logcat that Service starts again(but only if service is already working) and it causes nullPointerException because intent is null. You can see in this code why it happens
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Bundle bundle = intent.getExtras();
alarm = (Alarm) bundle.getSerializable("alarm");
Log.d("service","On Start Command is called ");
serviceIntent = intent;
return START_STICKY;
}
However I need to start my service only by pressing button and no other way. Does anyone know how to fix that?
Return START_NOT_STICKY in onStartCommand instead of START_STICKY.
If START_STICKY is returned, system will try to re-create service after it is killed.
If START_NOT_STICKY is returned, system will not try to re-create service after it is killed.

Grab intent information from a service, only once

I am running a service. My ideal process would be to call a method once, get the intent/extras and set parameters, then run my service for a given amount of time.
From my understanding, onCreate() is called only once, while onStartCommand is called every time your service is called. This currently puts me in a backward situation, the method I want called once, doesn't have access to the intent extras, and I don't want to reassign parameters each time the startService is called through the method that has intent access (or should I not care about this happening?).
Is the only way to go about getting what I want to happen the way I want it, to just restrict the ability to start the service if it is already running?
Thanks in advance.
put a static int flag in onCreate and raise it to 1 on first call then check it in OnStartcommand whether this flag is 1 or zero if zero do what ever u want to do else do nothing
static int flag=0;
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
flag=1
}
public int onStartCommand(Intent intent, int flags, int startId) {
if(flag==0){
//run ur code
}
}

cancelling an alarm after program crash

I have a background service which sets a repeating alarm, does its task and stops itself. Then when the alarm wakes up it starts the service again. If the program crashes the alarm is still around and wakes up the alarm broadcastreceiver. Is there any way to cancel the alarm on a crash - I suppose I might be able to cancel the alarm from any caught exceptions but what about other causes?
Or when the alarm Broadcast receiver is triggered is there any way to tell if the program that set it has crashed?
The activity lifecycle can detect a clean finish from an "unexpected" termination through use of the isFinishing() function in the onDestroy() callback. You could add this to each activity in your app:
protected void onDestroy () {
boolean crashedOnLastClose = false;
if (!isFinishing()) {
// The application is being destroyed by the O/S
crashedOnLastClose = true;
// Store the result somewhere for the BroadcastReceiver to check later
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("crashedOnLastClose", crashedOnLastClose);
editor.commit();
}
}
Then in your BroadcastReceiver, pull the result back from the SharedPreferences framework (or wherever you decide to store it), and cancel the action if the value is true:
public class MyAlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean crashedOnLastClose = settings.getBoolean("crashedOnLastClose", false);
if (!crashedOnLastClose) {
// No crash on last run, handle the alarm
// ...
}
}
}
Remember to reset it back to false on next launch though! Something like this should do:
protected void onCreate (Bundle savedInstanceState) {
// Make sure this always resets to FALSE on launch
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("crashedOnLastClose", false);
editor.commit();
}
This should capture your crash events; but remember it will also detect other times when your app is force-closed - such as if the memory is low on the device. To distinguish between these kinds of events you would need to inspect the system status as well as isFinishing().
EDIT - Services
Your Service has a separate lifecycle, you have to treat it almost like a separate app. As you've noticed, there is no "isFinishing()" here, but Services are actually quite simple since they are only terminated cleanly by your code, which you can trap pretty easily.
In your Service, add a new boolean (perhaps called "isFinishing") and set it to true when your service is finished with. Then override onDestroy() as well and add a check similar to the one I described for your activities:
protected void onDestroy () {
// Store the result somewhere for the BroadcastReceiver to check later
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("crashedOnLastClose", !isFinishing);
editor.commit();
}
The broadcast receiver will then use the same code we added earlier to detect if either the service or an activity crashed.

Categories

Resources