22 November 2014

Generate webapp using maven archetype plugin

In this example describes how to create web app using maven archetype plugin.And here i used jetty server. First You have to install maven in your computer. http://mavenforu.blogspot.com/2013/09/maven-installation-guide.html in here i have explain how to install maven in windows.So then go to your workspace and type this command.

mvn archetype:generate -DgroupId=com.sajith.maven -DartifactId=MyFirstWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
GroupId and DartifactId can be change to your package name and project name.

Then Change the POM file and add the jetty maven plugin.Inside the Plugins notation add these plugin.
<plugin>
     <groupId>org.mortbay.jetty</groupId>
     <artifactId>maven-jetty-plugin</artifactId>
     <version>6.1.10</version>
     <configuration>
     <scanIntervalSeconds>10</scanIntervalSeconds>
     <connectors>
     <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
          <port>8080</port>
          <maxIdleTime>60000</maxIdleTime>
     </connector>
     </connectors>
     </configuration>
 </plugin>
Then Enter this command
mvn clean install
mvn jetty:run

How to use maven for Android


If you are Android developer you must used some development IDE(eclipse, idea) for developingandroid application.

But when you want to build the APK file you no need to use IDE. So Maven introduce the maven build plugin for android.

Here i have describe how to install maven(http://sajithforu.blogspot.com/2014/10/maven-installation-guide.html).

First you need android keystore for verify the apk file.So this will help you to how to create keystore in android.(http://developer.android.com/tools/publishing/app-signing.html).

First you have to install android sdk development kit in your machine.Then First set ANDROID_HOME path variable in your machine.



Second step genarate the keystore set KEYSTORE_HOME path variable in your PC.




Third create your android application using any IDE.

Inside the project create pom.xml file and put this code. and change the  App_name and relevant changes.

Make sure you have correct platform version



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>App</groupId>
        <artifactId>modules</artifactId>
        <version>1.0.8</version>
    </parent>
 
    <groupId>App</groupId>
    <artifactId>App</artifactId>
    <version>1.0.8</version>
    <packaging>apk</packaging>
    <name>App-name</name>
 
    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>${platform.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
        </dependency>

        <dependency>
            <groupId>org.roboguice</groupId>
            <artifactId>roboguice</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>App</finalName>
        <sourceDirectory>src/main/java</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jarsigner-plugin</artifactId>
                <version>1.2</version>
                <executions>
                    <execution>
                        <id>signing</id>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                        <phase>package</phase>
                        <inherited>true</inherited>
                        <configuration>
                            <includes>
                                <include>target/*.apk</include>
                            </includes>
                            <keystore>${env.KEYSTORE_HOME}/android-pos.keystore</keystore>
                            <storepass>keystorepassword</storepass>
                            <keypass>keypass</keypass>
                            <alias>your_alias</alias>
                            <verbose>true</verbose>
                            <arguments>
                                <argument>-sigalg</argument>
                                <argument>MD5withRSA</argument>
                                <argument>-digestalg</argument>
                                <argument>SHA1</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <release>true</release>
                    <sdk>
                        <platform>17</platform>
                        <path>${env.ANDROID_HOME}</path>
                    </sdk>
                    <sign>
                        <debug>false</debug>
                    </sign>
                    <extractDuplicates>true</extractDuplicates>
                    <proguard>
                        <skip>false</skip>
                        <config>proguard.cfg</config>
                        <configs>
                            <config>${env.ANDROID_HOME}/tools/proguard/proguard-android.txt</config>
                        </configs>
                        <filterMavenDescriptor>false</filterMavenDescriptor>
                        <filterManifest>false</filterManifest>
                        <jvmArguments>
                            <jvmArgument>-Xms256m</jvmArgument>
                            <jvmArgument>-Xmx512m</jvmArgument>
                        </jvmArguments>
                    </proguard>
                </configuration>
                <extensions>true</extensions>
            </plugin>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>maven-android-plugin</artifactId>
                <configuration>
                    <zipalign>
                        <verbose>true</verbose>
                        <skip>false</skip>            
                        <inputApk>${project.build.directory}/${project.artifactId}-${project.version}.apk</inputApk>
                        <outputApk>${project.build.directory}/${project.artifactId}-${project.version}-RELEASE.apk>
                        </outputApk>
                    </zipalign>
                    <manifest>
                        <debuggable>false</debuggable>
                        <versionCodeAutoIncrement>true</versionCodeAutoIncrement>
                    </manifest>
                    <proguard>
                        <skip>true</skip>
                    </proguard>
                </configuration>
                <executions>
                    <execution>
                        <id>zipalign</id>
                        <phase>install</phase>
                        <goals>
                            <goal>zipalign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <configuration>
                    <reportPlugins>
                        <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>findbugs-maven-plugin</artifactId>
                        </plugin>
                    </reportPlugins>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


Then go to the project and run these maven cvommands to compile and run the project.
         mvn clean install
         mvn android:compile
         mvn android:run

And that is all.Thanks

19 November 2014

How to access global variable in android all activities

Hi in this post i will describe how to use global variable to access all the activities in android application.First you have to define application class in manifest file.


<application android:label="@string/app_name" 

     android:icon="@drawable/logo"

     android:name="com.homesystem.net.CustomApplication">

</application>

CustomApplication class is a class which used to store and access global variables.

public class CustomApplication extends Application {

    public static String message = "";

    public static boolean state =true;

    private static CustomApplication singleton;

    public static CustomApplication getInstance() {

        return singleton;

    }

    @Override
    public void onCreate() {
        super.onCreate();
        singleton = this;
    }


    }


And then inside any activity you can update and real values in CustomApplication class.

String Message = CustomApplication .message;
boolean state=CustomApplication .state;


Java JDBC connection using maven

In this article I will describe how to create jdbc connection and read values from mysql database.

First In your POM file add the following dependency.

<dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>5.1.17</version>
     <type>jar</type>
     <scope>compile</scope>
 </dependency>

Then From your java application you can check the Jdbc connection.In my application i have database called java_form  and table called user_details.

In here i have First check the database connection.And next method insert data to database. Then Retrieve data from database after all  close the connection.

public class DataBaseConnectivity {
    Connection connection;

    /**
     *  Connect database
     * @return
     */
    public boolean connect_database() {
        boolean check_connection = false;
        String database_Url= "jdbc:mysql://localhost/java_form";
        String database_Class= "com.mysql.jdbc.Driver";        
        String username = "root";
        String password = "";
        try {
            Class.forName(database_Class);
          connection = DriverManager.getConnection(database_Url, username, password);          
           
            if (connection != null) {
                check_connection = true;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return check_connection;
    }

    /**
     *  Insert DATA to table
     * @param uId
     * @return
     */
    public boolean insert_uId(String uId) {
        String query = "INSERT INTO user_details (uID)VALUES (" + uId + ")";
        Statement statement = null;
        try {
            statement = connection.createStatement();
            int resultSet = statement.executeUpdate(query);
            System.out.println("UID added to database");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return true;
    }

    /**
     *   Retrive data from table
     * @return
     */
    public boolean retrive_Data() {
        String query = "Select uID from user_details";

        try {
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(query);
            while (resultSet.next()) {
                String tableName = resultSet.getString(1);
                System.out.println("UID value : " + tableName);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return true;
    }
}

Get Text from Listview when Listview clicked

This is simple question.But today i spent lot of time to search get text from Listview . Here i will share it for you.

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView<?> parent, View view,

                                    int position, long id) {

                TextView textview= (TextView) view.findViewById(R.id.label);

                String profileName = textview.getText().toString();


            }

        });



10 November 2014

How Send SMS in android Pragmatically

This is very simple.So There are two type to send SMS.First one is SMS manager API and second one is Build-in sms application.In here i have only explain how to send SMS via SMS manager API.
This is the class which support to send SMS.when we using SMS manager API we can't see any UI level change when sending message.

import android.content.Context;

import android.telephony.SmsManager;

import android.widget.Toast;

public class SendSms {

    Context context;

    public SendSms(Context context){

      this.context=context;

    }

    String phoneNo="telnumber(+9471 1111111)";

    public void SendSms(String sms){

        try {

            SmsManager smsManager = SmsManager.getDefault();

            smsManager.sendTextMessage(phoneNo, null, sms, null, null);

            Toast.makeText(context, "SMS Sent!",

                    Toast.LENGTH_LONG).show();

        } catch (Exception e) {

            Toast.makeText(context,

                    "SMS faild, please try again later!",

                    Toast.LENGTH_LONG).show();

            e.printStackTrace();

        }

    }

}

And make sure you have add SMS send uses-permission.
 <uses-permission android:name="android.permission.SEND_SMS" />

And happy coding..

How to receive SMS in android BroadcastReceiver

In this post i will tell you how to receive SMS in your application.If your application need to receive sms from specific number this is the way we want to do.First you need to BroadcastReceiver to receive sms from background.
    First you need to add uses-permission to READ SMS.

    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

Then This is the code line for receive SMS.

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.telephony.SmsManager;

import android.telephony.SmsMessage;

import android.util.Log;

import android.widget.Toast;

public class ReceiveSms extends BroadcastReceiver {

    final SmsManager sms = SmsManager.getDefault();

    public void onReceive(Context context, Intent intent) {      

        final Bundle bundle = intent.getExtras();

        try {

          if (bundle != null) {

           final Object[] pdusObj = (Object[]) bundle.get("pdus");

          for (int i = 0; i < pdusObj.length; i++) {

        SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);

        String phoneNumber = currentMessage.getDisplayOriginatingAddress();

        String senderNum = phoneNumber;

        String message = currentMessage.getDisplayMessageBody();

        int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(context,

                            "senderNum: "+ senderNum + ", message: " + message, duration);

                    toast.show();
                }
            }

        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" + e);

        }

    }

}


And Finally you have to Add your receiver in manifest file(Inside application tags).You must be add android:priority="500" other wise it will not work.

<receiver android:name="com.example.HomeControlSystem.ReceiveSms">
      <intent-filter android:priority="500">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
      </intent-filter> </receiver>

Happy coding..

02 November 2014

Begin with bootstrap

Here i have started twitter bootstrap web application development.In here i will talk basic stuff to build your own responsive web application.First thing first.You need to know what is twitter bootstrap.It is world famous responsive web development framework.First you have to download bootstrap from this link. [http://getbootstrap.com/customize/?id=ecab5932bfb5dc96278d].First unzip the file and Then you need to add index.html file to home folder.

You can use template html file to index.html file.So this is the code.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap 101 Template</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">  
  </head>
  <body>
    <h1>Hello, world!</h1>
    <a href="" class="btn" >Hello World</a>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
 
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>


Then here on words i will talk what are the components one by one.So first look at how to add button and change color and size of button.There are free-defined css type for buttons.

Buttons

There are seven types of buttons.

  <button type="button" class="btn btn-default">Default</button>
  <button type="button" class="btn btn-primary">Primary</button>
  <button type="button" class="btn btn-info">Info</button>
  <button type="button" class="btn btn-success">Success</button>
  <button type="button" class="btn btn-warning">Warning</button>
  <button type="button" class="btn btn-danger">Danger</button>
  <button type="button" class="btn btn-link">Link</button>

When we need to change size of button this also have free-defined sizes.