Skip to main content

Posts

Detecting shut down and reboot in android

Hi, Have a great day frnds..

Now we going to look up the detecting shut down and reboot events in android.

Here is the code:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;


public class ShutDownReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
            Log.i("event", "shutdown");
        } else if (intent.getAction().equals(Intent.ACTION_REBOOT)) {
            Log.i("event", "reboot");
        }
    }
}

Then apply in your activity,

shutDownReceiver = new ShutDownReceiver();
filter.addAction(Intent.ACTION_REBOOT);
filter.addAction(Intent.ACTION_SHUTDOWN);

@Override
    protected void onPause() {
        super.onPause();
unregisterReceiver(shutDownReceiver);
}

@Override
    protected void onResume() {
        super.onResume();
registerReceiver(shutDownReceiver, fil…
Recent posts

Detecting Screen on and Screen Off via services

Hi buddies, have a happy Sunday.

Today I'll explore about detecting screen on and screen off in android devices.

I got your mind, can use also by Broad cast Receivers. Yes, you can.

But how do you know, if app goes offline and screen off.

First write the Broad cast receiver class for receiving results.

ScreenReceiver.Java
--------------------------
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class ScreenReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            screenOff = true;
            Log.i("screenLog", "screen off");

        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOff = false;
            Log.i("screenLog", "screen on");
        }
    }
}

Then write the service class:

UpdateScreen.Java

import a…

Print the large string values in android log cat.

Hi,

Have a happy Monday.

In Android, sometimes we need to print the large numbers of strings value in log. But log can't accept whole value of the string.

In this case, we need to split the logs. It's very simple android code, I attached below this blog.

public static void logLargeString(String TAG, String msg) {         if (str.length() > 3000) {             Log.i(TAG, msg.substring(0, 3000));             logLargeString(TAG, msg.substring(3000));         } else {             Log.i(TAG, msg);         } }
Just use it and enjoy...

Backward and Forward Asterisks

Hi, Have a happy Sunday. Today I'm going to explain, how to deal with asterisks problems.

Just before the day, I've been attend the interview.

Question:
To print out Asterisk (*) in system console in specific order.
*****
****
***
**
*

This is backward asterisks, I'm failed to do that, because of less time, no test cases and unfortunate system freezes. Yeah, this is not accept by recruiters. It's very simple problem, I know.

Anyway 15 minutes passes by. No clues.

I'm going to give the answer by two ways here now.
1. Backward Asterisks
2. Forward Asterisks

Solution:

public class Asterisks{
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the number of rows : ");
        int n = scan.nextInt();
        System.out.println("Backward Asterisks");
        for (int i = n; i >=0; i--) {
 …

Vending Machine Task - Java

Hi, Happy Weekends. Most boring thing is every time you've been asked same questions or you've been requested to do same tasks in different ways. I faced a lot.

In IT field, It's happens for everyone at least one time. Don't be sad, It's a good practice and helpful. We can also prove our self to do same works in different ways, It's a kind of practice. It's a kind of strategy.

In earlier times, Most of times in interview, I getting the calculator tasks in Android. Yeah, I do successfully in each time.

Here I come to explain the Vending Machine Task in Java. I've asked for this task for three times in a row now, each time differently.

What is Vending Machine?
A vending machine is a machine that dispenses items such as snacks, beverages, alcohol, cigarettes, lottery tickets to customers automatically, after the customer inserts currency or credit into the machine.

How it's works?
Vending machine is having a different product items with different deno…

Image Gallery Using Flickr

Hi friends, have a great day. Now I just explain how to use Flickr.

Flickr is one of largest media library in the world, you can store your photos & videos in this application.

Both web version and mobile version are available.

API details: https://www.flickr.com/services/api/

You can access the flickr API by create the app, get the api_key and secret key.

You can able to show the public or your own photo catalogues.

Public feeds are available in

https://www.flickr.com/services/feeds/docs/photos_public/

I create one simple application to access the public photos using Flickr API.

You can download or clone in Github

Algorithm - Heap Sort

Heap Sort
Heapsort is a comparison-based sorting algorithm. Heapsort can be thought of as an improved selection sort: like that algorithm, it divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region.

It's an In-Place algorithm, but It's not a stable sort.

Solution

public class HeapSort {
    private static int num;
    public static void sort(int array[]) {
        doSort(array);
        for (int i = num; i > 0; i--) {
            swap(array, 0, i);
            num = num - 1;
            maxHeap(array, 0);
        }
    }
    public static void doSort(int arr[]) {
        num = arr.length - 1;
        for (int i = num / 2; i >= 0; i--) {
            maxHeap(arr, i);
        }
    }
    public static void maxHeap(int array[], int i) {
        int left = 2 * i;
        int right = 2 * i + 1;
        int max = i;
        if (left <= num && array[left] > array[i]) {
  …