Skip to main content

Start an android activity from cordova plugin

We all need to start an android activity from a cordova plugin. Here is a small and complete code to start an activity from cordova plugin

1. Install plugman to create cordova plugin
npm install -g plugman
2. Create cordova plugin using plugman
plugman create --name PluginName --plugin_id com.example.sample.plugin --plugin_version 0.0.1

N.B : plugin id never start with Uppercase
Now PluginName directory will be created. Plugin structure will be

> PluginName/

> |- plugin.xml

> |- src/

> |- www/PluginName.js

3. Add android platform to plugin
plugman platform add --platform_name android

Now plugin structure will be

> PluginName/

> |- plugin.xml

> |- src/android/

> |- www/PluginName.js

4. Now create a java file named in src/android diretory

This activity will be shown using our plugin. Write sample activity in

package com.example.sample.plugin;
import android.os.Bundle;
public class NewActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        String package_name = getApplication().getPackageName();
 setContentView(getApplication().getResources().getIdentifier("activity_new", "layout", package_name));
5. Now create layout file activity_new.xml in src/android diretory 

This is the layout file for our new activity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
         android:text="New Activity"
         android:layout_marginTop="77dp" />

6. Now edit in src/android 

Now we need to handle the request and start our new activity.

package com.example.sample.plugin;
import android.content.Context;
import android.content.Intent;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaInterface;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class PluginName extends CordovaPlugin {
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        Context context = cordova.getActivity().getApplicationContext();
        if(action.equals("new_activity")) {
            return true;
        return false;
    private void openNewActivity(Context context) {
        Intent intent = new Intent(context, NewActivity.class);

7. Now edit PluginName.js in www directory 

Now create new method to call to start our new activity.

var exec = require('cordova/exec');
function plugin() {
plugin.prototype.new_activity = function() {
 exec(function(res){}, function(err){}, "PluginName", "new_activity", []);
module.exports = new plugin();

8. Now edit plugin.xml

Now we need to specify our files to plugin and make necessary changes in cordova AndroidManifest.xml file

<?xml version='1.0' encoding='utf-8'?>
<plugin id="com.example.sample.plugin" version="0.0.1"
    <js-module name="PluginName" src="www/PluginName.js">
    <clobbers target="PluginName" />
    <platform name="android">
    <config-file parent="/*" target="res/xml/config.xml">
    <feature name="PluginName">
    <param name="android-package" value="com.example.sample.plugin.PluginName" />
    <config-file target="AndroidManifest.xml" parent="/manifest/application">
       <activity android:label="New Activity" android:name="com.example.sample.plugin.NewActivity"></activity>
    <config-file parent="/*" target="AndroidManifest.xml"></config-file>
    <source-file src="src/android/" target-dir="src/com/example/sample/plugin" />
    <source-file src="src/android/" target-dir="src/com/example/sample/plugin" />
    <source-file src="src/android/activity_new.xml" target-dir="res/layout"/>

9. Now create a cordova project
cordova create CordovaProject com.example.sample.cordovaproject "Cordova App"

10. Add android platform to your cordova project
cordova platform add android

11. Now add your plugin
cordova plugin add your-plugin-local-patheg: cordova plugin add "C:\PluginName"

12. Add a button to index.html in www directory
<!DOCTYPE html>
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Hello World</title>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            <button id = "new_activity">New Activity</button>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
13. Add click handler for new button in index.js in www/js directory 

When our button is clicked, we will call our plugin method to start our new activity
var app = {
    initialize: function() {
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    onDeviceReady: function() {
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');
        console.log('Received Event: ' + id);
        document.getElementById("new_activity").addEventListener("click", new_activity);
function new_activity() {

14. Now run this app in android phone
cordova run androidIf all these steps are success and when we click New Activity button, our new activity will show up. 


  1. Thanks for the useful Information



  2. Nice article.
    bigo live is great app. it helps users to relax, connect friends.
    Download and install the application here

  3. Nice Article...NICE GAME Thanks for the sharing..

    Bigo live is a Wonderfull APK. it helps users to relax, connect friends.
    Download and install the application here

    Bigo Live Mac PC

    Wonderful Platform

  4. Nice Article. Thank you for sharing the informative article with us. Click the below link to know more about
    PlayBox Online,

    PlayBox Online,

    PlayBox APP for Android,

    PlayBox APP for iOS,

    PlayBox Online,

  5. Thank you so much for the inclusion nice article and very informative.
    BIGO LIVE is a cross-platform application, which has the ability to work on all sort of modern platforms.Here you have much freedom to share anything as like as you share on your smartphone.
    Click Here:
    Bigo Live Connector
    Bigo Live Login
    bigo live pc connector
    BIGO LIVE PC Download
    Bigo Live Login
    Bigo live Android App
    bigo live for pc

  6. I read your post and got it quite informative. I couldn't find any knowledge on this matter prior to. I would like to thanks for sharing this article here. Webview Set Up Online

  7. Hello, I would like to appreciate your work. It’s a great platform got to learn a lot. Your data is really worthy. Thank you so much. Keep it up!

     Playbox App For Android

     Playbox Download Android

  8. Great Post! thanks for sharing useful information with us. If you want to know more about this topic, then click on the below links.
    Thank You!
    Playbox Online
    PlayBox Online APK

  9. I'm new in ionic want to more specific how to call newActivity noot working from index and do we need cordovaproject separate in ionic ...i m using ionic 5.4.11


Post a Comment

Popular posts from this blog

Setup Asterisk 13 with FreePBX 13 in CentOS 7

Launch CentOS 7 AWS Ec2 InstanceLog in to your aws consoleGo to ec2 management page and click Launch Instance on Instance pageIn Choose AMI step, go to AWS MarketPlace tab and search CentOS 7 on search field. List of centos 7 ami's will be available.Select CentOS 7 (x86_64) ami which is free tier eligible. Select your instance type and click configure instance. Keep default values from Configure Instance to Add Tags stepsIn configure security group, create a security group which allow minimum ports openSSH : port 22HTTP : port 80Custom UDP Rule : 10000-20000 (if you are giving RTP ports 10000-20000)Custom UDP Rule : 5060 (ChanSIP port)Custom TCP Rule : 5060 (ChanSIP Port)Click Launch Instance. Select your key file and accept terms to launch instance.
Setup CentOS Server SSH into newly created CentOS server with username centos and your key file.Update all existing packagessudo yum update -yNow start http service(you can access your server via browser by going to your server ip addres…

Implementing Client Side WebRTC using Sipml5 javascript

Step 1:

Download and require Sipml5 library function.

Step 2:

Initialize sipml5 Engine in your web page :

var readyCallback = function(e) {
// function called when sipml is successfully initialised.
createSipStack(); // calling this function to create sip stack(see below)

var errorCallback = function(e) {
// function called when error occured during sipml initialisation.

SIPml.init(readyCallback, errorCallback);

Step 3:

Create Sip Stack :-
Sip Stack is an object that must be created before making/receiving call and sms. Creating Sip stack is an asynchronous process, so you need to create an event listener function to get state change notification.

var sipStack;

function EventListener(e) {

* e.type ;type of event listener
* e.session ; current event session
* e.getSipResponseCode() ; event response code
* e.description ; event description

if(e.type == 'started'){
// successfully started the stack.
} else if(e.type == 'i_new_call'){
// when new incoming call comes.