Can a native intent be triggered from an app enclosed within PhoneGap on Android?

Currently, I am in the process of creating an application using Sencha Touch 2.0.1 & PhoneGap. My main challenge is finding a way to capture and transfer events that are triggered within Sencha Touch to the native Android environment.

For instance, there are certain buttons on my Sencha Touch interface that need to trigger an intent upon being clicked, which will then initiate another activity (that is not related to PhoneGap).

Although I have come across examples like webintents and this, it seems that these solutions may not be applicable to my specific situation.

At this point, I am considering either abandoning PhoneGap and exploring alternative wrappers, or trying to find a workaround for this issue. Any advice or suggestions would be greatly appreciated. Thank you!

Answer №1

If you want to launch the native activity from your PhoneGap app, creating a custom plugin is necessary. You can take inspiration from the ContactView plugin as a reference for developing your own plugin.

For detailed guidance, check out the ContactView plugin at: https://github.com/phonegap/phonegap-plugins/blob/master/Android/ContactView/ContactView.java

Pay attention to these two key methods:

@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
    startContactActivity();
    PluginResult mPlugin = new PluginResult(PluginResult.Status.NO_RESULT);
    mPlugin.setKeepCallback(true);
    this.callback = callbackId;
    return mPlugin;
}

public void startContactActivity() {
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
    this.ctx.startActivityForResult((Plugin) this, intent, PICK_CONTACT);
}

Answer №2

Check out the explicit and implicit intent sections (1.2, 1.3) here:

Next, review the source code for WebIntent.java, specifically focusing on the startActivity function: https://github.com/phonegap/phonegap-plugins/blob/master/Android/WebIntent/WebIntent.java

void startActivity(String action, Uri uri, String type, Map<String, String> extras) {
  Intent i = (uri != null ? new Intent(action, uri) : new Intent(action));

Also, explore the intent constructors available by searching for Constructors here: http://developer.android.com/reference/android/content/Intent.html

WebIntent does not currently support the Intent constructor that includes an Android class.

However, you can modify the function to work with an explicit intent using the following untested code snippet:

void startActivity(String action, Uri uri, String type, String className, Map<String, String> extras) {
  Intent i;
  if (uri != null)
    i = new Intent(action, uri)
  else if (className != null)
    i = new Intent(this.ctx, Class.forName(className));
  else
    new Intent(action));

In the execute function, ensure to parse out the new parameter in the "Parse the arguments" section as well:

// Parse the arguments
JSONObject obj = args.getJSONObject(0);
String type = obj.has("type") ? obj.getString("type") : null;
Uri uri = obj.has("url") ? Uri.parse(obj.getString("url")) : null;
String className = obj.has("className") ? obj.getString("className") : null;
JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;

Then pass the new className string in the call to startActivity a few lines below like this:

startActivity(obj.getString("action"), uri, type, className, extrasMap);

Once done, you should be able to invoke an android activity through classname using a structure similar to:

Android.callByClassName = function(className) { 
  var extras = {};
  extras[WebIntent.EXTRA_CUSTOM] = "my_custom";
  extras[WebIntent.EXTRA_CUSTOM2] = "my_custom2";
  window.plugins.webintent.startActivity({
    className: className, 
    extras: extras 
  }, 
  function() {}, 
  function() {
    alert('Failed to send call class by classname');
  }
); 

};

Make sure the classname follows a format such as: com.company.ActivityName

DISCLAIMER: Code is raw and needs testing.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble accessing an element using jQuery(idOfElement) with a variable as the name parameter

Possible Duplicate: Issue with JavaScript accessing JSF component using ID Whenever I attempt to retrieve an element by passing a variable in jQuery(idOfElement), it doesn't work. But if I use something like jQuery('#e'), it works perfe ...

Anchor tag in AngularJS not responding to ng-disabled directive

I have been successfully using ng-disabled for input and buttons, but I have run into an issue with anchor tags. How can I make it work for anchor tags as well? HTML code <a ng-disabled="addInviteesDisabled()">Add</a> JS code $scope.addIn ...

Transform an SVG file into a PNG format using the image's path as the guideline

Can an SVG image be converted to the PNG format within an angular project? I came across a library called save-svg-as-png, but it requires the id of an svg canvas. However, in my code, I am using the img tag to display the SVG image: <img src="path/to ...

The functionality of Jade views is disrupted by external CSS files

I am new to node.js and apologize if this question seems too obvious. I have built my app logic with all the routes and views, and it was working fine until I tried to add more styles to my views using Jade in Express. The only change I made was including ...

Trigger an event, pause, and subsequently trigger another one within Vue

I have successfully emitted the following events to the parent component: this.$emit('sendToParent1', true); this.$emit('sendToParent2'); this.$emit('sendToParent3'); this.$emit('sendToParent4', true); this.$emit(&ap ...

Invoking a C# function inside a cshtml file

Having trouble calling a function in my CSHTML page. In the script of my CSHTML page : <script type="text/javascript" > //var sLoggedInUser = <%# GetSession_LoggedInIdUser() %>; $(document).ready(function () { ale ...

Angular animations: triggered by user interaction

I'm encountering some difficulties with animating elements in my Angular application. The issue involves a grid made up of cells (created using ng-repeat). What I'm aiming to achieve is a simple animation: when a cell is clicked, it should disapp ...

Ionic - Managing Large Image Galleries with Efficient Memory Usage

I've developed an app with Ionic that incorporates the latest angular 4 and ionic 3 versions. Within the app, there is a scrollable list displaying numerous large images. The issue arose on IOS due to memory crashes caused by the accumulation of the ...

Sequencing animations with *ngIf in Angular 6

In my component, there are two elements that utilize the same fade animation. Only one element is visible on screen at a time, controlled by *ngIf directives for visibility management. The animation is a simple fade in/fade out effect. When the state of m ...

Utilizing Vue.js pagination and table sorting with Element components

I am currently working on a project using Vue js, Element Plus Table, and Pagination. The issue I am facing is that all columns of the table are sortable, but the sorting only works on the current page. I need to be able to sort all my data across multiple ...

What is the best way to enforce a required bindings property in an AngularJS component?

Suppose I have the following component defined: angular.module('myApp').component('myComponent', { templateUrl: 'myComponent.html', bindings: { myPropOne: '<', myPropTwo: '<' ...

Comparing "if" and "else" within the XMLHttpRequest() function will not

function banUser() { var userToBan = document.getElementById('userToBan').value; var cid = document.getElementById('chatId').value; if (userToBan.length <= 0) { var x = document.getElementById("banerror"); x.innerHTML ...

Navigating the world of getElementById and addEventListener outside of the DOM

Having some dynamic HTML code in my JS, I'm hoping to assign an ID to a particular tag: content: ` <p id="openKeyboard"> If the click happens, I want to trigger an event. </p> ` However, upon ...

Sending a value to a text box using json data transmission

I am having trouble accessing data from a js file and fetching the value to an html text box. Despite trying, I'm unable to get the desired result. Below are the contents of samle.js file and jsonhtml.html file: { "var1": "1", "var2": "2" } <scri ...

Refresh the HTML content within a specified div element

In my index.html, there is a graph (created using d3.js) along with some code that displays a stepper with a number of steps equal to the child nodes of the clicked node: <div ng-include="ctrl.numlab==2 && 'views/stepper-two-labs.htm ...

The Objectid seems to be causing issues with the Mongodb find function

I've encountered an issue with my code where I am receiving a null value for the doc. Strangely, when I use findOne({'webpageid':docs[i]._id} and replace docs[i]._id with webpageid, it functions correctly. I have double-checked that docs con ...

Generating an interactive Datepicker using Jquery

How can I design a dynamic date picker similar to the image provided below? https://i.sstatic.net/euoNI.png I have attempted to create one, but I am looking for a more interactive date picker. Is there a way to achieve the design shown in the image? The ...

Difficulty with slideToggle and other jQuery animations arises when selecting specific elements

When elements are selected from the page using the following code: $('.offers')[count - 1].slideToggle(500); The slideToggle function stops working, along with any other animations. However, this code snippet does work: $('.offers')[ ...

Is there a way to upload several documents in just one writing?

Can anyone advise me on how to add multiple documents to my Firestore collection using just one write operation? I have over 20,000 records and I'm looking for a cost-effective solution. Is there a way to add multiple docs in a single write? Apprecia ...

Stop the npm start command with a specific error message if the file is not found in the repository

Can the npm start process be stopped or cancelled if a specific file is not found in your codebase? I am looking to end the process if the file dev-variables.js is missing, preferably displaying a custom error message like "You are missing the dev-variabl ...