Tuesday, October 30, 2012

what is meant by Stick Intents

Its interview questions i search some sites my knowledge to share to you.

A normal broadcast Intent is not available anymore after is was send and processed by the system. If you use the sendStickyBroadcast(Intent) method, the Intent is sticky, meaning the Intentyou are sending stays around after the broadcast is complete.


// Register for the battery changed event
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);

/ Intent is sticky so using null as receiver works fine
// return value contains the status
Intent batteryStatus = this.registerReceiver(null, filter);

// Are we charging / charged?
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
  || status == BatteryManager.BATTERY_STATUS_FULL;

boolean isFull = status == BatteryManager.BATTERY_STATUS_FULL;

// How are we charging?
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC; 
Sticky Broadcast Intents typically require special permissions.  reference link



first u understand intents and intents filters:

its also nice blog are here:intents and filters


and u study  services also: services





Wednesday, October 24, 2012

convert text to url.


Linkify is used to convert text to link and text to dial phone number also used.

so we used us Linkify.ALL method.


TextView link = (TextView) findViewById(R.id.link);

link.setText("www.google.com");
Linkify.addLinks(link, Linkify.ALL);

Friday, September 21, 2012

SENDING FILE TO WEB SERVER USING ANDROID


FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(image);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
     
           bitmap=BitmapFactory.decodeStream(fileInputStream);
           ByteArrayOutputStream baos = new ByteArrayOutputStream();
           bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
           byte[] data = baos.toByteArray();
           IMAGE= Base64.encodeBytes(data);
           Log.e("THE IMAGE BYTES",""+IMAGE);




IMAGE IS STRING TO SEND TO WEB SERVER 

Thursday, September 20, 2012

how to find file path extension and size not exceeded to 1 mb in android





String s="file path";
File file = new File(s);
   
long sizeInBytes = file.length();
sizeInBytes = sizeInBytes/1024;
Log.e("login bytes", ""+sizeInBytes);
     //transform in MB
     //double sizeInMb = sizeInBytes/(1024*1024);
               
     if(sizeInBytes<=2048)
   
        {
    Log.e("login", ""+sizeInBytes);
      int dot = c.lastIndexOf(".");
      String ff= c.substring(dot+1);
         
    //  tv1.setText(ff);
    if(ff=="pdf"||ff.compareTo("txt")==0||ff=="docx"||ff=="doc"||ff=="xls"||ff=="xlsx"||ff=="ppt"||ff=="pptx")
      {
    //Log.e("login if", ""+ff);
        exsistingFileName=c;
        Log.e("path file", ""+exsistingFileName);
    String searchFor = "bit.ly/yyyyyy";
    String s1=feed1.getText().toString();
        int len = searchFor.length();
        int result = 0;
        if (len > 0) {
        int start = s1.indexOf(searchFor);
        while (start != -1) {
        result++;
        start =s1.indexOf(searchFor, start+len);                
             
        }
        }        
       if(result==0)
    {
    feed1.append(" bit.ly/yyyyyy ");
    }
       else
       {
     
       }
     
      }
    else
    {
    showAlertDialog("file type only doc,txt,doc,docx,ppt,xls,xls");
    }

//Log.e("path", ""+exsistingFileName);
        }
    else
    {
    showAlertDialog("file size not exceed to 2mb");
    }

}

how to use bit.ly url in ur android program


int leng=ulink.length();
     
         for (int i = 0;  i <= (leng - 7);i++) {
                if (ulink.regionMatches(i, "http://", 0, 6)) {
                youtubelink=ulink;}}
         for (int i = 0;  i <= (leng - 4);i++) {
                if (ulink.regionMatches(i, "www.", 0, 3)) {
                youtubelink=ulink.replace("www.", "http://");
                youtubelink=youtubelink.trim();}}  
       try{
   
          url = as("user name", "api key").call(shorten(youtubelink));
          String s1=url.getShortUrl();
          feed1.append(" "+s1);
       }
       catch(Exception e)
       {
        showAlertDialog("please check your url and its wrong");
       }
       
     
         // Do something with value!
         }




public class BitActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
     
       
        Url url = as("username", "key").call(shorten("http://google.com"));
        String s=url.getShortUrl();
        Log.e("url",s);
   
        URLConnection conn = null;
       
       conn =  connectURL(s);
     
       String sss=conn.toString();
       Log.e("expand",sss);

           
    }

private URLConnection connectURL(String s) {
// TODO Auto-generated method stub
URLConnection conn =null;
try {
        URL inputURL = new URL(s);
        conn = inputURL.openConnection();
             //  int test = 0;

    }catch(MalformedURLException e) {
        System.out.println("Please input a valid URL");
    }catch(IOException ioe) {
        System.out.println("Can not connect to the URL");
    }
return conn;
}





its very simple
       

how to get a device id in android phone


 final TelephonyManager tm = (TelephonyManager) this
  .getSystemService(Context.TELEPHONY_SERVICE);

  final String tmDevice, tmSerial, androidId;
  tmDevice = "" + tm.getDeviceId();
  tmSerial = "" + tm.getSimSerialNumber();
  androidId = ""
  + android.provider.Settings.Secure.getString(this
  .getContentResolver(),
  android.provider.Settings.Secure.ANDROID_ID);

  UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice
  .hashCode() << 32)
  | tmSerial.hashCode());
  DID = deviceUuid.toString();


important user permission is  <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Friday, August 31, 2012

KSOAP2 USING SEND IMAGE TO WEB SERVER IN ANDROID

its ksoap codes to send image and file to web server :



public class SoapuploadActivity extends Activity {
    /** Called when the activity is first created. */
public final String SOAP_ACTION = "http://tempuri.org/Add";

public  final String OPERATION_NAME = "Add";

public  final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";

public  final String SOAP_ADDRESS = "http://grasshoppernetwork.com/NewFile.asmx";
TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        tv=(TextView)findViewById(R.id.textView2);
     
        Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bMap .compress(Bitmap.CompressFormat.PNG, 100, baos);
        //bMap is the bitmap object
        byte[] b = baos.toByteArray();
        int i=b.length;
        Log.e("the output is",""+i);
String  IMAGE= Base64.encodeBytes(b);
        Call(IMAGE);
     
    }
 
 

public void Call(byte[] b)
{

SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
PropertyInfo pi=new PropertyInfo();

        pi=new PropertyInfo();
        pi.setName("b");
        pi.setValue(b);
        pi.setType(byte[].class);
        request.addProperty(pi);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;

envelope.setOutputSoapObject(request);

HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
Object response=null;
try

{

httpTransport.call(SOAP_ACTION, envelope);

response = envelope.getResponse();



}

catch (Exception exception)

{

response=exception.toString();
Log.e("the error is",""+response);

}

Log.e("the output is",""+response);
tv.setText(response.toString());



}
 
 
}



its download and add library file to project:  link
its download and add base64conversion to project:  base 64