Saturday 29 September 2012

Way to communicate with server in java

Many ways to communicate with a server

Socket class
• Lets you do general-purpose network programming
– Same as with desktop Java programming

HttpURLConnection
• Simplifies connections to HTTP servers
– Same as with desktop Java programming

HttpClient
• Simplest way to download entire content of a URL
– Not standard in Java SE, but standard in Android

JSONObject
• Simplifies creation and parsing of JSON data
– Not standard in Java SE, but standard in Android

Thursday 27 September 2012

Diffrent input-type value in EditText Android

  • none
  • text
  • textCapCharacters
  • textCapWords
  • textCapSentences
  • textAutoCorrect
  • textAutoComplete
  • textMultiLine
  • textImeMultiLine
  • textNoSuggestions
  • textUri
  • textEmailAddress
  • textEmailSubject
  • textShortMessage
  • textLongMessage
  • textPersonName
  • textPostalAddress
  • textPassword
  • textVisiblePassword
  • textWebEditText
  • textFilter
  • textPhonetic
  • textWebEmailAddress
  • textWebPassword
  • number
  • numberSigned
  • numberDecimal
  • numberPassword
  • phone
  • datetime
  • date
  • time

Friday 21 September 2012

Android Dialog Box with item select

final CharSequence[] items = {"Add Note", "Add Anniversary", "Add Event","View All"};
        AlertDialog.Builder builder = new
        AlertDialog.Builder(MyCalendar.this);
        builder.setTitle("Add Details");
        //builder.setIcon(R.drawable.calendarmonth);
        builder.setItems(items, new    DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item)
        {
         if(item==0)
         {
             Intent in=new Intent(MyCalendar.this, AddNote.class);
                startActivity(in);
         }
         else if(item==1)
         {
             Intent in=new Intent(MyCalendar.this, AddAnnivarsory.class);
                startActivity(in);
         }
         else if(item==2)
         {
             Intent in=new Intent(MyCalendar.this, AddEvent.class);
                startActivity(in);
         }
         else if(item==3)
         {
             Intent in=new Intent(MyCalendar.this, ViewAll.class);
                startActivity(in);
         }
        }
        });
        AlertDialog alert = builder.create();
        alert.show();

Friday 14 September 2012

Open Android Market App using url in Android

Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setData(Uri.parse("market://search?q=pname:MyApp")); 
startActivity(intent);

Wednesday 12 September 2012

Upload Image from Android to Server using PHP

Android Activity for upload image on Server:--
package com.santosh.geo_map_social_app;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class MainActivity extends Activity {

 InputStream is;

 @Override

 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.activity_main);
  new  AddTask().execute();
 }
 class AddTask extends AsyncTask<Void,Void, Void> {

  private ProgressDialog dialog;
  protected void onPreExecute() {
   dialog = new ProgressDialog(MainActivity.this);
   dialog.setMessage("Retrieving data ...");
   dialog.setIndeterminate(true);
   dialog.setCancelable(false);
   dialog.show();
  }

  protected Void doInBackground(Void... unused) {
   Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
   ByteArrayOutputStream bao = new ByteArrayOutputStream();
   bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
   byte [] ba = bao.toByteArray();
   String ba1=Base64.encodeBytes(ba);
   ArrayList<NameValuePair> nameValuePairs = new
     ArrayList<NameValuePair>();
   nameValuePairs.add(new BasicNameValuePair("image",ba1));
   try{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new
      HttpPost("http://192.168.1.10:80/GK/ImageUpload.php");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
   }catch(Exception e){
    Log.e("log_tag", "Error in http connection "+e.toString());
   }
   return(null);
  }

  protected void onProgressUpdate(Void... unused) {
   // grid_main.setAdapter(imgadp);
  }

  protected void onPostExecute(Void unused) {
   dialog.dismiss();

  }
 }


}

Thursday 6 September 2012

Find Your CURRENT Location In Android

package com.santosh.map;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class FindMyLocation extends Activity implements LocationListener{

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LocationManager locationmanger=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Criteria c=new Criteria();
        c.setAccuracy(Criteria.ACCURACY_FINE);
        c.setAltitudeRequired(false);
        c.setBearingRequired(false);
        c.setCostAllowed(true);
        c.setPowerRequirement(Criteria.POWER_LOW);
        String provider=locationmanger.getBestProvider(c, true);
        Location location=locationmanger.getLastKnownLocation(provider);
      
        locationmanger.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                0, 0, this);
      
    }


    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            System.out.println("Lat:"+location.getLatitude());
            System.out.println("Lat:"+location.getLongitude());
            System.out.println("Lat:"+location.getAccuracy());
            System.out.println("Lat:"+location.getAltitude());
            System.out.println("Lat:"+location.getBearing());
            System.out.println("Lat:"+location.getProvider());
            System.out.println("Lat:"+location.getSpeed());
            System.out.println("Lat:"+location.getTime());
             }

        Toast.makeText(FindMyLocation.this, "onLocationChanged", 1).show();
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText(FindMyLocation.this, "onProviderDisabled", 1).show();
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText(FindMyLocation.this, "onProviderEnabled", 1).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
        Toast.makeText(FindMyLocation.this, "onStatusChanged", 1).show();
    }
 
   
   
}

Wednesday 5 September 2012

Delect Incoming Call in Android

public class PhoneCallReceiver extends BroadcastReceiver {
 Context context = null;
 private static final String TAG = "Phone call";
 private ITelephony telephonyService;

 @Override
 public void onReceive(Context context, Intent intent) {
  Log.v(TAG, "Receving....");
  TelephonyManager telephony = (TelephonyManager) 
  context.getSystemService(Context.TELEPHONY_SERVICE);  
  try {
   Class c = Class.forName(telephony.getClass().getName());
   Method m = c.getDeclaredMethod("getITelephony");
   m.setAccessible(true);
   telephonyService = (ITelephony) m.invoke(telephony);
   telephonyService.silenceRinger();
   telephonyService.endCall();
  } catch (Exception e) {
   e.printStackTrace();
  }
  
 }
 
 
//////////////////////////////////////
 
interface ITelephony {

   
    boolean endCall();

  
    void answerRingingCall();

   
    void silenceRinger();

  }
 
/////////////////////////////////////////////
 
   android:name="android.permission.MODIFY_PHONE_STATE" />
     android:name="android.permission.CALL_PHONE" />
     android:name="android.permission.READ_PHONE_STATE" />
  

Delect Outgoing Call in android

public class OutgoingCallReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
                Bundle bundle = intent.getExtras();
               
                if(null == bundle)
                        return;
               
                String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

                Log.i("OutgoingCallReceiver",phonenumber);
                Log.i("OutgoingCallReceiver",bundle.toString());
               
                String info = "Detect Calls sample application\nOutgoing number: " + phonenumber;
               
                Toast.makeText(context, info, Toast.LENGTH_LONG).show();
        }
}