Tuesday, 19 March 2013

Handler Example in android Or Calling the method according to time interval Or Calling method every 1 minute...

private static int DATA_INTERVAL = 60 *1000;

//Creating an Object of Handler
  Handler _handler;
_handler = new Handler();

 //Used for removing call back .(the method we have register using postDelayed used to stop.).
 _handler.removeCallbacks(getData);


//Used for calling method according to time.
 _handler.postDelayed(getData, DATA_INTERVAL);


//Method which will be called accroding to time
private final Runnable getData = new Runnable() {
        public void run() {
            getTimeCalculate();
        }
    };


//Method which will be called after 1 Minute
private void getTimeCalculate() {
Log.d("Hi I am called","Please check the log")   

}


Thanks Enjoy all who is facing this type of issue,if you like the post or dont like leave your comment,so we will try to give our best post which will be helpful for android developers.

Wednesday, 13 March 2013

Setting size of the image view using bitmap

Bitmap bitmapOrg;
                   try{
                    
                         URL centreImageURL = new URL(hotelUrl);
                         URLConnection conn = centreImageURL.openConnection();
                         conn.connect();
                         InputStream is = conn.getInputStream();
                         BufferedInputStream bis = new BufferedInputStream(is);
                 
              bitmapOrg = BitmapFactory.decodeStream(is);
             }
             catch (Exception e) {
                  bitmapOrg = BitmapFactory.decodeResource(getResources(),
                                       R.drawable.hotel);
            }
                int width = bitmapOrg.getWidth();
                int height = bitmapOrg.getHeight();
                int newWidth = 60;
                int newHeight = 60;

                // calculate the scale - in this case = 0.4f
                float scaleWidth = ((float) newWidth) / width;
                float scaleHeight = ((float) newHeight) / height;

                // create a matrix for the manipulation
                Matrix matrix = new Matrix();
                // resize the bit map
                matrix.postScale(scaleWidth, scaleHeight);
                // rotate the Bitmap
                matrix.postRotate(0);

                // recreate the new Bitmap
                Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                                  width, height, matrix, true);

                // make a Drawable from Bitmap to allow to set the BitMap
                // to the ImageView, ImageButton or what ever
                BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
            ImageView hotelImage=new ImageView(this);
            hotelImage.setImageDrawable(bmd);

Android Notification Manager

// notification is selected
        Intent intent = new Intent(this, HotelListTable.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

        // Build notification
        // Actions are just fake
        Notification noti = new Notification.Builder(this)
            .setContentTitle("New mail from " + "test@gmail.com")
            .setContentText("Subject").setSmallIcon(R.drawable.hotel)
            .setContentIntent(pIntent)
            .addAction(R.drawable.hotel, "Call", pIntent)
            .addAction(R.drawable.hotel, "More", pIntent)
            .addAction(R.drawable.hotel, "And more", pIntent).build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // Hide the notification after its selected
        noti.flags |= Notification.FLAG_AUTO_CANCEL;

        notificationManager.notify(0, noti);

Tuesday, 12 March 2013

Get the phone's last known location using LocationManager

This is a fast code to get the last known location of the phone. If there is no exact gps-information it falls back to the network-based location info. This code is using LocationManager. [updated]
 
 
private double[] getGPS() {
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
        List<String> providers = lm.getProviders(true);

        /* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
        Location l = null;
        
        for (int i=providers.size()-1; i>=0; i--) {
                l = lm.getLastKnownLocation(providers.get(i));
                if (l != null) break;
        }
        
        double[] gps = new double[2];
        if (l != null) {
                gps[0] = l.getLatitude();
                gps[1] = l.getLongitude();
        }
        return gps;
}