Office Hours — Today, January 8

Tuesday, January 6

Jan 8
7:25 PM
Mark M.
has entered the room
Mark M.
turned on guest access
EGHDK
has entered the room
Mark M.
howdy, EGHDK!
how can I help you today?
EGHDK
Hey. I am making a game where you'll get tokens on whether or not you've been in the app for a while. And while I'm designing the architecture myself... I'm left pondering the question if apis from the android system can be used against me?
I know it's unlikely, but I'm mostly curious about SystemClock.elapsedTime()
Can anyone change that?
Mark M.
do you mean elapsedRealtime()
?
EGHDK
So that it always returns one number or something. Yes sorry. I don't have the docs open or code in front of me.
Mark M.
I do not know of any way for somebody to change that, other than by rebooting the phone, and therefore starting the count-up process again
7:30 PM
Mark M.
a custom ROM could break it, but at that point, they'll have tons of expertise to break your system any number of ways
EGHDK
If someone was rooted can they change the way an api call works?
Micalet
has entered the room
Mark M.
not really
(hello, Micalet -- I will be with you shortly!)
Micalet
Ok Mark
EGHDK
So in a custom rom they could just change the imlementation of that method?
Mark M.
sure
EGHDK
They can change any methods... at that point... correct?
Mark M.
sure
I mean, their changes may break stuff, but that's their problem
EGHDK
Alright. I figured the answer was probably going to be "sure" or "maybe", but that's better than a definitive "no".
Mark M.
don't take this the wrong way, but it seems unlikely that somebody is going to bother hacking some custom ROM just to break your token scheme
to use Tim O'Reilly's phrasing, your problem is not piracy, but obscurity
let me take a question from Micalet, and I will be back with you in a bit
Micalet: your turn! do you have a question?
Micalet
yes
I have a problem with alarmManager and notifications
7:35 PM
Micalet
Can I paste here some code?
Mark M.
sure, or if you prefer, paste it into some pastebin-style service and link to it here
this chat doesn't do any syntax highlighting or the like, as it's just a chat :-)
Micalet
yes, I know, but I don't have user in any service like pastebin
I'll try to paste it here
I'm using this code to start a service that search for changes in a rss feed and send a notifications if feed have changed
View paste (12 more lines)
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

Intent intent = new Intent(context, AlarmActivity.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);

Calendar data_actual = Calendar.getInstance();
data_actual.setTimeInMillis(System.currentTimeMillis());

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hora);
calendar.set(Calendar.MINUTE, minut);

Calendar calendariActual = Calendar.getInstance();
calendariActual.setTimeInMillis(System.currentTimeMillis());
...
Mark M.
your use of FLAG_CANCEL_CURRENT worries me, but, beyond that, what is your specific problem?
Micalet
I like to search for updates twice a day, so that I create a method whith this code
public void Alarma(int hora, int minut, long repeticio, int id, Context context){}
7:40 PM
Micalet
and set two alarms identicall with different ids
View paste
public void setAlarm(Context context) {
    	Alarma(10, 30, AlarmManager.INTERVAL_DAY, 1, context);
    	Alarma(17, 00, AlarmManager.INTERVAL_DAY, 2, context);}
I've tried with other flags, but the problem still is the same
Mark M.
and what is the problem?
Micalet
my problem is that only works one of the alarms, and doesn't fire at that times
Mark M.
it is not supposed to fire at those times
these are inexact alarms
for all you know, the two of them could be fired within milliseconds of each other
Micalet
yes, I know, but is it correct that fire at hours from specified time?
Mark M.
sure
Micalet
ok
Mark M.
they can be invoked anywhere within the 24 hour INTERVAL_DAY window, AFAIK
" while the overall period of the repeating alarm will be as requested, the time between any two successive firings of the alarm may vary"
I would recommend that you use one alarm with INTERVAL_HALF_DAY
Micalet
then, should I use alarmMgr.setRepeating if I want to fire at the specified times?
Mark M.
well, setRepeating() is also inexact, on Android 4.4+, if your targetSdkVersion is 19 or higher
Micalet
ok
Mark M.
on Android 4.4+, there is no exact repeating alarm
7:45 PM
Mark M.
other than one you roll yourself, using setWindow() or setExact() and doing the repeating part yourself
I would also recommend using FLAG_UPDATE_CURRENT, or even 0, instead of FLAG_CANCEL_CURRENT
Micalet
ok, I'll change that
Mark M.
in your case, since your underlying Intent extras are not changing (because you do not have any), 0 would work just fine
Micalet
ok
the other problem (I told you that I have two problems with that) is that the notifications doesn't appear in devices with android>4.x, and I'm not sure if >3.x
Mark M.
start by using Log calls to see whether your alarms are getting control
Micalet
i'm using this code to send the notifications:
View paste (24 more lines)
private void sendNotification(String msg) {
    	Log.d("Notificació", "Iniciada");
        mNotificationManager = (NotificationManager)
               this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, Promos_activity.class), PendingIntent.FLAG_UPDATE_CURRENT);//0);

        Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_notif)
        .setLargeIcon(largeIcon)
        .setContentTitle(getResources().getString(R.string.notifTitol))
        .setStyle(new NotificationCompat.BigTextStyle()
...
Mark M.
do you see your Log.d("Notificació", "Iniciada"); calls in LogCat?
Micalet
in my own device with android 2.3.7 is running correctly, but in newer devices don't
yes in my own device
in others I couldn't check it
7:50 PM
Mark M.
what happened when you tried an emulator?
Micalet
I still don't tried in an emulator.
Mark M.
then you may want to start there
Micalet
Apparently the code is ok?
Mark M.
so long as you have the VIBRATE permission, nothing leaps out at me as being a problem
in order to debug problems, you need environments that you can test and look at LogCat, which in many cases will have to be an emulator
here, you need to determine if your alarms are not working, or whether they are working and you are crashing somewhere in your Notification code, etc.
let me take another question from EGHDK, and I will be back with you later
EGHDK: your turn! do you have another question?
Micalet
ok
EGHDK
Is there a main timer that runs the phone? I'm guessing theres a master timer that ticks in the kernel or something, where all other time revolves around said timer?
Is that the SystemClock()?
Mark M.
there would be at least one, perhaps more, but that's way below the level of the Android SDK (and, hence, my areas of expertise)
EGHDK
Got it.
7:55 PM
EGHDK
im trying to learn more about lower level stuff in android. you said last time that stuff like startActivity is asynchonous (or at least it doesn't go off right away)... but it doesn't say that in the docs. I asked you before "Then how do you know". You said "experience" and mentioned some book. Do you remember the name of that book?
Mark M.
I'm not quite sure what book you're referring to
if you're referring to a book on Android internals, that may have been Karim Yaghmour's _Embedded Android_
EGHDK
I think you mentioned a book from oh Yeah. That's it
Sweet. Thanks.
I'm really curious about that and still trying to wrap my head around Handlers and stuff. What would you recommend for that?
Mark M.
I am not aware of any book that really gets into that part of the framework in a deep fashion
EGHDK
Okay. That's fine. Just curious. You can go back to Micalet
Mark M.
there probably is a book that does, but there are dozens of them, and I generally don't buy or read them to avoid accidental plagiarism
OK
Micalet: your turn! do you have another question?
EGHDK
So you just study AOSP and read the android docs?
Mark M.
(EGHDK: only when I need to, for specific problems, not as light reading before bedtime :-)
8:00 PM
Mark M.
Micalet: your turn! do you have another question?
Micalet
I'm trying with an emulator
Mark M.
well, with a half-day interval on your alarms, that might take a while :-)
Micalet
yes, of course :-D
Mark M.
OK, if either of you have a question, just ask
Micalet
I'll make some test with minor intervals
ok
Bill M.
has entered the room
Mark M.
howdy, Bill!
Bill M.
Hola.
:D
Mark M.
Bill: we're kind in free-for-all mode right now, so if you (or anyone) has a question, go right ahead
Bill M.
Any good chatter?
EGHDK
Or thsi question came out on reddit today. How do you know if the docs are updated? Someone actually mentioned you in the reddit post @Mark. haha
8:05 PM
Mark M.
yes, the documentation has substantial gaps
EGHDK
I think it's a valid question. Are the docs open source?
like can you see a git history?
Mark M.
they should be up as part of android.googlesource.com
I haven't really gone looking for the docs specifically, and the JavaDocs come from the Java code itself
EGHDK
Got it. Yeah. I guess I'm just trying to read the docs while I'm learning and it seems that I'd just want to know if theres some other resource people use (the sort of people that write books on android use)
Mark M.
this is a place where looking at the GitHub mirror may be easier, as GitHub's Web interface is much more capable than the Gitiles that Google is using
EGHDK
Cool. I'll keep digging.
8:10 PM
EGHDK
Github doesn't seem to have android/platform
I found the android project, but not the docs dir you linked to
Mark M.
EGHDK
Thanks mark. you always have got the answers.
Mark M.
not always
but I try
8:15 PM
Mark M.
again, if anyone has any questions, go right ahead and ask
EGHDK
If someone wanted to put a password entry screen for their application... how would they do it? Moving in and out of activities wouldn't require a password screen of course... so how would you accomplish this? Activity lifecycle overlaps... and application class can't tell if the user is in/or out of the app. How would you do it?
Mark M.
have some sort of session singleton in the process
that session knows when the user last authenticated
8:20 PM
Mark M.
and it knows the rules for when re-authentication is required
activities would check with the session in onCreate() and route things accordingly (e.g., redirect to another activity, show the authentication fragment themselves)
if the process is terminated (e.g., user puts app in the background for a while)
then we would re-authenticate on the next startup
as the session would be re-initialized
leastways, that's how I'd approach it
EGHDK
Session singleton in the process.... process = application class?
Mark M.
no, process = process
8:25 PM
EGHDK
Wait... deffinitely confused. So where or how would you create this session singleton?
Just as a regular class?
Mark M.
static data member
I mean, you could put it in a custom Application subclass, but I generally avoid that
EGHDK
Part of what class though?
Mark M.
its own class
EGHDK
Okay. It's own class got it.
Mark M.
call it EghdkCoolAppSession or something
EGHDK
Where would you instantiate it though?
In the Application class?
*subclass?
Mark M.
a singleton is usually lazy-instantiated via a static accessor method
EGHDK
Got it.
Once instantiated though. Since it's static... it will last as long as the process?
Mark M.
correct
EGHDK
Cool. So again. Lets say you have an activity, and you leave it, but in onStop() you create and start running an infinite loop in a thread for some reason.
Can that thread leak?
Or when the activity is killed will the threads that were created in that activity die as well
?
Mark M.
threads are pretty much always "leaked"
if you are the one who forks the thread, you have to clean it up
if *Android* forks the thread (AsyncTask, IntentService, etc.), then *Android* cleans it up, but only in those cases
8:30 PM
EGHDK
Gotcha.
Mark M.
and that's a wrap for today's chat
EGHDK
Cool. Okay. thanks for the help mark. helpful as always
Mark M.
I will post the transcript soonish to https://commonsware.com/office-hours
the next chat is Tuesday at 9am US Eastern
have a pleasant day!@
EGHDK
has left the room
Micalet
has left the room
Bill M.
has left the room
Mark M.
turned off guest access

Tuesday, January 6

 

Office Hours

People in this transcript

  • Bill Mote
  • EGHDK
  • Mark Murphy
  • Micalet