Pages

Sabtu, 05 Mei 2012

Notification

How to use Notification in Android?


Android represent the Notifications by using Notification class.We have to create a notification by using NotificationManager class,it can be received from the Activity via getSystemService() method.

Eg: NotificationManager notificationManager = (NotificationManager)getSystemService (NOTIFICATION_SERVICE);


Here i use two edit text for giving the email and message  and one button for create a notification.
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <EditText
        android:id="@+id/username_et"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
    android:inputType="text"/>
    <EditText
        android:id="@+id/mess_et"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:inputType="text" />
    <Button
        android:id="@+id/button1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:onClick="createNotification"
        android:text="Create Notification" android:layout_gravity="center_horizontal"/>
   
</LinearLayout>

 
Another xml for receive the notification message UI, name as notification_receiver.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the result activity opened from the notification" >
    </TextView>
</LinearLayout>


Java class name NotificationActivity.java

public class NotificationActivity extends Activity {
   EditText username_et,mess_et;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
    }
    public void createNotification(View view) {
     username_et=(EditText)findViewById(R.id.username_et);
        mess_et=(EditText)findViewById(R.id.mess_et);
       
  NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  Notification notification = new Notification(R.drawable.mss,"A new notification", System.currentTimeMillis());
  // Hide the notification after its selected
  notification.flags |= Notification.FLAG_AUTO_CANCEL;
  Intent intent = new Intent(this, NotificationReceiver.class);
  PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
  notification.setLatestEventInfo(this, username_et.getText().toString(), mess_et.getText().toString(),activity);
 
  notification.number += 1;
  notificationManager.notify(0, notification);
 }
}


and finally NotificationReceiver.java class.
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification_receiver);
    }


         
        


                                 

Selasa, 01 Mei 2012

How to use Image can convert into ByteArray

How to Image can convert into ByteArray
Here I used barcode image can convert into Byte Array and the Byte Array can reconvert into the original form.I used two Buttons,textview for values and one static image then ByteArrayOutputStream for conversion.
Here java code ,The class name as ImageConversion.java

public class ImageConvertScreen extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
  Button img_byte,byte_img;
  ImageView image;
  TextView value;
  public ByteArrayOutputStream bos;
  public Bitmap bm;
  public byte[] bitmapdata;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      
        image = (ImageView) findViewById(R.id.img_convert);
        value=(TextView)findViewById(R.id.convert_txt);
        img_byte =(Button)findViewById(R.id.img_byte);
        byte_img =(Button)findViewById(R.id.byte_img);
        img_byte.setOnClickListener(this);
        byte_img.setOnClickListener(this);
     
     
       bm = BitmapFactory.decodeResource(getResources(),R.drawable.barcode);
       bos = new ByteArrayOutputStream();
       bm.compress(Bitmap.CompressFormat.JPEG, 40 , bos);
      
      
    }
    public void onClick(View v) {
  if (v == img_byte) { 
   bitmapdata = bos.toByteArray();
      Log.w("Image Conversion", String.valueOf(bitmapdata.length));
      String converted_txt="";
         for (int i = 0; i < bitmapdata.length; i++) {
       Log.w("Image Conversion", String.valueOf(bitmapdata[i]));
          converted_txt=converted_txt+bitmapdata[i];
         }
         value.setText(converted_txt);
         value.setVisibility(View.VISIBLE);
         image.setVisibility(View.INVISIBLE);
  
  } else if (v==byte_img){
    bm = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);
        image.setImageBitmap(bm);
        image.setVisibility(View.VISIBLE);
        value.setText("");
        value.setVisibility(View.INVISIBLE);
        Log.w("Image Conversion", "converted");

  }
    }
   
}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    <Button
        android:id="@+id/img_byte"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="Image To Byte" android:layout_marginLeft="50dp"/>
    <TextView
        android:id="@+id/convert_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bytes"
        android:textAppearance="?android:attr/textAppearanceLarge" android:visibility="invisible"/>
    <ImageView
        android:id="@+id/img_convert"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.16"
        android:src="@drawable/barcode" android:layout_marginBottom="5dp"/>
    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <Button
            android:id="@+id/byte_img"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="Byte To Image" android:layout_marginLeft="50dp"/>
    </LinearLayout>
       
    </LinearLayout>
</ScrollView>