Wednesday, June 19, 2019

java LinkedList Example



package linkedlistexp;

class Node {
    int data;
    Node nextNode;
}
class LinkedList {

    private Node head;

    public void insert(int data) {
        System.out.println("----------insert() data=" + data);
        //create a new Node and store a data.
        Node new_node = new Node();
        new_node.data = data;
        new_node.nextNode = null;

        //check the head is null or not.
        //if head is null, assign the Node and exit.
        if (this.head == null) {
            head = new_node;
            System.out.println("1st node: head.data=" + head.data + " next=" + head.nextNode + " ");
            return;
        }

        //assign a head into the last Node and loop it until find the null reference.
        Node lastNode = this.head;
        while (lastNode.nextNode != null) {
            lastNode = lastNode.nextNode;
            System.out.println("while() : last.data=" + lastNode.data+ " last.next=" + lastNode.nextNode);
        }

        //assign new node.
        lastNode.nextNode = new_node;
        System.out.println("last.next.data=" + lastNode.nextNode.data+
                                " last.next.next=" + lastNode.nextNode.nextNode);
    }

    public void print() {
        if (this.head == null) {
            return;
        }
        //print all nodes
        Node tempNode = this.head;
        while (tempNode != null) {
            System.out.print(tempNode.data + "->");
            tempNode = tempNode.nextNode;
        }
        System.out.println("NULL");
    }
}
public class LinkedListExp {

  public static void main(String[] args) {
      LinkedList ls = new LinkedList();
      ls.insert(10);
      ls.insert(20);
      ls.insert(30);
      ls.insert(40);
      ls.insert(50);
      ls.print();
  }

}

output:
run:
run:
----------insert() data=10
1st node: head.data=10 next=null
----------insert() data=20
last.next.data=20 last.next.next=null
----------insert() data=30
while() : last.data=20 last.next=null
last.next.data=30 last.next.next=null
----------insert() data=40
while() : last.data=20 last.next=linkedlistexp.Node@15db9742
while() : last.data=30 last.next=null
last.next.data=40 last.next.next=null
----------insert() data=50
while() : last.data=20 last.next=linkedlistexp.Node@15db9742
while() : last.data=30 last.next=linkedlistexp.Node@6d06d69c
while() : last.data=40 last.next=null
last.next.data=50 last.next.next=null
10->20->30->40->50->NULL
BUILD SUCCESSFUL (total time: 0 seconds)


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

Advantages of a Linked List

  • Insertions and deletions are quick.
  • Grows and shrinks as needed.

Disadvantages of a Linked List

  • Random access is slow. Objects in a linked list must be accessed sequentially, therefore, it can be slow to access a specific object.
  • Memory is a concern. Each object in a linked list requires data as well as one (or more) pointers to other objects in the linked list. Using significantly less memory, each object in an array only requires data.

When to Use a Linked List?

  • You don’t need random access to any specific elements.
  • You need to do constant insertions and deletions.
  • You’re not sure how many items will be in the list.


Thursday, August 23, 2018

java xml parsing

download: http://www.mediafire.com/file/wcob5kk3013kvkp/sax_project1.zip/file

<?xml version="1.0" encoding="UTF-8"?>

<department>

  <employee>
      <empNo>E01</empNo>
      <empName>KING</empName>
      <hireDate>17-11-1981</hireDate>
      <salary>100000</salary>
  </employee>

  <employee>
      <empNo>E02</empNo>
      <empName>JONES</empName>
      <hireDate>02-04-1981</hireDate>
      <salary>200000</salary>
  </employee>

</department>

Sunday, November 19, 2017

android show notification like viber when app is in background



Manifest.xml

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

<activity android:name=".pushnotification.NewMsgDialogActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar"  />







MyFirebaseMEssagingService.java

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

 final Intent intent = new Intent(MyFirebaseMEssagingService.this, NewMsgDialogActivity.class);

        intent.putExtra("trainerId", trainer_id);

        intent.putExtra("trainer_image_str", trainer_image_str);

        intent.putExtra("trainer_name", trainer_name);


        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);


        PendingIntent pendingIntent = PendingIntent.getActivity(MyFirebaseMEssagingService.this, 0, intent, 0);

        try {

            // Perform the operation associated with our pendingIntent

            pendingIntent.send();


        } catch (PendingIntent.CanceledException e) {

            e.printStackTrace();

        }




NewMsgDialogActivity.java

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

public class NewMsgDialogActivity extends Activity {

    String trainerId, trainer_name, trainer_image_str = "";

    public  static  String new_msg = "";


    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


        show_new_msg_dialog();

    }


    public void show_new_msg_dialog() {

        AlertDialog.Builder dialog = new AlertDialog.Builder(this);


        dialog.setTitle(getResources().getString(R.string.new_msg))

                .setCancelable(false)

                .setIcon(R.drawable.messages_icon)

                .setMessage(NewMsgDialogActivity.new_msg)

                .setNegativeButton("Ignore", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialoginterface, int i) {

                        dialoginterface.dismiss();

                        finish();

                    }

                })

                .setPositiveButton("Check", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialoginterface, int i) {

                        goto_voice_msg_activity();

                        dialoginterface.dismiss();

                        finish();

                    }

                }).show();

    }



    @Override

    public void onBackPressed() {

        super.onBackPressed();

        finish();

    }

}



android check app is running foreground

static boolean shouldShowNotification(Context context) {
        ActivityManager.RunningAppProcessInfo myProcess = new ActivityManager.RunningAppProcessInfo();
        ActivityManager.getMyMemoryState(myProcess);
        if (myProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
            return true;

        KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        // app is in foreground, but if screen is locked show notification anyway
        return km.inKeyguardRestrictedInputMode();
    }