Thursday, November 22, 2012

How to get device MAC ID in ANDROID

--------------------------------------------------------------------------
Using WifiManager get wifi mac id in android.
--------------------------------------------------------------------------

wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
macaddress = wifiManager.getConnectionInfo().getMacAddress();
Log.i("MACID in loading...", "" + macaddress);

---------------------------------------------------------------------


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Using java.Utils package we can get MAC ID of device...

This is a code available in an Utils pkg.
We can add only this following class in our project...

---------------------------------------------------------------------

import java.io.*;
import java.net.*;
import java.util.*;
import org.apache.http.conn.util.InetAddressUtils;

public class RanjitUtils {

 /**
* Returns MAC address of the given interface name.
*
* @param interfaceName
*            eth0, wlan0 or NULL=use first interface
* @return mac address or empty string
*/
public static String getMACAddress(String interfaceName) {
try {
List<NetworkInterface> interfaces = Collections
.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface intf : interfaces) {
if (interfaceName != null) {
if (!intf.getName().equalsIgnoreCase(interfaceName))
continue;
}
byte[] mac = intf.getHardwareAddress();
if (mac == null)
return "";
StringBuilder buf = new StringBuilder();
for (int idx = 0; idx < mac.length; idx++)
buf.append(String.format("%02X:", mac[idx]));
if (buf.length() > 0)
buf.deleteCharAt(buf.length() - 1);
return buf.toString();
}
} catch (Exception ex) {
} // for now eat exceptions
return "";
/*
* try { // this is so Linux hack return
* loadFileAsString("/sys/class/net/" +interfaceName +
* "/address").toUpperCase().trim(); } catch (IOException ex) { return
* null; }
*/
}
}
----------------------------------------------
access getMACADDRES(String) static method
----------------------------------------------
String macid_eth = RanjitUtils.getMACAddress("eth0");
or
String macid_wlan = RanjitUtils.getMACAddress("wlan0");

Wednesday, November 21, 2012

How to find distance in km between two lat long in ANDROID


package ranjit.static_var_fun;

public class RanjitStaticDataNFun {

public static float distFrom(float lat1, float lng1, float lat2, float lng2) {
                 // This is earth radius
double earthRadius = 3958.75;

double dLat = Math.toRadians(lat2 - lat1);
double dLng = Math.toRadians(lng2 - lng1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2)
* Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double dist = earthRadius * c;

int meterConversion = 1609;
float dist_in_km = (new Float(dist * meterConversion).floatValue()) / 1000;
                //distance in an KM
return dist_in_km;
}

}

Sunday, November 18, 2012

How to get correct position of list item when custom list button clicked


use setTag attribute of the View..............

use this button code in getView() method
Button call = (Button) rowView.findViewById(R.id.button1);
call.setTag(position);

and after that directly use onClick listner for that listview item button
call.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
         int which = -1;
         Obejct obj =  v.getTag();
         if(obj instaceof Integer){
         which  = ((Integer)obj).intValue();
         }

      if(which >-1){
      //write code here to get value from any way using position means which
      Toast.makeText(getContext(), "Current clicked position is : "+which, 1).show();
   }
 }
});

Reference from: http://stackoverflow.com/questions/11156078/android-get-the-listview-item-from-button-clicked-in-custom-listview

Wednesday, November 14, 2012

How to get windows width and height in ANDROID


private static Point getDisplaySize(final Display display) {
    final Point point = new Point();
    try {
        display.getSize(point);
    } catch (java.lang.NoSuchMethodError ignore) { // Older device
        point.x = display.getWidth();
        point.y = display.getHeight();
    }
    return point;
}
Its helpful, this code we can use for older and newer devices.
I used in my circular list project.
From this page:
http://stackoverflow.com/questions/9654016/getsize-giving-me-errors

How to create Circular List in Android

I developed this circular list for Android.
Related help already available on web.

Logic for circular list.

1. Create circular list using Table Layout.
2. Add dynamic row with row items.
3. get x y coordinates of dynamic row and this coordinates compare with screen window position. If row coordinates and targeted screen coordinates match then set left margin you have to need.

|@
|->@
|--->@
|----->@
|------>@
|------->@
|------->@
|------->@
|------>@
|----->@
|---->@
|-->@
|@

Above diagram shown my logic...

I developed circular list this is shown in above image..

If you want help regarding this.
Mail me: ranjitvcsc@gmail.com
----------------------------------------------------
Thank you-Ranjit
---------------------------------------------------- 

YouTube Player - Stack Overflow Help

Remember:
I have managed to fix it, there was just need to add a signature to the link in the VideoStream file of this library and everything is working like charm!
VideoStream.java (Line: 30)
before: mUrl = lArgMap.get("url");
after:  mUrl = lArgMap.get("url") + "&signature=" + lArgMap.get("sig");

Saturday, November 10, 2012

Supporting Different Screen Sizes

http://developer.android.com/training/multiscreen/screensizes.html

try this...
Its very helpfull to adjust UI when UI changes...

get view location on screen


Using view.getLocationOnScreen(loc);
we can easily get location of component.

Best tutorial check it given link...
http://android-er.blogspot.in/2012/07/error-of-getlocationinwindow-and.html

Thursday, November 8, 2012

How to play youtube video in Android using media player and video view

With the help of media player and video view we can play youtube video in android.

first get "RTSP" address for youtube video. Then play youtube video using this address in Android.

Tutorial for "how to get "RTSP" address for youtube videos."

Note: Youtube video not playing in emulator. Its perfectly working in device.