This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ColorUtils { | |
private static final double LM_RED_COEFFICIENT = 0.2126; | |
private static final double LM_GREEN_COEFFICIENT = 0.7152; | |
private static final double LM_BLUE_COEFFICIENT = 0.0722; | |
public static int calculateRelativeLuminance(int color) { | |
int red = (int) (Color.red(color) * LM_RED_COEFFICIENT); | |
int green = (int) (Color.green(color) * LM_GREEN_COEFFICIENT); | |
int blue = (int) (Color.blue(color) * LM_BLUE_COEFFICIENT); | |
return red + green + blue; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CONNMGR_CONNECTION_DETAILED_STATUS *pConnMgrDet; | |
HRESULT hResult; | |
DWORD dwBufferSize=0;/ | |
hResult=ConnMgrQueryDetailedStatus(pConnMgrDet,&dwBufferSize); | |
if(hResult==(HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER))) | |
{ | |
pConnMgrDet=(CONNMGR_CONNECTION_DETAILED_STATUS *)new BYTE[dwBufferSize]; | |
hResult=ConnMgrQueryDetailedStatus(pConnMgrDet,&dwBufferSize); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Latency Comparison Numbers | |
-------------------------- | |
L1 cache reference 0.5 ns | |
Branch mispredict 5 ns | |
L2 cache reference 7 ns 14x L1 cache | |
Mutex lock/unlock 25 ns | |
Main memory reference 100 ns 20x L2 cache, 200x L1 cache | |
Compress 1K bytes with Zippy 3,000 ns | |
Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms | |
Read 4K randomly from SSD* 150,000 ns 0.15 ms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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. | |
* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
NewerOlder