WakefulIntentService Integration - android

I have a few questions regarding the WakefulIntentService implementation by CWAC:
1) Is it safe to use multiple WakefulIntentServices at the same time within my application?
2) Is it still ok to use my own code that handles AlarmManager? This would save me re-implementing my alarm handling code. Currently, I have a class with static methods and variables which are used by other classes within the application to set the alarm. My AlarmReceiver then starts the WakefulIntentServices by classing doWakefulWork().
This class is fantastic work!
Thanks

Is it safe to use multiple WakefulIntentServices at the same time within my application?
You should only need one. Use different Intent information (e.g., actions, extras) to distinguish the different commands.
I have not tried multiple distinct services -- while it is possible that it will work, I cannot guarantee it.
Is it still ok to use my own code that handles AlarmManager?
Oh, sure. Follow the "Basic Usage" instructions, calling sendWakefulWork() on WakefulIntentService when you want the work to be done. Just bear in mind that you must do that from a BroadcastReceiver's onReceive() if AlarmManager is the trigger -- that's an AlarmManager requirement.
This class is fantastic work!
I am glad that you like it!

Related

how to call certain method of activity via appwidget

i'm trying to call a method in my main activity via a button of a widget.
the widget has different buttons with different values. i want the method to do its job without showing up the gui attached to the main activity. (send http request with button value)
so far i searched for some tutorials but i didn't quite understand them, my code got all clumsy and it barely worked. i think a mix of intents, services and broadcasts are needed to realize this?! i really don't know, can somebody post a understandable description or tutorial which covers all aspects of how to do this?
i think there is no code or pictures to provide because there is nothing look at right now.
thanks in advance for any answer.
Android AppWidgets don't need to have any relation at all to an Activity, although it is certainly possible to have a click or something start an activity.
As you probably know, the primary interface to AppWidgets is through your AppWidgetProvider (if you don't know what any of this is about, take a look at the official android guide).
You can tell a button to use a PendingIntent to do one of several things when clicked:
To start a service (probably your best option for time consuming tasks like http requests), using PendingIntent.getService.
To start an activity, use PendingIntent.getActivity.
To broadcast a message to a BroadcastReceiver, create it using PendingIntent.getBroadcast.
In all of these cases, you would tell the button to run the PendingIntent with the
setOnClickPendingIntent(int id, PendingIntent operation) method of RemoteViews.
The Intent you pass to any of these methods should typically be created using the Intent(Context, Class) constructor, where the second argument is the service, activity or receiver class you want the pending intent to be sent to.

Using sqlite in thread that runs from alarmmanager

I want to create a simple Service (based on the ApiDemos\app\AlarmService_Service example)
In the service's thread I want to add a record to a local DB every 15Sec.
But the problem is that the DataSource I created (with the help of http://www.vogella.com/articles/AndroidSQLite/article.html article) needs Context object in order to initiate the DataSource.
In the near future I'll want a service like this example to run also when the device booted.
So my qustion is how can I use sqlite in situation where I have no Context?
So my qustion is how can I use sqlite in situation where I have no Context?
Service inherits from Context, and so you have a Context. Bear in mind that you need to have a singleton SQLiteOpenHelper (or SQLiteDatabase) if you expect other threads to also be using the database.
BTW, that sample is quite possibly the worst Google has ever published. I am not completely clear on why the user wants you to "add a record to a local DB every 15Sec" in the background, but that sample:
will fail to keep the device awake
displays a Notification for no particular reason
has you roll your own thread rather than using IntentService which handles that for you
does not handle multiple commands sent to the service
has a largely pointless Binder
etc.
The proper way to use AlarmManager with _WAKEUP alarms is either to use my WakefulIntentService or the Android Support package's WakefulBroadcastReceiver.
I have published a sample using WakefulIntentService with AlarmManager and an equivalent sample using WakefulBroadcastReceiver.

Android Activity Recognition with listener or broadcast receiver?

I'm wondering, why ActivityRecognitionClient has no method to request updates with standard java listener as parameter? More strange, that in the same time, LocationСlient has such method and it works good.
Official example for Activity Recognition looks terrible. A lot of coupled and boilerplate code. It looks like IntentService is sole variant for handling updates from ActivityRecognitionClient. It's extremely uncomfortable.
#Guys from Android Team, why it happens?
As Developer I hope to see requestActivityUpdates(interval, listener) method in next version of Google Play Services.
For now, does anyone know, is it possible to use BroadcastReceiver for handling updates from ActivityRecognitionClient?
I'm wondering, why ActivityRecognitionClient has no method to request updates with standard java listener as parameter?
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.
Beyond that, quoting the documentation for requestActivityUpdates():
A common use case is that an application wants to monitor activities in the background and perform an action when a specific activity is detected. To do this without needing a service that is always on in the background consuming resources, detected activities are delivered via an intent. The application specifies a PendingIntent callback (typically an IntentService) which will be called when activities are detected.
It looks like IntentService is sole variant for handling updates from ActivityRecognitionClient.
It's not.
For now, does anyone know, is it possible to use BroadcastReceiver for handling updates from ActivityRecognitionClient?
There are several types of PendingIntent, created from various factory methods. The sample shows using getService() to create a PendingIntent that will call startService(). You are welcome to use any other PendingIntent, such as one from getBroadcast(), which will call sendBroadcast().

Implementing unique alarms without creating my own alarm clock application

I have asked this question before (here), however it received no attention and I feel like this could really prove as useful to those who chose to embark on a similar issue.
Basically, I am implementing an application that sets alarms for the user, and in my current attempt I am using the ACTION_SET_ALARM intent to set the system alarm. Now this works fine with one exception: whenever I set an alarm, it makes a brand new alarm until eventually the alarm database is utterly filled with redundant alarms.
I'm trying to figure out how to set UNIQUE alarms without having to completely design my own alarm clock application. There must be a way to do this, simply by utilizing some feature of the native Android alarmclock class.
Methods I have employed thus far:
Android developer documents;
Pretty much every forum post on SO about this topic;
Utilizing various other intents i found by searching through the deskclock source code;
Importing the Alarms class, the setAlarm class.
The last remaining option for me is to embark on the AlarmManager class, but this is essentially recreating the alarmclock class and I want this application be generic (it can apply to most alarm clock applications) and not have to rely on its own.
Any assistance would be very appreciated.
There must be a way to do this, simply by utilizing some feature of the native Android alarmclock class.
You are welcome to your opinion, though your opinion may need to be accompanied by your own fork of Android and your own line of mobile devices.
First, there isn't really much of a "native Android alarmclock class". It is mostly just an Intent action string and a series of keys for extras. The implementation of the alarm clock functionality is device- and user-dependent, though many will simply use one from the Android open source project.
At this point, you run into two problems:
The app from the Android open source project does not support what you want
No other app has to support what you want, as it is outside the scope of documented behavior
What you need to do is use AlarmManager to broadcast out a pulse. This is the MOST RELIABLE way to get a pulse to go out. Have a broadcast receiver listen on that pulse an pass over to a service or start an Activity as needed. There you will examine your list of Alarm conditions and see if they have been met. Note you can also check timestamp at this point. Yes AlarmManager is far more reliable than any other service you can use for this.

How do I make a Android Service where I can pass information to it when it starts?

I wanna start an android service but I need to pass some info to it before it starts how can I do this?
There are a few approaches you could take.
Take a look at http://programmerbruce.blogspot.com/2011/04/simple-complete-app-widget-part-1.html for an example of adding a value to the Intent extras, which is then received by an IntentService in onHandleIntent.
Another approach would be to use an AsyncTask, which makes it very easy to provide an arbitrary data structure.
For more ideas, take a look at how Jeff Sharkey's android-sky app handles threading.

Categories

Resources