Sunday 2 March 2014

Android Notification example



final NotificationManager mgr =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.phone,"Vacant Class Notification..!", System.currentTimeMillis());
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

// This pending intent will open after notification click
PendingIntent i = PendingIntent.getActivity(this, 0,new Intent(this, NotificationReceiver.class), 0);
note.setLatestEventInfo(getApplicationContext(), "Vacant Class Notification..!",message, i);
note.sound = soundUri;

mgr.notify(0, note);

Sunday 16 February 2014

Android Dynamic Check-box Example

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Attendance extends Activity {
CheckBox checkBox;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.stud_attendence);
LinearLayout my_layout = (LinearLayout) findViewById(R.id.my_layout);
LinkedHashMap<String, String> alphabet = new LinkedHashMap<String, String>();
// int Array_Count = 0;
String[] Str_Array = new String[] { "12111447:Arun Kumar",
"12111448:Aneesh", "12111449:Binoy shyam" };


for (int i = 0; i < Str_Array.length; i++) {
String[] std= Str_Array[i].split(":");
alphabet.put(std[0],std[1]);

}

Set<?> set = alphabet.entrySet();

Iterator<?> i = set.iterator();
while (i.hasNext()) {
@SuppressWarnings("rawtypes")
Map.Entry me = (Map.Entry) i.next();
checkBox = new CheckBox(this);
checkBox.setId(Integer.parseInt(me.getKey().toString()));
checkBox.setText(me.getKey().toString()+" : "+me.getValue().toString());
checkBox.setOnClickListener(getOnClickDoSomething(checkBox));
my_layout.addView(checkBox);
}

}

View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(
getApplicationContext(),
"Id:" + button.getId() + " and text: "
+ button.getText().toString(), 2000).show();

}
};
}

}

Output:





Wednesday 12 February 2014

Android Reading Inbox SMS

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

public class SMSRead extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      TextView view = new TextView(this);
      Uri uriSMSURI = Uri.parse("content://sms/inbox");
      Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
      String sms = "";
      while (cur.moveToNext()) {
          sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";         
      }
      view.setText(sms);
      setContentView(view);
  }
}
Add below permission to AndroidManifest.xml
   
<uses-permission android:name="android.permission.READ_SMS" />
  

Monday 10 February 2014

DES Algorithm source code in JAVA


import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;

public class DES {

    static Cipher ce;
    static Cipher cd;

    public static void main(String args[]) throws Exception {

     

        SecretKey skey;
        skey = KeyGenerator.getInstance("DES").generateKey();

        byte[] initializationVector = new byte[]{0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x01, 0x02};

        AlgorithmParameterSpec algParameters = new IvParameterSpec(initializationVector);

        ce = Cipher.getInstance("DES/CBC/PKCS5Padding");

        cd = Cipher.getInstance("DES/CBC/PKCS5Padding");

        ce.init(Cipher.ENCRYPT_MODE, skey, algParameters);

        cd.init(Cipher.DECRYPT_MODE, skey, algParameters);

        FileInputStream is = new FileInputStream("F:/sem1report/test.txt");

        FileOutputStream os = new FileOutputStream("F:/sem1report/encry.txt");

        int dataSize = is.available();

        byte[] inbytes = new byte[dataSize];

        is.read(inbytes);

//        String str2 = new String(inbytes);
//
//        System.out.println("Input file contentn" + str2 + "n");

        write_encode(inbytes, os);

        os.flush();

        is.close();

        os.close();

        System.out.println("Ecrypted Content to output.txt");

        FileInputStream is1 = new FileInputStream("F:/sem1report/encry.txt");
        int endataSize = is1.available();

        byte[] decBytes = new byte[endataSize];

        read_decode(decBytes, is1);

        is.close();
        FileOutputStream os1 = new FileOutputStream("F:/sem1report/decry.txt");

        os1.write(decBytes);
        os1.flush();
//        String str = new String(decBytes);
//
//        System.out.println("Decrypted file contents:n" + str);

    }

    public static void write_encode(byte[] bytes, OutputStream output) throws Exception {

        CipherOutputStream cOutputStream = new CipherOutputStream(output, ce);

        cOutputStream.write(bytes, 0, bytes.length);

        cOutputStream.close();
    }

    public static void read_decode(byte[] bytes, InputStream input) throws Exception {

        CipherInputStream cInputStream = new CipherInputStream(input, cd);

        int position = 0, i;

        while ((i = cInputStream.read()) != -1) {

            bytes[position] = (byte) i;

            position++;

        }
    }
}

Sunday 9 February 2014

Decimal to Hexadecimal Converter in Java

CONVERT DECIMAL INTEGER TO HEXADECIMAL 

NUMBER EXAMPLE


import java.io.*;
import java.lang.*;

public class DecimalToHexadecimal{
  public static void main(String[] args) throws IOException{  
  BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter the decimal value:");
  String hex = bf.readLine();
  int i = Integer.parseInt(hex);
  String hex1 = Integer.toHexString(i);
  System.out.println("Hexa decimal: " + hex1);
  }
}


run:
Enter the decimal value:
16
Hexa decimal: 10

How to Convert from binary to hex in java

SIMPLE ONE LINE CODE TO CONVERT STRING CONTAINING A BINARY VALUE TO HEX


String bin = Integer.toHexString(Integer.parseInt(binOutput, 2));
//when integer size exceeds use Long instead of Integer

String bin = Long .toHexString(Long .parseLong(binOutput, 2));

FULL CODE FOR CONVERSION


import java.io.*;
import java.lang.*;



public class  BinaryToHexadecimal{
  public static void main(String[] args)throws IOException{
  BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter the Binary number:");
  String hex = bf.readLine();
  long num = Long.parseLong(hex);
  long rem;
  while(num > 0){
  rem = num % 10;
  num = num / 10;
  if(rem != 0 && rem != 1){
  System.out.println("This is not a binary number.");
  System.out.println("Please try once again.");
  System.exit(0);
  }
  }
  int i= Integer.parseInt(hex,2);
  String hexString = Integer.toHexString(i);
  System.out.println("Hexa decimal: " + hexString);
  }
}

run:
Enter the Binary number:
101010
Hexa decimal: 2a

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