Services in Android

Services are seprate entity which is being run in background of the app. It have no effect on the front end of the app activity(functionality).

• A facility for the application to tell the system about something it wants to be doing in the background (even when the user is not directly interacting with the application).  This corresponds to calls to Context.startService(), which ask the system to schedule work for the service, to be run until the service or someone else explicitly stop it.
• A facility for an application to expose some of its functionality to other applications. This corresponds to calls to Context.bindService(), which allows a long-standing  connection to be made to the service in order to interact with it.

Here we implement LocalService for calling some sync task which is being required for particular app. Service class is created by extends Service. Override method of Services is as : onBind(Intent i), onCreate(), onDestroy() and onStart(Intent intent, int startid).


import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import com.ylog.backGroundServices.SyncTableUsers;
import com.ylog.backGroundServices.SyncVehicleLog;
import com.ylog.misc.ApplicationClass;


public class LocalService extends Service {
private static Boolean flag = false;
private ApplicationClass app;

Below method is used to bind with any particular intent.

@Override
public IBinder onBind(Intent intent) {
Log.i("TAG ", intent.getAction());
return null;
}

Below method is used to Create the service. As it is part of Service life cycle.

@Override
public void onCreate() {
Log.i("info", "onCreate");
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
flag = true;
}
Below method is used to Destroy or stop the service. As it is part of Service life cycle.

@Override
public void onDestroy() {
Log.i("info", "onDestroy");
flag = false;
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
}

Below method is used to Start service. Here we implement the task which we want to be run it on the app background.
Here we use 3 timer for calling particular function after every fixed duration of time.

@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startid) {
app = (ApplicationClass) intent.getSerializableExtra("app");
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Timer timer = new Timer();
final SyncTableUsers syncUserObj = new SyncTableUsers(LocalService.this);
final SyncVehicleLog vLogObj = new SyncVehicleLog(LocalService.this);

Below method will execute after 10 millisecond as service is started, and call after every 1.30 minute.

TimerTask timerTask = new TimerTask() {

@Override
public void run() {
if (flag) {
syncUserObj.syncUserData();
}
}
};
timer.scheduleAtFixedRate(timerTask, 10, 1800000); // call after
// every 1.30 minute

Below method will execute after 10 millisecond as service is started, and call after every 10 second.


if (app.getType_name().equalsIgnoreCase("DRIVER")) {
TimerTask timerTask1 = new TimerTask() {

@Override
public void run() {
if (flag) {
int logId = vLogObj.insertInTblVehicleLog();
}
}
};
timer.scheduleAtFixedRate(timerTask1, 10, 10000); // call
// after 10 sec
}

Below method will execute after 10 second as service is started, and call after every 1minute.


if (app.getType_name().equalsIgnoreCase("DRIVER")) {
TimerTask timerTask2 = new TimerTask() {

@Override
public void run() {
if (flag) {
vLogObj.syncVehicleLog();
}
}
};
timer.scheduleAtFixedRate(timerTask2, 10000, 60000); // call after 1 minute
}
super.onStart(intent, startid);
}
}

For start service we call it as:
Intent i = new Intent(Context, LocalService.class);
startService(i);
For using service it require a enry in Android mainfeat file
// Below android:name is package name where service class is created
< service
android:name="com.webservice.LocalService" android:enabled="true" >
< /service>

Running and Configuring VNC Server on Linux

VNC stands for Virtual Network Computing. It is, in essence, a remote display system which allows you to view a computing 'desktop' environment not only on the machine where it is running, but from anywhere on the Internet and from a wide variety of machine architectures.

The VNC system allows you to access the same desktop from a wide variety of platforms.

Oracle Enterprise Linux ships with a VNC Server. If you find it is not installed, you can deploy the package thats part of the install CD/DVD. Just cd to the "Server" directory and run yum install vnc-server*

• To run VNC on different port other than default vncport edit /usr/bin/vncserver file.


[root@sanjay ~]# vim /usr/bin/vncserver
vncport=
for ex. vncport=5902

$vncPort = 5902;
$desktopLog = "$vncUserDir/$host:$displayNumber.log";
unlink($desktopLog);

• Starting VNC service on new defined port
#vncserver :5902
• Set the password by using vncpasswd command

[root@sanjay ~]# vncpasswd
Password:
Verify:

• Edit the /root/.vnc/xstartup file,
Uncomment the following two lines for normal desktop.
unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc

• Perform the following steps



Now, you can confirm the VNC server is up again by executing netstat command:


[root@sanjay ~]# netstat -tulpan | grep vnc

Parsing XML Files in iOS

Reading XML files is one of the common tasks we perform in our application, In this tutorial let's see how we can use NSXMLParser to parse XML in our iPhone app.

Introduction

NSXMLParser is a forward only reader or an event driven parser. What it means is, an event is raised whenever the parser comes across a start of an element, value, CDATA and so on. The delegate of NSXMLParser can then implement these events to capture XML data. Some of the events are raised multiple times like the start of an element, value of an element and so on. Since NSXMLParser is known as an event driven parser, we can only read data at the present node and cannot go back. The iPhone only supports NSXMLParser and not NSXMLDocument , which loads the whole XML tree in memory.

Books Application


To understand how to use an instance of NSXMLParser, let's create a simple navigation based application where we will list
the title of the book in the table view and upon selecting a title, display the detail information in a detail view.Create
a new application inXCode by selecting Navigation-Based Application, I have named my app XML. Since the NSXMLParser is a
forward only parser or an event driven parser, we need to store the data locally, which can be used later. To store this data,
we will create a class which replicates the elements and attributes in the XML file. An instance of this class represents one
single Book element in the XML file. I have named this class "Book" and its source code is listed below

//Book.h
#import

@interface Book : NSObject {

NSInteger bookID;
NSString *title; //Same name as the Entity Name.
NSString *author; //Same name as the Entity Name.
NSString *summary; //Same name as the Entity Name.
}

@property (nonatomic, readwrite) NSInteger bookID;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *author;
@property (nonatomic, retain) NSString *summary;
@end

//Book.m
#import "Book.h"
@implementation Book
@synthesize title, author, summary, bookID;

- (void) dealloc {
[summary release];
[author release];
[title release];
[super dealloc];
}

@end

Notice that the name of the property is the same as the element name in the XML file. Since the XML file has n number of Book elements, we need an array to hold all the books we read, so we declare an array in the application delegate and this is how the source code changes


//XMLAppDelegate.h
#import
@interface XMLAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
NSMutableArray *books;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) NSMutableArray *books;

@end

Reading XML files is one of the common tasks we perform in our application, In this tutorial let's see how we can use NSXMLParser to parse XML in our iPhone app.

Introduction

NSXMLParser is a forward only reader or an event driven parser. What it means is, an event is raised whenever the parser comes across a start of an element, value, CDATA and so on. The delegate of NSXMLParser can then implement these events to capture XML data. Some of the events are raised multiple times like the start of an element, value of an element and so on. Since NSXMLParser is known as an event driven parser, we can only read data at the present node and cannot go back. The iPhone only supports NSXMLParser and not NSXMLDocument, which loads the whole XML tree in memory.

Books Application


To understand how to use an instance of NSXMLParser, let's create a simple navigation based application where we will list the
title of the book in the table view and upon selecting a title, display the detail information in a detail view.

Create a new application in XCode by selecting Navigation-Based Application, I have named my app XML. Since the NSXMLParser is
a forward only parser or an event driven parser, we need to store the data locally, which can be used later. To store this data,
we will create a class which replicates the elements and attributes in the XML file. An instance of this class represents one
single Book element in the XML file. I have named this class "Book" and its source code is listed below

//Book.h
#import

@interface Book : NSObject {
NSInteger bookID;
NSString *title; //Same name as the Entity Name.
NSString *author; //Same name as the Entity Name.
NSString *summary; //Same name as the Entity Name.
}

@property (nonatomic, readwrite) NSInteger bookID;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *author;
@property (nonatomic, retain) NSString *summary;

@end

//Book.m
#import "Book.h"
@implementation Book
@synthesize title, author, summary, bookID;

- (void) dealloc {
[summary release];
[author release];
[title release];
[super dealloc];
}
@end

Notice that the name of the property is the same as the element name in the XML file. Since the XML file has n number of Book elements, we need an array to hold all the books we read, so we declare an array in the application delegate and this is how the source code changes


//XMLAppDelegate.h
#import

@interface XMLAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
NSMutableArray *books;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) NSMutableArray *books;
@end

This is how the data looks like in the table view and the detail view controller.



The data is then shown in a UITableView with a detail view, the complete code is not shown here but you can download the source code and to get a better understanding UITableView, you can follow my suggested readings below.

Oracle Startup

Starting oracle database
Starting up Oracle database requires some important detail to note. Failure to follow the proper
steps would end up to failed database startup.
Remember that we need to login as a user with SYSDBA privileges to startup an Oracle database which would enable us to for example do a – SQL>connect / as sysdba.
Here is an example of a DBA connecting to his database and starting the instance:

In the above example the database was shutdown so when we logged as sysdba, we are then connected to idle instance. When we executed the startup query the database was opened after going through different startup stages as shown in example and discussed below:
* Startup (nomount)
* Mount
* Open
Let's look at these stages in a bit more detail.

The Startup (nomount) Stage

When you issue the startup command, the first thing the database will do is enter the nomount stage. During the nomount stage, Oracle first opens and reads the initialization parameter file (init.ora) to see how the database is configured. For example, the sizes of all of the memory areas in Oracle are defined within the parameter file.
After the parameter file is accessed, the memory areas associated with the database instance are allocated. Also, during the nomount stage, the Oracle background processes are started. Together, we call these processes and the associated allocated memory the Oracle instance. Once the instance has started successfully, the database is considered to be in the nomount stage. If you issue the startup command, then Oracle will automatically move onto the next stage of the startup, the mount stage.

Starting the Oracle Instance (Nomount Stage)


There are some types of Oracle recovery operations that require the database to be in
nomount stage. When this is the case, you need to issue a special startup command: startup
nomount, as seen in this example:

SQL> startup nomount

The Mount Stage

When the startup command enters the mount stage, it opens and reads the control file. The control file is a binary file that tracks important database information, such as the location of the database datafiles.
In the mount stage, Oracle determines the location of the datafiles, but does not yet open them. Once the datafile locations have been identified, the database is ready to be opened.

Mounting the Database

Some forms of recovery require that the database be opened in mount stage. To put the database in mount stage, use the startup mount command as seen here:

SQL> startup mount

If you have already started the database instance with the startup nomount command, you might change it from the nomount to mount startup stage using the alter database command:

SQL> alter database mount;

The Open Oracle startup Stage

The last startup step for an Oracle database is the open stage. When Oracle opens the database, it accesses all of the datafiles associated with the database.
Once it has accessed the database datafiles, Oracle makes sure that all of the database datafiles are consistent.

Opening the Oracle Database


To open the database, you can just use the startup command as seen in this example

SQL> startup

If the database is mounted, you can open it with the alter database open command as seen in
this example:

SQL> alter database open;

Opening the Database in Restricted Mode

You can also start the database in restricted mode. Restricted mode will only allow users with special privileges (we will discuss user privileges in a later chapter) to access the database (typically DBA's), even though the database is technically open. We use the startup restrict command to open the database in restricted mode as seen in this example.

SQL> startup restrict

You can take the database in and out of restricted mode with the alter database command as seen in this example:
-- Put the database in restricted session mode.
SQL> alter system enable restricted session;
-- Take the database out of restricted session mode.
SQL> alter system disable restricted session;

Note: Any users connected to the Oracle instance when going into restricted mode will remain connected; they must be manually disconnected from the database by exiting gracefully or by the DBA with the "alter system kill session 'sid,serial#'" command.

Drag and drop marker on google map

I have markers. I would like to move the marker with a drag and drop-function, and then check the location where we drop that marker.
1.) In this show google map tiles show on screen and zoom at given level.

MainActivity.java


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


MapView map = (MapView) findViewById(R.id.map);

map.getController().setCenter(
getPoint(40.70686417491799, -74.01572942733765));
map.getController().setZoom(17);
map.setBuiltInZoomControls(true);
}

2.) I was doing some precise measurements with markers on my map, and I wanted my marker to be exactly on the spot I touched and lifted my finger so the measurement was exactly precise.

MainActivity.java


ImageView dragImage=(ImageView) findViewById(R.id.drag);
Drawable marker = getResources().getDrawable(R.drawable.marker);

marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());

map.getOverlays().add(new SitesOverlay(marker, dragImage, map));

3.) If you touch "near" the marker, the marker does not go to that exact point, but moves along with your finger's movement. I needed the marker to appear just below my finger on the ACTION_DOWN, ACTION_MOVE and ACTION_UP.

SiteOverLay.java


package com.example.draganddropbasic;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class SitesOverlay extends ItemizedOverlay {
private List items = new ArrayList();
private Drawable marker = null;
private OverlayItem inDrag = null;
private ImageView dragImage = null;
private int xDragImageOffset = 0;
private int yDragImageOffset = 0;
private int xDragTouchOffset = 0;
private int yDragTouchOffset = 0;
MapView map;

public SitesOverlay(Drawable marker, ImageView dragImage, MapView map) {
super(marker);
this.marker = marker;
this.map = map;
this.dragImage = dragImage;

xDragImageOffset = dragImage.getDrawable().getIntrinsicWidth() / 2;
yDragImageOffset = dragImage.getDrawable().getIntrinsicHeight();

items.add(new OverlayItem(getPoint(40.748963847316034,
-73.96807193756104), "UN", "United Nations"));
items.add(new OverlayItem(getPoint(40.76866299974387,
-73.98268461227417), "Lincoln Center",
"Home of Jazz at Lincoln Center"));
items.add(new OverlayItem(getPoint(40.765136435316755,
-73.97989511489868), "Carnegie Hall",
"Where you go with practice, practice, practice"));
items.add(new OverlayItem(getPoint(40.70686417491799,
-74.01572942733765), "The Downtown Club",
"Original home of the Heisman Trophy"));

populate();
}

private GeoPoint getPoint(double lat, double lon) {
return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
}

@Override
protected OverlayItem createItem(int i) {
return (items.get(i));
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);

boundCenterBottom(marker);
}

@Override
public int size() {
return (items.size());
}

@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
final int action = event.getAction();
final int x = (int) event.getX();
final int y = (int) event.getY();
boolean result = false;

if (action == MotionEvent.ACTION_DOWN) {
for (OverlayItem item : items) {
Point p = new Point(0, 0);

map.getProjection().toPixels(item.getPoint(), p);

if (hitTest(item, marker, x - p.x, y - p.y)) {
result = true;
inDrag = item;
items.remove(inDrag);
populate();

xDragTouchOffset = 0;
yDragTouchOffset = 0;

setDragImagePosition(p.x, p.y);
dragImage.setVisibility(View.VISIBLE);

xDragTouchOffset = x - p.x;
yDragTouchOffset = y - p.y;

break;
}
}
} else if (action == MotionEvent.ACTION_MOVE && inDrag != null) {
setDragImagePosition(x, y);
result = true;
} else if (action == MotionEvent.ACTION_UP && inDrag != null) {
dragImage.setVisibility(View.GONE);

GeoPoint pt = map.getProjection().fromPixels(x - xDragTouchOffset,
y - yDragTouchOffset);
OverlayItem toDrop = new OverlayItem(pt, inDrag.getTitle(),
inDrag.getSnippet());
Log.i("info",
"get lat:" + pt.getLatitudeE6() + " lang:"
+ pt.getLongitudeE6());
items.add(toDrop);
populate();

inDrag = null;
result = true;
}

return (result || super.onTouchEvent(event, mapView));
}

private void setDragImagePosition(int x, int y) {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) dragImage
.getLayoutParams();

lp.setMargins(x - xDragImageOffset - xDragTouchOffset, y
- yDragImageOffset - yDragTouchOffset, 0, 0);
dragImage.setLayoutParams(lp);
}
}

4.) MainActivity.java


package com.example.draganddropbasic;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.ImageView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class MainActivity extends MapActivity {
private MapView map = null;
private MyLocationOverlay me = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


map = (MapView) findViewById(R.id.map);

map.getController().setCenter(
getPoint(40.70686417491799, -74.01572942733765));
map.getController().setZoom(17);
map.setBuiltInZoomControls(true);

ImageView dragImage = (ImageView) findViewById(R.id.drag);
Drawable marker = getResources().getDrawable(R.drawable.marker);

marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());

map.getOverlays().add(new SitesOverlay(marker, dragImage, map));

me = new MyLocationOverlay(this, map);
map.getOverlays().add(me);
}

@Override
public void onResume() {
super.onResume();

me.enableCompass();
}

@Override
public void onPause() {
super.onPause();

me.disableCompass();
}

@Override
protected boolean isRouteDisplayed() {
return (false);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_S) {
map.setSatellite(!map.isSatellite());
return (true);
} else if (keyCode == KeyEvent.KEYCODE_Z) {
map.displayZoomControls(true);
return (true);
}

return (super.onKeyDown(keyCode, event));
}

private GeoPoint getPoint(double lat, double lon) {
return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
}

}

5.) activity_main.xml


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

< com.google.android.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:apiKey="0mjl6OufrY-tHs6WFurtL7rsYyEMpdEqBCbyjXg"
android:clickable="true" />

< ImageView
android:id="@+id/drag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/marker"
android:visibility="gone" />

< /RelativeLayout>

6.) Now run this and you get the desired result.

Google launched Zopfli to compress data for making webpages load faster

Google launched Zopfli Compression Algorithm, a new open sourced general purpose data compression library.

It is a new deflate compatible compressor that was inspired by compression improvements developed originally for the lossless mode of WebP image compression. Being compatible with deflate makes Zopfli's output compatible with zlib. It uses a different and more effective algorithm to compress data that can compress web content about 3-8% smaller compared to zlib at maximum compression.

Due to the amount of CPU time required — 2 to 3 orders of magnitude more than zlib at maximum quality — Zopfli is best suited for applications where data is compressed once and sent over a network many times, for example, static content for the web. By open sourcing Zopfli, thus allowing webmasters to better optimize the size of frequently accessed static content, hope to make the Internet a bit faster for all of us. Most internet browsers support deflate decompression, and it has a wide range of other applications. Because Zopfli is compatible with the decompression algorithms that are already part of all modern web browsers, using Google's new algorithm and library on a server could lead to faster data transmission speeds and lower web page latencies, which would ultimately make the web a little bit faster.

Zopfli is written in C. It is bit-stream compatible with compression used in gzip, Zip, PNG, HTTP requests and others.

Table Sorting in HTML by Javascript

This JavaScript code can be used to convert tables in ordinary HTML into sort table ones by associating each title column with an onClick event to automatically sort the data rows. No additional coding is necessary. All you need to do is give your table an ID field, include the sorttable.js file in jsp page and call init in your document's onLoad method.

JSP Page Code


< %@page contentType="text/html" pageEncoding="UTF-8"%>
< !DOCTYPE html>
< html>
< head>
< meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
< script language="javascript" src="js/sort.js">< /script>
< title>JSP Page< /title>
< style type="text/css">
#tablebox th {
background: none repeat scroll 0 0 #7da35a;
border-bottom: 1px dotted #666;
border-left: 1px dotted #666;
color: #FFFFFF;
line-height: 25px;
padding: 4px;
white-space: nowrap;
}

#tablebox td{
border-bottom: 1px dotted #666666;
border-left: 1px dotted #666666;
line-height: 12px;
padding:4px;
line-height: 20px;
white-space: nowrap;
}

#tablebox tr:hover{
background:#9cba73;
}
#tablebox thead tr {
position: relative;
}
table.sortable td {
text-align: left;
padding: 2px 4px 2px 4px;
/* width: 100px;*/
border-style: solid;
border-color: #444;
}
table.sortable tr:hover{
background:#9cba73;
}
< /style>
< /head>
< body>
< table class="sortable" id="tablebox" border="1">
< thead>
< tr>< th>MAR-2013< /th>< th>FEB-2013< /th>< th>JAN-2013< /th>< th>DEC-2012< /th>< th>NOV-2012< /th>
< th>OCT-2012< /th>< th>SEP-2012< /th>< th>AUG-2012< /th>< th>JUL-2012< /th>< th>JUN-2012< /th>
< th>MAY-2012< /th>< th>APR-2012< /th>< th>TOTAL< /th>< /tr>
< /thead>
< tbody>
< tr>< td>2146< /td>< td>1853< /td>< td>1718< /td>< td>2297< /td>< td>1909< /td>< td>1562< /td>< td>1500< /td>
< td>1555< /td>< td>1456< /td>< td>1554< /td>< td>1475< /td>< td>1582< /td>< td>20607< /td>< /tr>
< tr>< td>2389< /td>< td>1958< /td>< td>1947< /td>< td>2652< /td>< td>2129< /td>< td>1763< /td>< td>1778< /td>
< td>1788< /td>< td>1645< /td>< td>1822< /td>< td>1738< /td>< td>1841< /td>< td>23450< /td>< /tr>
< tr>< td>2616< /td>< td>2278< /td>< td>3392< /td>< td>2941< /td>< td>2226< /td>< td>2134< /td>< td>2115< /td>
< td>2328< /td>< td>2165< /td>< td>2186< /td>< td>2068< /td>< td>2134< /td>< td>28583< /td>< /tr>
< /tbody>
< /table>
< script type="text/javascript">
sorttable.init('Start');
< /script>
< /body>
< /html>

Download Sort.JS

Screenshots

Table

Table

After Sorting

After Shorting

Setting up Password less or Passphrase less SSH/SCP

SSH uses a public key cryptosystem. That means that, among other things, it can present a secret signed with your secret key, which anyone can decrypt with your public key, in order to verify that you are really who you say you are, assuming no one has stolen your private key.

There are two main ways of setting up ssh authentication in such a way that you do not have to enter a password or passphrase to log into machines, but without sacrificing security overmuch. You can set up ssh keys without a passphrase, or you can set up ssh keys with a passphrase and then use ssh agent to enter your passphrase automatically. What we're doing below, is generating a public+privatekeypair, and then adding the public key to a file that lists public keys that are allowed to ssh into the account the key is added to.

1. srchost> cd .ssh
2. srchost>ssh-keygen –t rsa



# Note: this step is not necessary if ~/.ssh/id_rsa.pub already exists

Example:


Setting up Password

3. srchost> # when ssh-keygen asks for a passphrase, just hit enter.

Example:


Setting up password

4. srchost>scp id_rsa.pub remote:/tmp/id_rsa.pub


Setting up password

5. dsthost> cd .ssh

6. dsthost> cat id_rsa.pub >authorized_keys

Scheduling shell Script to check memory usage & automated email notification

1)Create a .sh file


[root@ystbckp ~]# vim script_memory.sh

2)Write a the following script in the script_memory.sh file


Schedulng a shell

3)Change the permission to the script_memory.sh file


[root@ystbckp ~]# chmod 744 script_memory.sh

4)Scheduling the file to execute on a particular time


[root@ystbckp ~]# crontab -e

Each entry in a crontab file consists of six fields, specified in the following order

Minute hour day month weekday command
05 * * * * sh /root/script_memory.sh

When an asterisk (*) is displayed, it means all possible values for the field. For example, an asterisk in the hour time field would be equivalent to "every hour"

Browse existing image or capture new image in android

In this we will learn about how to select exist image in gallery . And In this we will learn about how to capture image from the android device and save into internal memory of android device. In this we do not need external memory or sdcard for capture image.


1.) Intent browsePicture = new Intent();
browsePicture.setType("image/*");
browsePicture.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(browsePicture, "Select Picture"),SELECT_PICTURE);

by calling this intent it opens options to the user to select existing image from gallery and etc.

2.) Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

by calling this intent it opens camera of android devices.


3.) FileOutputStream fos =openFileOutput("demo.jpg",Context.MODE_WORLD_WRITEABLE);
fos.close();
File f = new File(getFilesDir() + File.separator + "demo.jpg");

Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(takePicture, CAPTURE_PICTURE);

In this we write captured images into internal memory in android devices.


4.) String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null,
null);
if (cursor != null) {
// HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
// THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;

In this we get the path of saved capture image.