Service referenced by local variable, yet holds value between function calls - android

I was playing around with services and dialogs, and I got a doubt. Within a dialog, I am starting a service like this:
Intent lock = new Intent(getActivity(),AppLockService.class);
getActivity().stopService(lock);
getActivity().startService(lock);
Now the first time I call the dialog through
dialog_name.show(getFragmentManager(), "dropbox");
Upon pressing the OK button, the intent is launched. Now later, during the same app execution, the dialog is triggered again ( which is according to my code logic -- nothing wrong here). The code in the dialog then stops the previously triggered intent and starts the new intent.
My question is this:
lock is a local intent variable as per my definition. So how does it know that it has to stop that particular service I have triggered here the first time? Would someone please explain this to me?

You don't have to keep track of the service in a variable because Android does it for you.
The way that the OS treats a service is that it will not allow more than one instance of the service be to running at any time.
So at any moment there are 0 or 1 instances of your service. If there are 0, no problem, the OS will ignore the call to StopService. If there is 1 instance, it must be the instance you started previously - so it will be stop that one.

Related

call method while application closed

How can I call a method while my application is closed on Android?
I've tried:
Onpause(), OnDestroy(), and OnStop(). With no luck.
What I want is receiving something from the database and do my reaction
based upon when the application is closed.
I think you can use broadcast or service to do it.
You could create a base activity (BaseActivity class) from which all your activities would have to be derived. Then inside BaseActivity.onCreate - you would increase some SharedPreferences counter, and inside BaseActivity.onDestroy you would decrease it. Now when that counter is equal to zero, you might assume your application is closed - but the process might still run in the background.
What I want is receiving something from the database
now I assume you already know how to "receive something from database". This could be background Service, where you could check SharedPreferences and do your processing.
What is the trigger?
Part 1 : Intent -
By example, you can set an alarm, that launch an Intent.
Part 2 : Broadcast Receiver - You must create a Broadcast Receiver (which reacts to an intent).
Part 3 : Service - The Broadcast Intent will launch a service that will execute when the application is not open.

logcat warning message : "showStatusIcon on inactive InputConnection" in Android

Application Cycle :
I have a mainActivity, which starts a service, bind with it, and finish() after X seconds, the bind should be broken but the service should keep running.
The service with a specific trigger will start the MainActivity again (startService will not be called , i check if the service already exists).
So on the first time mainActivity finishes, the warning doesn't appear, while on the second time and on it does.
I tried removing all relevant 'binding' calls so no bind will be made and the warning still appears, it leaves us with a single option : is it because i start the service and not stopping it ? if so , how should I do it without getting this message ?
I start the Service using the following code :
Intent intent = new Intent(getApplicationContext(), LockerService.class);
intent.addCategory("LockerServiceTag");
startService(intent);
The stop is from another activity and not relevant since its not the problem for sure.
I DONT want to stop service when exiting.
Any insights why this warning message happens ?

Android notification checking from service?

I'm currently writing an app, what has an on-boot service. This service simply pops up a notification after a time, and update it in given periods with new info. Currently there's no need for any further battery saving, I just want the update sequence to work.
I've hit a problem tho. Created the boot BroadcastReceiver what then starts my Service. It it, I do the work via a Timer and a TimerTask. But as I saw, there's no easy way to check if the notification was deleted, and thus not try to update it, but re-create it.
What I want to do:
private Notification n;
NotifTask extends TimerTask {
public void run() {
if(n.exists){
// Update notification
}else{
n = Notification.Builder(context).[customize it].build();
}
}
}
And in the service's onStart, set up a timer what runs this task every 10 seconds.
Now, my question is: is there any way to do the "n.exists" part easily, without intents?
If you post a notification and this notification already exists, it will just get updated with the new information. If it doesn't exist it will be created. I don't think you need to do anything special to get the behaviour you want.
From the documentation for NotificationManager.notifiy():
Post a notification to be shown in the status bar. If a notification
with the same id has already been posted by your application and has
not yet been canceled, it will be replaced by the updated information.

how to pop up alarm even when the application is closed

I'm developing a location based alarm which is an Android application.
All the coding part has been done.
But the alarm alert dialog is not displaying when the application is closed.
Please help me, I'm trying to pop up the alarm even when user is using another application or is in the home screen.
The code of my alarm at the moment is shown below:
final MediaPlayer mp = MediaPlayer.create(LocAlarmProject.this, R.raw.airtel);
mp.start();
// LocAlarmProject.this below is what's causing the problem:
final AlertDialog.Builder builder=new AlertDialog.Builder(LocAlarmProject.this);
builder.setTitle(disp_title);
builder.setMessage(disp_desc);
builder.setIcon(R.drawable.alarm);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mp.stop();
}
});
builder.show();
I am pretty sure you are looking for Service.
Copied and Paste:
A Service is an application component representing either an
application's desire to perform a longer-running operation while not
interacting with the user or to supply functionality for other
applications to use. Each service class must have a corresponding
declaration in its package's AndroidManifest.xml. Services
can be started with Context.startService() and Context.bindService().
Use Notification service to keep track of the status.....visit......http://developer.android.com/guide/topics/ui/notifiers/notifications.html
The proper way to do this requires a few steps. Sorry, it's not trivial if you haven't done it before:
First, you're going to use Android's built-in AlarmManager to schedule the time you want your app to be activated. You schedule a new alarm with the time to wake up and a PendingIntent.
Second, create your PendingIntent that's used to activate your app for the time to show the alarm.
Third, you can handle this Intent several ways, the most common is to create a class that extends BroadcastReceiver to receive the Intent. This class is notified when it's time to show the alarm. The Intent that you created and put inside your PendingIntent is passed to its onReceive() method. You pass this information to your app to display the alert dialog. --- You can alternatively just register your main app to receive the Intent instead, then override the onNewIntent(Intent) method in your Activity.
Four, register your BroadcastReceiver in your AndroidManifest.xml file. This is also the place to register this receiver to listen for the Intent you created. You do this using the <intent-filter> tag.
Five, (optional) if you want your alarm to display even if the phone is asleep, and you want to make sure it doesn't go back to sleep before the user acknowledges the alarm, you'll need to obtain a WAKE_LOCK to do so.
The reason this is preferable to using a Service is it doesn't consume resources just to wait for the alarm, and also it still works if Android decides to kill your app free up memory. Good luck and happy coding!

Recommended method of starting a scheduled activity on start up?

Im trying to make an scheduled activity go off every hour or so, all working in the background.
Right now i have a BroadcastReceiver that picks up when the device is booted.
The BroadcastReceiver creates a PendingIntent to an activity (Called AlarmController) that creates has all necessary methods that i need for making the scheduled activity to go off.
How ever, this doesnt seem to work.
This is how my BroadcastReciever class onReceive{} looks like and is indentical to my main activity onCreate{}(Only for testing)
Intent intent = new Intent(serviceactivirt.this, AlarmController.class);
PendingIntent sender = PendingIntent.getActivity(serviceactivirt.this, 0, intent, 0);
try {
sender.send();
} catch (CanceledException e) {
Toast.makeText(getApplicationContext(), "FEJLSAN", Toast.LENGTH_LONG).show();
}
This actually works, except that my app crashes at launch, but the scheduled activity is working...
Any ideas? Is this "The way to do it" or is there a more recommended way?
Cheers!
Solution:
Instead of having a BroadcastReciever calling an Activity, i made the BroadcastReciever starting a Service. And changed my Activity to a Service, programmaticly and in manifest.
Works great!
Im trying to make an scheduled activity go off every hour or so, all working in the background.
Please allow users to configure other options, such as using a Notification, rather than being interrupted by an activity taking over the foreground.
Right now i have a BroadcastReceiver that picks up when the device is booted.
You would only need that to set up an AlarmManager schedule for your hourly events. Your PendingIntent for the AlarmManager could be one you obtain via getActivity().
How ever, this doesnt seem to work.
If you want to start an activity, call startActivity(). Do not create a PendingIntent, then immediately send() the PendingIntent.
Also, get rid of getApplicationContext() and simply use this.
except that my app crashes at launch
Use adb logcat, DDMS, or the DDMS perspective in Eclipse to examine LogCat and look at the stack trace associated with your crash.

Categories

Resources