Tuesday, September 27, 2011

How to take a screenshot with the Android SDK

You can take a screenshot in Android using one of the several Market's application, or using "ddms", a tool you can find in the tools directory of the sdk.

So, go to that directory and start it:

cd [sdkdir]/tools
./ddms


Then go on the Device menu ant click on Screen capture. Then save the image file.






If you want to take a screenshot from Eclipse, it is even simpler.
Just set the DDMS perspective:
Look for the camera button, and click on it to show the following menu:




Here you can find the "Screen Capture" command.

Monday, September 19, 2011

How to check if our app is running on an iPad or iPhon

The easiest way is using the UI_USER_INTERFACE_IDIOM macro. Only SDKs 3.2 an later support it. Here an example:

if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) 
{
  NSLog(@"I'm on iPad");
}

In the same way:

if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone) {
  NSLog(@"I'm on iPhone or on an iPod touch"); 
}

The macro uses the [[UIDevice currentDevice] userInterfaceIdiom] method. If you are sure that your SDK supports the userInterfaceIdiom selector, you can also use something like this:

[[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPad

Thursday, September 8, 2011

Android's services

Android uses services to perform long running jobs, or background tasks (think to a network interaction or an mp3 player). How can we start a service?

We should begin extending the android.app.Service class:


public class TestService extends Service {

     @Override

     public IBinder onBind(Intent arg0) {

          return null;

     }



     public void onCreate () {

       Log.i("onCreate", "onCreate");   

     }

    
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {

           Log.i("onStartCommand", "onStartCommand");  

           Toast.makeText(this, "Hi, this is a service!",
                           Toast.LENGTH_LONG).show();

           return START_STICKY;

      }

}

The two most important methods, for the purposes of this post, are onCreate and onStartCommand. Android invokes the first, when the service is created, and the second one, when it is executed. The Toast.makeText(…) command is a nice way to give notifications to the users.

Please note that the onStartCommand callback exists only in the versions 2.0 and later. In older versions you should override the onStart method (now deprecated).

Once you have you service’s skeleton, you can register with the operating system, adding a line to the manifest:

 <service android:name="TestService" android:enabled="true" >service>


Or in a easier way, using the Eclipse interface:

At the end is time to start our service, using two instructions:

Intent intent = new Intent(this, TestService.class);

     startService(intent);

We create an android.content.Intent instance, and then we use it in the startService method. The Intent, ask you for a context (your calling Activity is fine), and the implementation class. He will take care of the instantiation. The startService will call your onStartCommand procedure, passing it also the intent instance. A nice place to do it could be in the onClick method:


     public void onClick(View v) {

          switch (v.getId()) {

          case R.id.buttonStart:

               Intent intent = new Intent(this, TestService.class);

               startService(intent);

               break;

          }

     }

Friday, September 2, 2011

How to take screenshots directly on the iPhones

If you want to take a screen-shot directly from your iPhone/iPod touch/iPad, and find it directly in your photo library, there is a simple procedure:
  1. Press the Home button, without releasing it;
  2. Press and release the Sleep button;
  3. Release the Home button.
That's all! Now go in your camera roll, and enjoy the image of screen. Some more thing on screen-shots here!
The (so called) Home and Sleep buttons are marked in red in the picture.