Today, I ran into a problem where my content observer is not getting notify when I delete something. After hours of searching I realized, sometime you need to set the
notifyForDescendents parameter as true. Weird but it worked!
Eg.
registerContentObserver(uri, true, observer);
Monday, August 1, 2011
Android ContentObserver not getting notify ?
How to use ACTION_TIME_TICK broadcast ?
Today, I was looking into how to track change notification to the camera folder in Android. While Googling for this, I came across ACTION_TIME_TICK broadcast event.
Android Doc says
public static final String ACTION_TIME_TICK
The current time has changed. Sent every minute. You can not receive this through components declared in manifests, only by exlicitly registering for it with Context.registerReceiver().
Sometimes we use a backgroud timer in our application to check for something in a continues loop but I think listening to this event is much better approach, but you have to make sure it's thread safe.
How to use.
@Override
public void onCreate(){
super.onCreate();
registerReceiver(
new ThikReceiver(),
new IntentFilter(Intent.ACTION_TIME_TICK));
}
}
public class ThikReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0)
// Do something
else
// Do something else
}
}
Android Doc says
public static final String ACTION_TIME_TICK
The current time has changed. Sent every minute. You can not receive this through components declared in manifests, only by exlicitly registering for it with Context.registerReceiver().
Sometimes we use a backgroud timer in our application to check for something in a continues loop but I think listening to this event is much better approach, but you have to make sure it's thread safe.
How to use.
@Override
public void onCreate(){
super.onCreate();
registerReceiver(
new ThikReceiver(),
new IntentFilter(Intent.ACTION_TIME_TICK));
}
}
public class ThikReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0)
// Do something
else
// Do something else
}
}
Friday, July 29, 2011
How to create a thread in android without hurting the UI
Sometimes when we want to do something out of the ordinary like pushing something to the server while user is playing a game or checking emails we create Threads. If you do not set the thread priority to low this might hurt the UI performance.
Correct way to do is to call setPriority in the thread class.
SomeTaskThread someTaskThread = new SomeTaskThread();
someTaskThread.setPriority(Thread.NORM_PRIORITY-1); //Make the background thread low priority. This way it will not affect the UI performance
someTaskThread.start();
or you can use AsyncTask class.
Correct way to do is to call setPriority in the thread class.
SomeTaskThread someTaskThread = new SomeTaskThread();
someTaskThread.setPriority(Thread.NORM_PRIORITY-1); //Make the background thread low priority. This way it will not affect the UI performance
someTaskThread.start();
or you can use AsyncTask class.
Monday, July 18, 2011
Unable to find instrumentation target package ? duh!
Today I was trying to write some test cases after i got inspired by this link
Problem was I was keep getting "Unable to find instrumentation target package" Error and not sure how to fix it. Well there are few ways to fix this problem. Easy way out is:
1.Double click on the AndroidManifest.xml
Delete any "instrumentation" tags
2.Goto Instrumentation Tab
3.Click Add
4.Select Instrumentation
5. Select the name and target package.
This should work. Trying to add the Instrumentation manually is a nightmare.
Hope this will help to someone else
Problem was I was keep getting "Unable to find instrumentation target package" Error and not sure how to fix it. Well there are few ways to fix this problem. Easy way out is:
1.Double click on the AndroidManifest.xml
Delete any "instrumentation" tags
2.Goto Instrumentation Tab
3.Click Add
4.Select Instrumentation
5. Select the name and target package.
This should work. Trying to add the Instrumentation manually is a nightmare.
Hope this will help to someone else
Sunday, July 3, 2011
How do I investigate an Application Not Responding (ANR) in Android?
Today I got an ANR error and it's really annoying because you know you have done something wrong and not sure where to look for to find out the problem.
When I googled, I found this on StackOverFlow.
"An ANR happens when some long operation takes place in the "main" thread. This is the event loop thread, and if it is busy, Android cannot process any further GUI events in the application, and thus throws up an ANR dialog." - From Stackoverflow
so, how to diagnose the problem, it's easy,
adb shell cat /data/anr/traces.txt
will drump all the appplication not responding traces to the command prompt. (Make sure to change the command prompt buffer size otherwise you can not see the full log out)
When I googled, I found this on StackOverFlow.
"An ANR happens when some long operation takes place in the "main" thread. This is the event loop thread, and if it is busy, Android cannot process any further GUI events in the application, and thus throws up an ANR dialog." - From Stackoverflow
so, how to diagnose the problem, it's easy,
adb shell cat /data/anr/traces.txt
will drump all the appplication not responding traces to the command prompt. (Make sure to change the command prompt buffer size otherwise you can not see the full log out)
Monday, June 20, 2011
How get PDUs from SMS in Android
Today, I wanted to convert the "pdus" bytes back to hex, this is how to do it.
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object[] aPDUArray = (Object[]) bundle.get("pdus");
String pduString = toHexString((byte[]) aPDUArray[0]);
}
private final static char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' };
public static String toHexString(byte[] array) {
int length = array.length;
char[] buf = new char[length * 2];
int bufIndex = 0;
for (int i = 0 ; i < length; i++)
{
byte b = array[i];
buf[bufIndex++] = HEX_DIGITS[(b >>> 4) & 0x0F];
buf[bufIndex++] = HEX_DIGITS[b & 0x0F];
}
return new String(buf);
}
Friday, June 10, 2011
isProcessRunning in Android - The hardway
public static boolean isProcessRunning(String paramString)
throws Exception
{
int i = 0;
Process localProcess = Runtime.getRuntime().exec("ps");
InputStream localInputStream = localProcess.getInputStream();
InputStreamReader localInputStreamReader = new InputStreamReader(localInputStream);
BufferedReader localBufferedReader = new BufferedReader(localInputStreamReader);
String str = localBufferedReader.readLine();
localBufferedReader.close();
localProcess.waitFor();
if (!str.contains(paramString))
break;
}
}
throws Exception
{
int i = 0;
Process localProcess = Runtime.getRuntime().exec("ps");
InputStream localInputStream = localProcess.getInputStream();
InputStreamReader localInputStreamReader = new InputStreamReader(localInputStream);
BufferedReader localBufferedReader = new BufferedReader(localInputStreamReader);
String str = localBufferedReader.readLine();
localBufferedReader.close();
localProcess.waitFor();
if (!str.contains(paramString))
break;
}
}
Subscribe to:
Posts (Atom)