Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Aug 12, 2018

Running Ionic 4 Application On Android Device

Hi Guys, hope you all followed my previous article about Getting Started With Ionic. Lets see how we can test this simple application in an Android Device.

First of all what you have to do is setup the environment. 

Step 1 : Install Java Development Kit. I have an bit old article about setting up the java environment.


Step 2 : We should install the Android SDK. You can simply download and install Android Studio.


Step 3 : In order to test and configure it on Android Device, you may need to configure Gradle Daemon. Without going into much details or over confusing you can simple achieve this by creating a sample android project in Android Studio and adding few target SDks.


Step 4 : Run the below command to add the android platform.


1
ionic cordova platform add android

This command will take few minutes depending on your internet connection. if everything goes correct you will get below output.



Step 5 : Lets build it for Android using below command.


1
ionic cordova build android

If everything goes correct you will get below output.






Step 6 : That's all you have to do. Copy the APK file to Mobile Device from the folder location shown in the output. 


Step 7 : Click on the APK file in the mobile device. You will get the below message, because we are trying install an APK which is not fully trusted APK with google agreements. For now we only need to run this on our device. In my case I'm trying it on my Samsung Galaxy S6.





Step 8 : Click Settings.





Step 9 : Then click on the Unknown Source Toggle button.





Step 10 : Then press OK




Step 11 : Then Press Install



If everything went well you will get the following message.


Step 12 : Then click open. You will see your application in mobile.



Hope you follow it up. Read my next article about Working With Ionic Modal Popup.

Getting Started With Ionic 4

In this article I will be covering a series of articles for you to develop an Ionic Application and Publish it Google Play Store. The current version of Ionic is 4. Lets get started with Hybrid Mobile Application using Ionic. Hope you know what is Ionic and how it works. First of all you need to setup the environment for IONIC development.

Step 1 : Install latest version of Node if you have not installed. You can check your node version using below command in CMD.

1
node -v

Step 2 : Download a code editor for development. I use VS Code. Which is an open source code editor.

Step 3 : Install Ionic CLI globally using below command.


1
npm install -g ionic@latest

Step 4 : Lets create an Ionic App now. There are 3 main options while creating an Ionic Application.
You can create either Blank Application or Tabs Application or Side Menu Application. First Lets create Side Menu Application using below command.


1
ionic start SideMenuApp sidemenu

When you execute this command you will get something like this.



Its basically asking you if you want to create a mobile application (android and/or iOS). If that's your intention, then hit the y key, followed by enter. It will then ask you which platforms you want to install, type a platform name from the options and hit the enter key again.

If your intent is not to create a mobile app, then hit the n key, followed by enter and you'll be on your way to developing a single page, mobile first application.

So lets Enter "N" and Proceed.

Step 5 : Go to your newly created folder and run the Application using below commands.


1
2
cd SideMenuApp
ionic serve

You will see get the following output in the Command Prompt.


Then application will open in the browser.



Hope you follow it up. Lets see how we can run this on a device in my next article.

Jul 25, 2014

Android Backgound Processing Service With Timer Task

This Example Show How To Use Services In Android With Timer Task.Our Main Task In This Example Is Start A Service and Run The Service In A Timer Task.Basically We Are Triggering The  Service In A Time Interval.A Service Is A Component That Runs In The Background To Perform Long-Running Operations Without Any Interact With The User.

First Of All We Need To Create A New Android Application Project.In My Case I'm Calling My Application As 'BackgroundService'.The Start up Window Appear As Below.





After Creating The Application We Have To Design Our Layout With Two Buttons.In My Case Im Calling My Buttons As 'Start Service' & 'End Service'.In Order To Do That Open The 'activity_main.xml' File.Replace The File With Following Codes.



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

   <Button android:id="@+id/btnStartService"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/start_service"
    android:onClick="startService"/>

   <Button android:id="@+id/btnStopService"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/stop_service"
    android:onClick="stopService" />

</LinearLayout>


After Adding This Codes You Will Get Few Errors.Don't Worry About It.Errors Are Coming Because Because We Didn't Add The Strings to strings.xml File.In Order To Do That Go To res --> values --> strings.xml And Add The Two Strings As Follows.


<resources>
    <string name="app_name">HelloWorld</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="start_service">Start Service</string>
    <string name="stop_service">Stop Service</string>
</resources>



Now We Have Our Layout and We Need To Create Service Class.In Order To Create Service Class Right Click The 'src' Folder & Go To New --> Class.It Will Open You A New Window To Create A New Class.Give The Class Name,Make The Class As Public Too.In My Case I'm Calling My Service Class As 'MyService'. 


package com.example.backgroundprocess;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {

   private static Timer timer = new Timer();
   private Context ctx;

   @Override
   public IBinder onBind(Intent arg0) {
      return null;
   }

   public void onCreate() {
      super.onCreate();
      ctx = this; 
      startService();
   }

   private void startService(){           
      timer.scheduleAtFixedRate(new mainTask(), 0, 5000);
   }

   private class mainTask extends TimerTask{ 
      public void run(){
         toastHandler.sendEmptyMessage(0);
      }
   }

   public void onDestroy() {
      super.onDestroy();
      Toast.makeText(this,"Service Distroyed",Toast.LENGTH_SHORT).show();
   }

   private final Handler toastHandler = new Handler(){
      @Override
      public void handleMessage(Message msg){
         Toast.makeText(getApplicationContext(),"Service Started",Toast.LENGTH_SHORT).show();
      }
   };

}


Now We Have To Call This Service To Start.In Order To Do That We Have To Add Few Code Lines To 'MainActivity.java'.


package com.example.backgroundprocess;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

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

    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    // Method to start the service
    public void startService(View view) {
       startService(new Intent(getBaseContext(), MyService.class));
    }

    // Method to stop the service
    public void stopService(View view) {
       stopService(new Intent(getBaseContext(), MyService.class));
    }
}

Change Manifest To Identify The Service.Add Following Code In AndroidManifest File In Between "</activity>" &  "</application>" Tags As Follows.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.backgroundprocess"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="9" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.backgroundprocess.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyService" />
    </application>

</manifest>


Now We Have Completed Our Coding Parts.Now Run The Application & See.After Clicking 'Start Service' You Will Get The Out Put As Below.


Every 5 Seconds You Will Get This Toast.Now Press The 'Stop Service' Button.You Will Get The Following Output.


Now You Can Use This Is Simple Application As A Component To Your Application. 
For Your Knowledge : You Can Start A Services Many Time Without Even Stopping It.But You Can Only Stop A Service If Their Is A Running Service Only.
 


Jun 12, 2014

Android Backgound Processing Service

This Example Show How To Use Services In Android.Our Main Task In This Example Is Start A Service and End A Service.A Service Is A Component That Runs In The Background To Perform Long-Running Operations Without Any Interact With The User.

First Of All We Need To Create A New Android Application Project.In My Case I'm Calling My Application As 'BackgroundService'.The Start up Window Appear As Below.





After Creating The Application We Have To Design Our Layout With Two Buttons.In My Case Im Calling My Buttons As 'Start Service' & 'End Service'.In Order To Do That Open The 'activity_main.xml' File.Replace The File With Following Codes.



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

   <Button android:id="@+id/btnStartService"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/start_service"
    android:onClick="startService"/>

   <Button android:id="@+id/btnStopService"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/stop_service"
    android:onClick="stopService" />

</LinearLayout>


After Adding This Codes You Will Get Few Errors.Don't Worry About It.Errors Are Coming Because Because We Didn't Add The Strings to strings.xml File.In Order To Do That Go To res --> values --> strings.xml And Add The Two Strings As Follows.


<resources>
    <string name="app_name">HelloWorld</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="start_service">Start Service</string>
    <string name="stop_service">Stop Service</string>
</resources>





Now We Have Our Layout and We Need To Create Service Class.In Order To Create Service Class Right Click The 'src' Folder & Go To New --> Class.It Will Open You A New Window To Create A New Class.Give The Class Name,Make The Class As Public Too.In My Case I'm Calling My Service Class As 'MyService'. 


package com.example.backgroundprocess;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {
   @Override
   public IBinder onBind(Intent arg0) {
      return null;
   }

   @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      // Let it continue running until it is stopped.
      Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
      return START_STICKY;
   }

   @Override
   public void onDestroy() {
      super.onDestroy();
      Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
   }
}


Now We Have To Call This Service To Start.In Order To Do That We Have To Add Few Code Lines To 'MainActivity.java'.


package com.example.backgroundprocess;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

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

    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    // Method to start the service
    public void startService(View view) {
       startService(new Intent(getBaseContext(), MyService.class));
    }

    // Method to stop the service
    public void stopService(View view) {
       stopService(new Intent(getBaseContext(), MyService.class));
    }
}

Change Manifest To Identify The Service.Add Following Code In AndroidManifest File In Between "</activity>" &  "</application>" Tags As Follows.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.backgroundprocess"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="9" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.backgroundprocess.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyService" />
    </application>

</manifest>


Now We Have Completed Our Coding Parts.Now Run The Application & See.After Clicking 'Start Service' You Will Get The Out Put As Below.






Now Press The 'Stop Service' Button.You Will Get The Following Output.





Now You Can Use This Is Simple Application As A Component To Your Application. 
For Your Knowledge : You Can Start A Services Many Time Without Even Stopping It.But You Can Only Stop A Service If Their Is A Running Service Only.

Android Hello World

Before Getting Into Android Development First You Must Setup The Environment For Android Development.Following Software Components Needed.
       
      1. Java Development Kit (JDK)
      2. Get The ADT Bundle Download

Extract ADT Bundle and Click Eclipse. It Will Open The Eclipse and Ask For A Work Space.



         Work Space is The Folder Where Your Project File Is Gonna Stored.You Can Select Any Folder As You Wish and You Can Change Your Work Space Anytime.In My Example I've Created A Folder Called Test In My D Drive I Selected That As My Work Space.After The Work Space Selection It Will Open The Eclipse Main Window For You.

To Create A New Android Project Go To File --> New --> Android Application Project
Then It Will Open A Window Like Below.


  

Now Specify Relevant Information In That Window.Application Name The Name Of The Application That Your Going To Create.In My Case I'm Going To Give As 'Hello World'.Then It Will Automatically Fill The Project Name and Package Name.

In The Minimum Required SDK Field We Have To Specify The Minimum Requirement To Run This Application In Android Based Mobile.In My Case I'm Selecting API Level 9.In The Target SDK  Field We Have To Specify The SDK Level In The Android Mobile.In The Compile With Field You Have To Select The API Level That You Have.In My Case I Only Have The API Level 19 Latest At The Moment.Finally It Will Apear Like Below.




Then Click The 'Next' Button.It Will Open Another Window.



Click The 'Next' Button Again.Then It Will Open Another Window.



Click The 'Next' Button Again.Then It Will Open Another Window.



Click The 'Next' Button Again.Then It Will Open Another Window.




Finally Click The 'Finish' Button.Then It Will Open The Application Window.




      To Test Our Application We Need Virtual Android Device, Or Else You Can Connect Your Android Mobile Phone Too.To Create A New Virtual Android Device Go To Window --> Android Virtual Device Manager.Then It Will Open A Window Like Below.



To Create A New Android Virtual Device Click The 'New' Button In The Right Corner.Then It Will Open Another Window Like Below.



Enter The Following Details To Create A New Device.


Then Click 'OK' Button.You Will End Up The Window Like Below.


Then Select The AVD Name and Click The 'Start' Button.



Now It Will Ask To Launch The Virtual Device.Click The 'Launch' Button.


It Will Start You A Virtual device Now.



Now To Test Our Application Follow The Below Steps.









Now Double Click The Android Application In The Right Side.


Browse The Project By Clicking the 'Browse' Button.


Now Press 'OK' Button.Click The 'Target Tab' To Select The Target Device.


Click The 'AVD Name Check Box' True and Click The 'Apply' Button.Then Click The 'Run' Button.This Only In First Time Configuration.In Next Time You can Do The Following Steps.



Now You Will See The Your Application Is Running In The Device.



If You Go To Application Window You Will See Our New Application Hello World Installed.




Now You May Got A Basic Knowledge About Android Application Development. Start Develop New Applications.

JWT Token Decode Using Jquery

When it come to authentication we use many mechanism. Ones the user authenticated we must keep these details somewhere safe. So we can share...