Office Hours — Today, February 7

Friday, February 1

Mark M.
has entered the room
Mark M.
turned on guest access
Feb 7
4:00 PM
Aron
has entered the room
Mark M.
howdy, Aro
er, Aron
Ben K.
has entered the room
Aron
Hello!
Ben K.
Hello
Mark M.
howdy, Ben
Aron
Is it just to write out the question here or is there some sort of turn system?
Mark M.
Aron: do you have a question
Aron
Sure do! 1 sec
I got a question regarding the calendar on Android. As of version 4.0 there is a calender provider API that allows one to perform query, insert and update on calendars/events/etc.
I am planning to use this API for a "room booking app", which will be used on a tablet outside of meetingrooms.
One of the things this app needs to do is to see if the room is currently booked or not. As people can book the room over the web (Google Apps) I need to continuously check if the room is available or not and update the status accordingly.
The implementation I have think about implementing is to have a service running that checks against the calendar every 5 seconds (and the calender uses push-notifications automatically by Android if I'm not mistaken).
My question to you is simply, is there any way to see when there has been an update to an event/calendar without having to check every few seconds. For example is there some sort of "broadcast message" sent out by the calendar everytime it syncs(gets info pushed to it)?
Appreciate any thought on this. :) Thanks for a great service.
Mark M.
register a ContentObserver
4:05 PM
Aron
Could you elaborate a little bit, I'm still kind of a newbie on Android. I can use contentobserver to see when the calendar has been updated?
Mark M.
yes
(sorry, I'm on an emergency phone call right now)
Aron
No problems, you can write here when you got the time! (i will read the API for contentobserver meanwhile)
Mark M.
OK, call over for now
you can register a ContentObserver to be notified when there is a change to some piece of content, represented by a Uir
er, Uri
Aron
okay. So I can be told for example if a database has been updated
Mark M.
right
Aron
which URI should I use for the calendar?
Mark M.
um, I don't know
the one you are querying on, presumably
or, the one for an individual event, if you want it scoped to a single event
the catch with a ContentObserver is that you have to be running
it's not like AlarmManager, which works even when your app is not running
Aron
Ah
Mark M.
personally, I would not "continuously check", but rather check only while the app is running
4:10 PM
Mark M.
unless you are trying to implement some sort of alert mechanism
in which case, polling is pretty much your only option that I can see
Aron
View paste
ContentResolver cr = getContentResolver();
Uri uri = Calendars.CONTENT_URI; 

is the uri i might be looking for "uri" here?
Mark M.
that would tell you about changes to which calendars are subscribed
it might tell you about changes within those calendars, but I cannot be sure
you may need the Uri for a specific calendar, the one containing the room bookings that you are monitoring
Aron
does it tell me what changes or just that "there are changes"?
Mark M.
just that the data was changed
you would query the ContentProvider to get the updated information, then determine if the change is something that matters to you
Aron
Ah!
That's perfect then, will probably work much better than polling all the time
Mark M.
let me take a question from Ben, and I'll be back with you shortly
Ben: do you have a question?
Ben K.
Yes, I am using an AlarmManager with you WakefulIntentService
Can I have multiple subclasses and alarms?
Mark M.
you should not need multiple subclasses
you can certainly have multiple alarms
Ben K.
So in order to tell which alarm is triggering I can add extras to the intent?
Mark M.
that's a likely solution
4:15 PM
Ben K.
Ok
Mark M.
make sure you use unique request codes when you create your PendingIntents
(second parameter on getBroadcast(), etc.)
Ben K.
Ok
Mark M.
you'd use one PendingIntent per distinct alarm, routed to your BroadcastReceiver
Ben K.
right
Mark M.
the receiver would turn around and use the same Intent for sendWakefulWork(), to pass along the extras
Aron: do you have another question?
Aron
I think I'm good for now. Still unsure about how the classes work but that is my own job to learn. :) Thanks for the help!
Mark M.
OK
Ben: do you have another question?
EGHDK
has entered the room
Mark M.
howdy, EGHDK!
4:20 PM
Mark M.
EGHDK: do you have a question?
4:20 PM
Ben K.
I don't have another question right now, I'm just going to hang out
Mark M.
Ben: OK, no problem
EGHDK
Hey Mark, I'm trying to get an imageView to move from it's location (bottom of the screen) towards the top on the screen, while holding down on a button. So when the button is being pressed down, it moves up and will stop at the top, but if you let go, it slowly moves back down to it's original position. I'm just kinda stuck on how to go about this. I've successfully gotten a button press that moves an image view up, but I don't know how to move the posiition up while the button is being pressed. As of right now, it just goes up one it's clicked. Sorry if it's a bit confusing.
Mark M.
I guess I
er
I guess I'm not sure what your specific question is
EGHDK
Well now it goes onClick move up this much.
Mark M.
you'd need to manage low-level touch events on the button for this, as there is no concept of "button being pressed" in Android
onClick() will not help you for your problem
in fact, a Button may not be the right solution
EGHDK
onLongClick?
lol
How about an imageview that "behaves" like a button? And reacts to "onTouch"?
Mark M.
probably you will need to create your own custom subclass of something, where you handle the onTouchEvent() call, as you may need to manage the background states yourself
EGHDK
onTouch move x amount up.
4:25 PM
Mark M.
something like that, yes
EGHDK
Okay, so that sounds reasonable. How do I handle "notOnTouch" move x amount down?
Mark M.
use an animation, presumably
EGHDK
Well, I've been using animation to move the imageView up right now.
Mark M.
right
you'd run a separate animation to move it down
where "down" would be "all the way", presumably
EGHDK
How would the separate down animation be started?
It should ideally be started when you remove your finger from the "button"
Mark M.
hence, when onTouchEvent() indicates that the user has lifted their finger, start the down animation
bear in mind that I haven't done anything much like what you're describing, so I am taking educated guesses -- more or less what I'd start trying if I had to do it
EGHDK
Yes, but your educated guess is worth a lot more to me than my educated guess. heh
4:30 PM
EGHDK
Okay, so stupid question. Is there any tutorial on reading documentation?
I'm still pretty new to java (IMO) so when I go to read android docs, I get confused more than anything else.
Mark M.
um
well
not really
I mean, the "right" answer is for you to get more comfortable with Java, JavaDocs, etc., whether inside or outside of Android
but we still haven't invented the microSD->brain interface
so you can't just put the Java card behind your right ear and absorb it
nor do we have Matrix-style "I know kung fu" technology
:-)
EGHDK
Not yet.
So let me use this example. Because it's had me stuck for the longest.
Let's say I wanted to perform a toast message.
I go to d.android.com
I type in toast, and see android.widget.toast. I click on that. And I go down to...
Public Methods
4:35 PM
EGHDK
and I see "Make a standard toast that just contains a text view."
So I figure, this is what I want to use. But then I start to create the toast. Toast.makeText(this, "myToast", LENGTH_LONG);
But length long doesn't work.
Mark M.
that is a static data member on the Toast class
EGHDK
It has to be Toast.LENGTH_LONG. But I don't see that anywhere.
Mark M.
LENGTH_LONG is defined on that very page
(or, use Ctrl+F to search within the page in your Web browser)
EGHDK
Exactly! It says "Show the view or text notification for a long period of time."
Mark M.
public static final int LENGTH_LONG
EGHDK
I guess to all comes down to me still not fully understanding "static"
Mark M.
and that's a fairly fundamental Java construct
with corollaries in most major OO programming languages
that blog post lists a number of Java topics that Android developers need to know
fifth bullet down is "Static and instance scope", pointing here: http://en.wikibooks.org/wiki/Java_Programming/U...
4:40 PM
Ben K.
I've got another question
Mark M.
Ben: go ahead
Ben K.
I have this code:
View paste
	public static void scheduleAlarms(Context ctxt) {
		AlarmManager mgr = (AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
		Intent i=new Intent(ctxt, SkootCommandService.class);
		PendingIntent pi=PendingIntent.getService(ctxt, 0, i, 0);
		mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
			SystemClock.elapsedRealtime() + __pollStartTime, __pollPeriod, pi);
	}
do I add the extras to the Intent i?
Mark M.
um
is SkootCommandService supposed to be the WakefulIntentService?
Ben K.
yes
Mark M.
then you're using WakefulIntentService wrong
Ben K.
Ok
Mark M.
for WakefulIntentService, you need to use a *broadcast* PendingIntent, routing to a BroadcastReceiver that can turn around and call sendWakefulWork() on WakefulIntentService
this is covered in the book in the chapter on AlarmManager
it is also covered in the README to the cwac-wakeful GitHub repo
4:45 PM
Mark M.
EGHDK: back to you, if you have another question or a follow-up on your earlier one
4:50 PM
Mark M.
if anyone has any questions, chime in
Ben K.
I've changed the getService to getBroadcast and mirrored your example from the book
Where do I add the extras?
Mark M.
you have two choices
1. add them in the BroadcastReceiver
2. add them to the Intent used in the getBroadcast() PendingIntent, in which case you need your BroadcastReceiver to pass those same extras along to the service
Ben K.
so the intent won't automatically keep the extras
?
I just need to pass in the same intent right
Mark M.
one flavor of sendWakefulWork() takes an Intent
that Intent needs to have the extras
how the extras get there is up to you
Ben K.
I see
Mark M.
for example, in my option #2, you could use the copy constructor to create an Intent for use with sendWakefulWork()
Ben K.
If I place the extras in the intent used in getBroadcast(), that intent will return in onReceive, and I can just pass in that intent to sendWakefulWork?
Mark M.
or possibly just use the original Intent passed to onReceive(), without copying it
the Intent for sendWakefulWork() needs to identify the service
your Intent received in onReceive() identifies the BroadcastReceiver
so, you'd need to call setComponent() to change it
Ben K.
Ok
4:55 PM
Ben K.
Is there anything else I need to do?
Mark M.
that is difficult for me to answer
Ben K.
Ok
Mark M.
at the end of the day, the Intent passed to sendWakefulWork() must identify the service that is your WakefulIntentService and contain whatever extras you want
how you get to that point is up to you
Ben K.
k thanks for your time1
Mark M.
any last questions?
EGHDK
Thanks for the resources. I will have some work cut out for me this weekend. Can't believe I never stumbled upon your blog posts. Thanks. I'm outta here. Have a good night. I don't think the storm is supposed to hit you too hard right Mark? I'm in NJ, way too many emergency alert text messages. hahaha
Mark M.
6-8 inches here, I think
no, 6-12
EGHDK
Stay warm. See ya at the next office hours
Mark M.
which are tomorrow at 10am Eastern
5:00 PM
Mark M.
the transcript of this chat will be archived at http://commonsware.com/office-hours/ shortly
this concludes today's chat -- have a pleasant day, all!
Aron
has left the room
Ben K.
has left the room
EGHDK
has left the room
Mark M.
turned off guest access

Friday, February 1

 

Office Hours

People in this transcript

  • Aron
  • Ben K
  • EGHDK
  • Mark Murphy