Thursday, 27 February 2014

How to start development of html5 Apps using PhoneGap and Eclipse

Following are seven simple steps to write hello world project using HTML5

  • Download and install PhoneGap
  • Create a new Android App Project
  • Change the project from Java to Javascript
  • Prepare the Activity-Class to work with PhoneGap
  • Create a simple index.html file to run on the App
  • Prepare the Manifest-file before running it
  • To run the App 
Step 1 : Create Android  Project in Eclipse
Step 2 : First create a new folder called www and a new file called index.html. both in the project folder assets
Step 3 : Copy the Cordova.js file[Take from the phoneGap folder which we have installed] in assets folder and Cordova.jar[Take from the phoneGap folder which we have installed] file in the lib folder and xml [Take from the phoneGap folder which we have installed] folder in the values of our android project.
Step 4 : In the eclipse locate the src-folder and open the MainActivity.java class in it and change it that way you'll see bellow:

package com.example.phonegapexample;

import org.apache.cordova.DroidGap;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends DroidGap  {

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

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            super.loadUrl("file:///android_asset/www/index.html");
        }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Step 5 : Content of Index.html

<!DOCTYPE HTML>
<html>
  <head>
    <title>First App</title>
  <script src="cordova-2.2.0.js"></script>
  <script>
     function onLoad(){
          document.addEventListener("deviceready", onDeviceReady, true);
     }
     function onDeviceReady(){ navigator.notification.alert("PhoneGap is working!!");
     }
  </script>
  </head>
  <body onload="onLoad();">
       <h1>Welcome to PhoneGap</h1>
       <h2>Edit assets/www/index.html</h2>
  </body>
</html>
 
Step 6 : Prepare the Manifest-File before launching
 
 <supports-screens
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"
        android:xlargeScreens="true"
        android:resizeable="true"
        android:anyDensity="true"
        />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.RECORD_VIDEO"/>
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />   
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />   
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY" /> 

Step 7 : Running the App[Same procedure of how we run usual android App like Rus as Android Application]
 

Wednesday, 26 February 2014

How to get App name from Manifest

final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
    ai = pm.getApplicationInfo( this.getPackageName(), 0);
} catch (final NameNotFoundException e) {
    ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");

Tuesday, 25 February 2014

Read me Sample file

INTRODUCTION
------------
 - Step 1
 - Step 2
 - Step 3
 - Step 4
 
How It Works
------------
 - Step 1
 - Step 2
 - Step 3
 - Step 4

 +------------+++-----------------+
 |    Steps for Building the Code      |
 +------------+++-----------------+
 o Step 1
 o Step 2
 o Step 3
  - 3.1
    explanation
  - 3.2
    explanation
  - 3.3
    explanation


   










Sunday, 16 February 2014

Record screen action (capture video) on Android KitKat



Android KitKat provide a new feature to record screen action.

To record video on Android KitKat:

    Enable developer options on KitKat
    Connect your KitKat device to your PC.
    In your PC, run:
    $ adb shell screenrecord /sdcard/video.mp4

To stop recording:

    Press Control-C on your PC

Your recorded video will be in /sdcard/video.mp4 on your Android KitKat device.

Thursday, 13 February 2014

Reverse engineer your apk

You can also de-obfuscate  your apk using any of the available tools from google. What we demonstrate or work with is dex2jar. Please download this tool from http://code.google.com/p/dex2jar/. 

1.In command prompt navigate to dex2jar directory: Now type the following command. See the screen shot . This will change your apk file to .jar.

.Jar will be generated in your path where dex2jar software will be present
In this case (D:\sample\D-final\soft\dex2jar-0.0.9.15\dex2jar-0.0.9.15)

2. Once you have the .jar file, you need  jd-gui tool to view .jar . jd-gui is a graphical utility for viewing java source files. First download jd-gui here.
Open jd-gui.exe file-> click on open files. Now choose .jar file that you received from dex2jar tool. You are done, you can see following screen and browse through your java classes









In Command Prompt type the following

D:\Sample\D-final\soft\dex2jar-0.0.9.15\dex2jar-0.0.9.15>d2j-dex2jar.bat C:\Us
ers\sample\Desktop\All\BatteryUsage.apk




Automatically generates once you type enter
dex2jar C:\Users\KRN4BMH\Desktop\All\BatteryUsage.apk -> BatteryUsage-dex2jar.ja
r















Sunday, 9 February 2014

Some Basic Knowledge on NDK

How to generate wrapper files using swig :

1)Install Swig using the following link
http://www.swig.org/download.html

2)Copy in your folder and from cmd prompt go the location and type the following command
swig -c++ -java path

Where path indicates location of .i file[which is mandatory to generate wrapper classes]

3)Corresponding wrapper classes will be generated and then build using eclipse before building in eclipse  follow the below steps

4)Set Ndk path in window--->preferences --->Android--->Ndk

5)Go to project properties c/c++ build --->Tool chain Editor and there set CurrentBuilder as Android Builder.

6)Navigate to C/C++ build there in Builder Settings following should be mentioned

Build Directory should be : ${workspace_loc:/Project Name}/
Build Command : ndk - build

7)Build will be successful refer make file for any directory mismatch


One of the reason why I faced UnsatisfiedLinkError while trying to call native function is:

I have build JNIWrapper using the followinh Swig command

C:\swig\swigwin-2.0.11\swigwin-2.0.11>swig -c++ -java -package com.sampel D:\Pro
j-workspace\All_Projects\nativecodeproject\jni\API.i

All the wrappers got generated with the above package name.

I had a file with .cxx extension[which serves as a linkage between the Java and the Cpp code]
which plays a primary  role while invoking native functions.
This .cxx file is also generated by swig when I run the above command.

As per the make file Android makefile I shoul copy it and paste it in the target folder.
I missed out the above step and this lead to Error.

Note android make file instructions should be folloed carefully and .cxx file should be placed in their appropriate positions

Monday, 3 February 2014

Battery Usage

package com.example.batteryusage;

import android.os.BatteryManager;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;

/**
 * Used for finding the battery level of an Android-based phone.
 *
 *
 *
 */
public class BatteryLevelActivity extends Activity {
    /** Called when the activity is first created. */
    private TextView batterLevel;

    @Override
    /**
     * Called when the current activity is first created.
     */
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_main);
        batterLevel = (TextView) this.findViewById(R.id.batteryLevel);
        batteryLevel();
    }

    /**
     * Android 2.0 or higher
     * Computes the battery level by registering a receiver to the intent triggered
     * by a battery status/level change.
     *
     * BatteryManager.Extra_Scale
        Integer containing the maximum battery level.
        BatteryManager.Extra_Level
        Integer field containing the current battery level, from 0 to EXTRA_SCALE.
     */
    private void batteryLevel() {
        BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                context.unregisterReceiver(this);
                int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                int level = -1;
                if (rawlevel >= 0 && scale > 0) {
                    level = (rawlevel * 100) / scale;
                }
                batterLevel.setText("Battery Level Remaining: " + level + "%");
            }
        };
        IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryLevelReceiver, batteryLevelFilter);
    }
   
}

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...