Skip to content

Instantly share code, notes, and snippets.

@jfsurban
jfsurban / why
Created September 7, 2012 04:00
why the lucky stiff re:Invitation for an Interview for Brazilian Blog
// http://www.akitaonrails.com/2012/09/03/why-documentary-at-rubyconf-2012-denver
Date: Thu, 19 Apr 2007 17:38:59 -0500
From: why the lucky stiff <[email protected]>
To: [email protected]
Subject: SPAM: Re: Invitation for an Interview for Brazilian Blog
On Wed, Apr 18, 2007 at 10:28:25AM -0300, Fabio Akita wrote:
> First of all let me introduce myself, my name is Fabio Akita, I've written
> the first brazilian Rails book called "Repensando a Web com Rails". I have a
@jfsurban
jfsurban / why_style
Created September 7, 2012 04:02
why programming style
// http://www.smashingmagazine.com/2010/05/15/why-a-tale-of-a-post-modern-genius/
I do not write tests for my code. I do not write very many comments. I
change styles very frequently. And most of all, I shun the predominant
styles of coding, because that would go against the very essence of
experimentation. In short: all I do is muck around.
So, my way of measuring a great programmer is different from some
prevailing thought on the subject. I would like to hear what Matz
would say about this. You should ask him, seriously.

Sublime Text 2 – Useful Shortcuts (Windows)

General

Ctrl+KB toggle side bar
Ctrl+Shift+P command prompt
Ctrl+` python console
Ctrl+N new file

Editing

int dipValue = 200; // we whant to convert this dip value to pix
Resources r = getResources();
float pix = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, r.getDisplayMetrics());
package com.company.app.utils;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
@jfsurban
jfsurban / intent_available.java
Last active December 17, 2015 22:39
Indicates whether the specified action can be used as an intent - #android http://www.curious-creature.org/2008/12/15/android-can-i-use-this-intent/
/**
* Indicates whether the specified action can be used as an intent. This
* method queries the package manager for installed packages that can
* respond to an intent with the specified action. If no suitable package is
* found, this method returns false.
*
* @param context The application's environment.
* @param action The Intent action to check for availability.
*
@jfsurban
jfsurban / android tips
Created June 10, 2013 03:21
Android tips & tricks
- do not use null on LayoutInflater.inflate
- use FrameLayout or LinearLayout instead of RelativeLayout as the latter takes at least 2 passes on layout
- use ViewHolder pattern
@jfsurban
jfsurban / check_running_service.java
Created June 28, 2013 01:49
Check if a Service is running in Android - http://stackoverflow.com/a/5921190/718
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (MyService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
If your service is going to be part of you application then you are making it way more complex than it needs to be. Since you have a simple use case of getting some data from a RESTful Web Service, you should look into ResultReceiver and IntentService.
This Service + ResultReceiver pattern works by starting or binding to the service with startService() when you want to do some action. You can specify the operation to perform and pass in your ResultReceiver (the activity) through the extras in the Intent.
In the service you implement onHandleIntent to do the operation that is specified in the Intent. When the operation is completed you use the passed in ResultReceiver to send a message back to the Activity at which point onReceiveResult will be called.
So for example, you want to pull some data from your Web Service.
You create the intent and call startService.
The operation in the service starts and it sends the activity a message saying it started
@jfsurban
jfsurban / AsyncTask_NonUI_Fragment.txt
Created July 10, 2013 10:49
Android - Background worker using non ui Fragment + AsyncTask - http://stackoverflow.com/a/12303649/718
I think you will enjoy my extremely comprehensive and working example!
Rotation works, and the dialog survives.
You can cancel the task and dialog by pressing the back button (if you want this behaviour).
It uses fragments.
The layout of the fragment underneath the activity changes properly when the device rotates.
There is a complete source code download and a precompiled APK so you can see if the behaviour is what you want.
Edit
As requested by Brad Larson I have reproduced most of the linked solution below. Also since I posted it I have been pointed to AsyncTaskLoader. I'm not sure it is totally applicable to the same problems, but you should check it out anyway.