Tuesday, May 29, 2012

How to convert and Java project to Android Library project ?

Today, I had to add  reference to a Java project. So I right clicked on the project and checked Properties but there was no "Android" in the treeview in the properties. How to fix this ?

1. Right click and close the project.
2. Open the .project file in notepad.
3. Search for <natures> </natures> tag and change to

<natures>
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
4. Save the file and open again in Eclipse.
5. Now you need to create "project.properties" and "AndroidManifest.xml" manually. (I just copied these two files from another project :D)
6. Right click on the project -> Android Tools -> Fix project properties.

After that when I right click and go to properties I can see "Android" in the listview :)



Friday, May 25, 2012

How to change screen timeout to never programmatically?



Settings.System.putString(cr,                     Settings.System.SCREEN_OFF_TIMEOUT, "-1");

Wednesday, May 23, 2012

How to capture sound from microphone and play it on speakers in c#

I have been using NAudio for one of our applications to record sound. Today, I wanted to play back the live recording over the speakers while doing the live record. After few mins of Googling, hammering and fixing. I managed to get something working.


private IWavePlayer waveOut;
private BufferedWaveProvider waveProvider;
private WaveIn m_WaveIn;


private void StartRecording() {
       // Setup Incoming
m_WaveIn = new WaveIn();
m_WaveIn.BufferMilliseconds = 50; // This is very very important.
m_WaveIn.DeviceNumber = set your device here;
m_WaveIn.DataAvailable += WaveIn_DataAvailable;
m_WaveIn.WaveFormat = m_waveFormat;
m_WaveIn.StartRecording();

       // Setup Output


waveOut = new WaveOut();
waveProvider = new BufferedWaveProvider(m_waveFormat);
waveOut.Init(waveProvider);
waveOut.Play();


}


public void WaveIn_DataAvailable(object sender, WaveInEventArgs e) {
    byte[] buffer = e.Buffer;
    waveProvider.AddSamples(buffer, 0, buffer.Length);
}

Thats it. This works like a charm. Hope this will be useful for someone else as well.


Wednesday, May 9, 2012

Your own CountDownTimer in Android


Android CountDownTimer sucks. Why ? because in CountDownTimer.java (Android source code) it creates a mHandler inside the code which requires to call Looper.prepare() and Looper.loop() before calling new CountDownTimer(), You are likely to get Can't create handler inside thread that has not called Looper.prepare() because of this.

  So I wrote my own version of  CountDownTimer using Thread.

Here is it.


abstract class CountDownTimer extends Thread {
    public abstract void onFinish();
    private boolean mCancelled = false;
 
    public void run() {
    mCancelled = false;
    start(60);
    }
 
    private final void start(long secs) {
    long delay = secs * 1000;
do {
delay = delay - 1000;
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
if(mCancelled) {
return;
}
} while (delay > 0);
onFinish();
    }
        public final void cancel() {
    mCancelled = true;
    }
}

How to use
=========


CountDownTimer countDownTimer = new CountDownTimer()  {
@Override
public void onFinish() {

}
};

countDownTimer.start();




Thursday, April 26, 2012

getElementsByTagName returns null in Android 4.0 or 3.0

Today, my application crashed on Android 4 device. I always thought it will work on Android 4 because it was a simple application but I guessed wrong.

For some reason  getElementsByTagName  returns null in Android 4.0 or Android 3.0, if your  code look somewhat like this.


 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 DocumentBuilder builder = factory.newDocumentBuilder();
 InputSource is = new InputSource(new StringReader("Some XML here and there"));
 Document document = builder.parse(is);
 Element root = document.getDocumentElement();
 root.normalize();
           
 NodeList items = root.getElementsByTagName("tag");  <-- this function will always return null on Android 4 


When i dig in I found a bug reported in Google forums. I found this very hard to believe because this is a very primary level issue and how did it get passed?

Anyway, As a work around I had to use XPath. Just remember to change the project's Android SDK to 2.2 or higher otherwise XPath classes are not visible. They resides in javax.xml.xpath namespace. Or you can use XmlPullParser which reads documents from top to bottom.


InputSource is = new InputSource(new StringReader("Some XML here and there"));
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
NodeList nodeList = (NodeList) xPath.evaluate("/xpath", is, XPathConstants.NODESET);




Sunday, March 11, 2012

Android requires compiler compliance level 5.0. Please fix project properties

Today, I moved a different work space from my old work space, but I imported some of the projects from my previous project and I started to get this error out of no where. I figure out it's something to do with the Eclipse compiler issue. To fix this you need to change globe flag which is located here

Window - > Preferences -> Java -> Compiler - > Change JDK compiler complience level to 1.5 or greater. (I use 1.6)

Tuesday, February 7, 2012

Wi-Fi Keep Alive / On for Kindle Fire

I have uploaded my first app to Amazon App Store, Check it out here

Wi-Fi Keep Alive


It is a simple for kindle fire to keep the wi-fi connection always on even the device screen goes off. This might use some extra battery power to keep the Wi Fi up but it wont disconnect your streaming music :)