Pages

Wednesday, 2 April 2014

Android google map v2 using mapView

Following url have nice sample for this
https://gist.github.com/joshdholtz/4522551#file-some_layout-xml

--------------------------------------
Example

public class SomeFragment extends Fragment {
 
 MapView mapView;
 GoogleMap map;
 
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View v = inflater.inflate(R.layout.some_layout, container, false);
 
   // Gets the MapView from the XML layout and creates it
  mapView = (MapView) v.findViewById(R.id.mapview);
  mapView.onCreate(savedInstanceState);
 
  // Gets to GoogleMap from the MapView and does initialization stuff
  map = mapView.getMap();
  map.getUiSettings().setMyLocationButtonEnabled(false);
  map.setMyLocationEnabled(true);
 
  // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
  try {
   MapsInitializer.initialize(this.getActivity());
  } catch (GooglePlayServicesNotAvailableException e) {
   e.printStackTrace();
  }
  
  // Updates the location and zoom of the MapView
  CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
  map.animateCamera(cameraUpdate);
 
  return v;
 }
 
 @Override
 public void onResume() {
  mapView.onResume();
  super.onResume();
 }
 
 @Override
 public void onDestroy() {
  super.onDestroy();
  mapView.onDestroy();
 }
 
 @Override
 public void onLowMemory() {
  super.onLowMemory();
  mapView.onLowMemory();
 }
 
}

Following is manifest file

 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
  
  
  
   
    
    
   
  
 
 
 


Layout file

    
    
 

Wednesday, 8 January 2014

android convert string to md5 properly

I found some converter have problem with "0". if string have "00" at start then in result some "0" are missing. Following function working well to convert string to md5
public String MD5(String md5) {
   try {
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        byte[] array = md.digest(md5.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
          sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
       }
        return sb.toString();
    } catch (java.security.NoSuchAlgorithmException e) {
    }
    return null;
}

Wednesday, 18 December 2013

Android upload multiple files to server using http


Add following file to your project. there is one demo function which upload file like (audio/video song) to server. you can pass other params also. like song name , artist etc. set your api's url to "AudioVideoBaseURL" variable


public class Soap {
 
 

 public static String AudioVideoBaseURL = "http://abcd.com";

 

 public static String getSoapResponseForVideoAudio(String postFixOfUrl,
   List nameValuePairs,
   List filenameValuePairs) {
  String xmlString = null;
  HttpClient httpClient = new DefaultHttpClient();
  HttpContext localContext = new BasicHttpContext();
  HttpPost httpPost = new HttpPost(AudioVideoBaseURL + postFixOfUrl);

  try {
   MultipartEntity entity = new MultipartEntity();

   for (int index = 0; index < filenameValuePairs.size(); index++) {
    File myFile = new File(filenameValuePairs.get(index).getValue());
    FileBody fileBody = new FileBody(myFile);
    entity.addPart(filenameValuePairs.get(index).getName(),
      fileBody);
   }

   for (int index = 0; index < nameValuePairs.size(); index++) {

    entity.addPart(nameValuePairs.get(index).getName(),
      new StringBody(nameValuePairs.get(index).getValue(),
        Charset.forName("UTF-8")));

   }

   httpPost.setEntity(entity);

   HttpResponse response = httpClient.execute(httpPost, localContext);
   HttpEntity r_entity = response.getEntity();
   xmlString = EntityUtils.toString(r_entity);

  } catch (IOException e) {
   e.printStackTrace();
  }
  return xmlString.toString();
 }


 public static String apiUploadSong(int userId, int songId,
   String songTitle, String songArtist, String isVideo, String fileData)
   throws ClientProtocolException, IOException {

  ArrayList alNameValuePairsFile = new ArrayList();
  NameValuePair nameValuePairsFile = new BasicNameValuePair("fileData",
    fileData);
  alNameValuePairsFile.add(nameValuePairsFile);

  ArrayList alNameValuePairs = new ArrayList();

  NameValuePair nameValuePairs = new BasicNameValuePair("userId", ""
    + userId);
  alNameValuePairs.add(nameValuePairs);
  nameValuePairs = new BasicNameValuePair("songId", ""+songId);
  alNameValuePairs.add(nameValuePairs);
  nameValuePairs = new BasicNameValuePair("songTitle", songTitle);
  alNameValuePairs.add(nameValuePairs);
  nameValuePairs = new BasicNameValuePair("songArtist", songArtist);
  alNameValuePairs.add(nameValuePairs);
  nameValuePairs = new BasicNameValuePair("isVideo", isVideo);
  alNameValuePairs.add(nameValuePairs);

  String result = Soap.getSoapResponseForVideoAudio(
    "?action=save_video_audio", alNameValuePairs,
    alNameValuePairsFile);
  Log.e("SOAP", "save_video_audio : " + result);

  return result;
 }
}


now just add following jar file to your libs folder
apache-mime4j-0.6.jar
httpmime-4.0.1.jar

Monday, 16 September 2013

Android notification using notification builder

To work following code for android version before 3.0, You need to add android support library.
Intent notificationIntent = new Intent(ctx, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
        YOUR_PI_REQ_CODE, notificationIntent,
        PendingIntent.FLAG_CANCEL_CURRENT);

NotificationManager nm = (NotificationManager) ctx
        .getSystemService(Context.NOTIFICATION_SERVICE);

Resources res = ctx.getResources();
Notification.Builder builder = new Notification.Builder(ctx);

builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.some_img)
            .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
            .setTicker(res.getString(R.string.your_ticker))
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentTitle(res.getString(R.string.your_notif_title))
            .setContentText(res.getString(R.string.your_notif_text));
Notification n = builder.build();

nm.notify(YOUR_NOTIF_ID, n);

Sunday, 15 September 2013

android string to date or date to string format


public class DateFormatClass {
    public static void main(String[] args) {

        String mytime="Jan 17, 2012";
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "MMM dd, yyyy");
        Date myDate = null;
        try {
            myDate = dateFormat.parse(mytime);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");
        String finalDate = timeFormat.format(myDate);

        System.out.println(finalDate);
    }
}