Showing posts with label notification. Show all posts
Showing posts with label notification. Show all posts

Signal in LINUX

Hi friends,

Currently, I'm working in wake-up issue in my project. I wanna inform android when my host OS windows wakes up. My project leader gave me an idea to send a signal from windows to android in the shell and use that signal to wake android when windows wakes up, like in exam days I keep alarm by 4am, my bad wakes up and give me a kick ;). But I'm zero at LINUX signal concept, as usual i studied it. Now, I'm typing here for my future ref. and other beginner's use.

SIGNAL IN LINUX

Now, we use CTRL + C to terminate something in LINUX. But, we don't wanna let someone to terminate our code with CTRL + C. So, what we have to do now. We have to monitor the user input and we should not let the system to perform any operation when user gives CTRL +C.

But, Instead what we can do in LINUX is, CTRL + C will generate a signal SIGINT in LINUX, so we can register a function to this signal in our code. So that when we user press CTRL + C, it will call our function, so our code won't be terminated.

What is signal in LINUX ? 
 LINUX has a set of signals. One nice example is, kill -9 PID, which we use to kill a process. Here, we are sending KILL signal to a process. Similarly, few other signals are available in LINUX. (SRY, i don't know to explain it, pls ask prof.google :( )

How can we register a function to a signal ?
here is the syntax,

signal(SIGINT, sig_handler)
SIGINT - signal generated on pressing CTRL + C. It can be any LINUX signal.
sig_handler - our function, which will be executed on receiving SIGINT signal.

What should be the sig_handler signature? 
here it is,
void sig_handler(int signo) {}
signo - signal number crossponding to the register signal.
Inside the function, we can write our own story.

Ok, How the final code will be? 
here is the simple one,
#include <stdio.h>
#include
<signal.h>
#include <unistd.h>

void sig_handler(int signo)
{
  if (signo == SIGINT)
    printf("received SIGINT\n");
}

int main(void)
{
  if (signal(SIGINT, sig_handler) == SIG_ERR)
  printf("\ncan't catch SIGINT\n");
  // A long long wait so that we can easily issue a signal to this process
  while(1)
    sleep(1);
  return 0;
}

Now, run the code and press CTRL + C. It will print "^Creceived SIGINT".

But, LINUX has alot of signals, is it possible to do for all signal of LINUX?
 Expect SIGKILL and SIGSTOP, we can use any signal in the place of SIGINT. So, change signal you would like register, change the body of sig_handler function as you like. Your coding will be done.

In my case, I have to generate a signal on my own... I choose to send user signal(SIGUSR1)...
So, the code will be?
here it is,

#include <stdio.h>
#include
<signal.h>
#include <unistd.h>

void sig_handler(int signo)
{
  if (signo == SIGUSR1)
    printf("received SIGUSR1\n");
}

int main(void)
{
  if (signal(SIGUSR1, sig_handler) == SIG_ERR)
  printf("\ncan't catch SIGUSR1\n");
  // A long long wait so that we can easily issue a signal to this process
  while(1)
    sleep(1);
  return 0;
}

Thats all, compile and run it :)... But how to send user signal? The following line will do that...
KILL -USR1 PID
 Got the signal!!!....

Android Notification ++

Hi friend,

Today, I was working in creating the Notification. I have to notify, when back button is pressed from my android app. As usual I went through the android developer site and studied how to do notify in the notification bar. But I had a small problem in it, which you can also get, when you work in Android Notification. So I planned to share it here.

Android Notification ++

Creating Notification is very simple. Just read it in android developer portal, probably you have to end with the following lines of code to create notification.

/Overriding the Back Button press event /
@Override
    public void onBackPressed() {
    // do something on back.

/* Get a reference to the NotificationManager */
    NotificationManager notificationManager = (NotificationManager)
                getSystemService(NOTIFICATION_SERVICE);

/* Instantiate the Notification */
    Notification notification = new Notification(R.drawable.icon,
            "A new notification", System.currentTimeMillis());

/* Hide the notification after its selected */
    notification.flags |= Notification.FLAG_AUTO_CANCEL;


/*  Creating the intent to an activity, which has to be called and setting it to notification */
    Intent intent = new Intent(this, ActivityName.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
    notification.setLatestEventInfo(this, "This is the title",
            "This is the text", activity);

/*Pass the Notification to the NotificationManager */   
    notification.number += 1;
    notificationManager.notify(0, notification);
    }

Sending the activity to background

But above lines of code will not send the activity to the background. So you will see the notification but the activity will be open. So you have to add the following line to send the activity to the background.

moveTaskToBack (true);

Bringing the activity to front


Once the notification is clicked, the intent will be created newly (note that the activity will be created newly). Older activity will be running in the back of the newly created activity. But most of the case we don't need it. We will need to open the activity that we sent to the background. The following line will do that magic for us.

intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

NOW

Creating the notification and sending the task to the back and on clicking the notification bringing the same old activity (or task) to the front is done by the following lines of code.

 @Override
    public void onBackPressed() {
        // do something on back.
        moveTaskToBack (true);
        NotificationManager notificationManager = (NotificationManager)
                getSystemService(NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher,
            "A new notification", System.currentTimeMillis());
    // Hide the notification after its selected
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    Intent intent = new Intent(this, Test.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
    notification.setLatestEventInfo(this, "This is the title",
            "This is the text", activity);
    notification.number += 1;
    notificationManager.notify(0, notification);
    }

 Thats All!!! enjoy!!! 

Older Posts Home

About Me

My photo
Hi everyone,myself Alagappan...electronic and communication engg. student... living in madurai... interested in everything... want to achieve something great in my lifetime...

Followers


Recent Comments