Monday, March 31, 2014

Step Detector and Step Counter Sensors on Android

Android KitKat has added a few more hardware sensors to it's API list. Step Sensors are one of them, which looks very promising. Although, not a lot of phones yet have these Step Sensors, in the future, this would gradually become a standard I think. Currently, Nexus 5 has them.

Let's see how we can interact with these sensors. Basically, there are 2 sensors.
  1. Step Counter: This keeps a count of the number of steps that you have taken. The counter is only reset when you re-boot the device, else, for every step you take (or the phone thinks you took, you counts up).
  2. Step Detector: This sensor just detects when you take a step. That's it. 
The example project shows you how to initialize and setup the SensorManager and respond to events from the Sensors.
// Step Counter
sManager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float steps = event.values[0];
textViewStepCounter.setText((int) steps + "");
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, sManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER),
SensorManager.SENSOR_DELAY_UI);

// Step Detector
sManager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
// Time is in nanoseconds, convert to millis
timestamp = event.timestamp / 1000000;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, sManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR),
SensorManager.SENSOR_DELAY_UI); 

No special permissions are required.

Head over to Github to get the full source code.

Saturday, December 7, 2013

Smartphone Usage Statistics Highlight Its Importance In Our Lives

Smartphone Usage Statistics Highlight Its Importance In Our Lives

This Infographic is produced by Coupon Audit (provides Finish Line promo code) and Techdroid.Kbeanie

If you would like to add this inforgraphic on your website or blog, copy and paste the below code as given in the textbox.

Friday, September 27, 2013

Making your Android apps crash-proof

Image courtesy: Crittercism Website
Do you have an android app on the Play Store, and get lots of negative comments/feedback?


Most of the time, apps crash on the first interaction with the user. And that's the reason most users are frustrated of even downloading your app, and that's when most of the negative comments start pouring in.

Of course, you have to look for crashes everywhere in your app, but crashing on the first page itself, is a really really bad situation. When that happens, and the user leaves a bad comment, instead of sending you a support mail, you have no idea how to fix it. You have no idea about the user's device, and other important information that would help you in determining the problem, and fixing it. And if you continue having such a build even for a week on the store, that puts a dent on your overall app reviews. People generally look at the first few reviews, and make a decision based on that, whether to try your app or not.

I have had an app on the store for years now, which I didn't update for a long time. And finally, when I got the time to work on it again, I decided to integrate Crittercism SDK within my app, along with Google Analytics SDK.

The very first update with +Crittercism along with +Google Analytics , my inbox was flooded with crash reports. I was amazed at how many un-reported crashes my app had. I was living in the dark, as to what all problems users were having with my app. Crittercism logs had more information about the crashes than I could have asked for. They were pretty detailed, in terms of the user's device name, and OS versions, the version of the application, and a device snapshot (Memory/Storage) when the crash occurred.

After about a week, I made the next update, and this time, the crash notifications reduced dramatically, although the number of users and the average usage of my app went up. From this, I can guess that the users who are still using my app are a happy lot. And that makes me happy as well.

I would strongly recommend integrating such a live error reporting tool into your apps to get live feedback for your apps. Specifically for Crittercism, there are free and paid plans. Currently, I am on the free plan, and it gives me the almost everything that I need to know to fix those random crashes. With paid plans, you have more reporting, which I think I don't need right away. Receiving crash notifications is just the one part of Crittercism. They call this solution as a "Mobile Application Performance Management" solution. Rightly so?

Have you used Crittercism in your apps? Do you have anything to say?

Tuesday, April 9, 2013

Easy Video Chooser Library for Android

After I wrote the "Easy Image Chooser library for Android", I thought I could easily extend this library to handle other things as well. For now, I have implemented the option of choosing/recording a video. And in the future, I am planning to extend this library for choosing other types of files.

Choosing videos, is again very similar to how the images are handled. Using this library, you would be also able to choose videos that reside in your Picasa albums on your phone. And this should work for all OSes and devices.

After processing, the library would return a ChosenVideo object, which would contain the original path to the video file, and 2 differently-sized thumbnails which you can directly use as a preview.

Here is how to use this library.

  • For capturing a video using the camera
videoChooserManager = new VideoChooserManager(this, ChooserType.REQUEST_CAPTURE_VIDEO);
videoChooserManager.setVideoChooserListener(this);
videoChooserManager.choose();
  •  For choosing a video from the gallery
videoChooserManager = new VideoChooserManager(this, ChooserType.REQUEST_PICK_VIDEO);
videoChooserManager.setVideoChooserListener(this);
videoChooserManager.choose();
  • On Activity Result, write this
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK &&
(requestCode == ChooserType.REQUEST_PICK_VIDEO ||
requestCode == ChooserType.REQUEST_CAPTURE_VIDEO)) {
videoChooserManager.submit(requestCode, data);
}
@Override
public void onVideoChosen(final ChosenVideo video) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (video != null) {
// Use the video
// video.getFilePathOriginal();
// video.getFileThumbnail();
// video.getFileThumbnailSmall();
}
}
});
}
@Override
public void onError(final String reason) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// Show error message
}
});
}
Here's the link to the Github project. Do let me know your thoughts and feedbacks, and also any bugs if you come across. I will try to fix them as soon as possible.

Saturday, March 30, 2013

Easy Image Chooser Library for Android

In almost all the Android apps that I have worked on, there has been a requirement for choosing an image or taking a snap and using the device's camera.

Taking a snap, is rather straightforward to implement, but choosing an image from your gallery is sometimes a hard nut to crack. And to implement this correctly, you would always end up writing a lot of code. Assuming that you want to target all OSes, devices, folders etc.

For example, choosing a picture from the Camera folder of your phone is nice and easy. But, if you want to have the user chose an image from one of his picasa web albums, which he has synced on his phone, you will find a dead-end. Well, almost.

For myself, I must have at-least rewritten the same code, multiple number of times. Of course, the platform should help us in achieving this seemingly straightforward feature with as less code as possible. But, right now, it's not possible.

So, I thought of creating a library which anyone could integrate within one's own app, without really worrying about the nitty-gritties and the bugs, to implement or add this feature into his app. And there's more. Most of the time, you would need a scaled down version of the chosen image to show a preview or use it in a listview. This library would give you 3 sizes, as of now.
  1. Original Size
  2. Thumbnail Size
  3. Thumbnail Smaller Size
The sizes are dynamically calculated, based on the original size of the image. It's pretty rough here, but I am looking forward to improve that part.

This version of the library is pretty basic. But, I would be working on improving this library to add more functionality in the future.

You can find more information about this library here.

By using this library in the current state, you could handle all these cases/special cases with just a few lines of code. An example of this is shown below.

Usage:

1. For choosing an image from gallery
imageChooserManager = new ImageChooserManager(this, ChooserType.REQUEST_PICK_PICTURE);
imageChooserManager.setImageChooserListener(this);
imageChooserManager.choose();
2. For capturing a picture using your camera
imageChooserManager = new ImageChooserManager(this, ChooserType.REQUEST_CAPTURE_PICTURE);
imageChooserManager.setImageChooserListener(this);
imageChooserManager.choose();

3. On Activity result, do this:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK &&
(requestCode == ChooserType.
REQUEST_PICK_PICTURE||
requestCode == ChooserType.
REQUEST_CAPTURE_PICTURE)) {
imageChooserManager.submit(requestCode, data);
}
}
4. Implement the ImageChooserListener interface and override these methods:
@Override
public void onImageChosen(final ChosenImage image) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (image != null) {
// Use the image
// image.getFilePathOriginal();
// image.getFileThumbnail();
// image.getFileThumbnailSmall();
}
}
});
}
@Override
public void onError(final String reason) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// Show error message
}
});
}
That's all you need to code. And the library takes care of handling all kinds of images, and also generates 2 thumbnails which you could directly use. Let me know if you need to add any new features or if you find a bug. I will try to address those as soon as humanly possible. If you would like to contribute to this project, drop me a mail.