• <strike id="8gw2s"></strike>
    ><\/span>\n<\/span> ><\/span>Tweets !!<\/h1<\/span>><\/span>\n<\/span>\n<\/body<\/span>><\/span>\n<\/span>\n<\/html<\/span>><\/span><\/span><\/pre>\n\n

    Now create a new file called script.js in the scripts folder and create the ajax call as shown:<\/p>\n\n

    
    	
    
    
    
    
    
    
    

    国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

    Table of Contents
    Key Takeaways
    Overview
    Creating Chrome App
    Creating a Chrome App for Android
    Conclusion
    Frequently Asked Questions (FAQs) about Running Chrome Apps on Mobile Device Using Cordova
    How can I install Cordova on my system?
    What are the prerequisites for running Chrome apps on a mobile device using Cordova?
    How can I convert my Chrome app into a Cordova project?
    How can I add platforms to my Cordova project?
    How can I build my Cordova project?
    How can I run my Cordova project on a device?
    How can I debug my Cordova project?
    What are the limitations of running Chrome apps on a mobile device using Cordova?
    Can I use Cordova plugins in my Chrome app?
    How can I update my Cordova project?
    Home Web Front-end JS Tutorial Running Chrome Apps on a Mobile Device Using Cordova

    Running Chrome Apps on a Mobile Device Using Cordova

    Feb 20, 2025 am 09:26 AM

    Running Chrome Apps on a Mobile Device Using Cordova

    Key Takeaways

    • Chrome apps can be run on Android and iOS devices using a toolset based on Apache Cordova, an open-source framework for packaging mobile apps using HTML, CSS, and JavaScript. Cordova wraps these web apps using the native shell, allowing for distribution on Google Play, The Apple App Store, and other stores.
    • The process of creating a Chrome app includes creating a project folder, defining required settings in a manifest.json file, creating a window for the app on launch, and setting up an AJAX call to retrieve data. This process was demonstrated with the creation of a simple Twitter Chrome app.
    • To run a Chrome app on Android, Java JDK 7 or higher, Android SDK 4.4.2 or higher, and Apache Ant need to be installed on the system. The cca command-line tool is also required. After setting up the environment, a new project can be created from the existing Chrome app to port to Android using specific commands.

    Chrome apps are popular among Chrome users. And why not, they provide a method of creating a portable ‘a(chǎn)pplication’ within the familiar environment of the Chrome browser.

    Imagine the power of Chrome apps available on your smartphone. We can now run our favorite chrome apps on Android and iOS using a tool set based on Apache Cordova.

    Cordova is an open source framework for packaging mobile apps using HTML, CSS and JavaScript. Cordova wraps the HTML, CSS and JavaScript web app using the native shell and allows for distribution on Google Play, The Apple App Store and other stores.

    Overview

    In this tutorial, we’ll create our own Chrome app using HTML, CSS and JavaScript and port it to the Android emulator. The application we’ll be creating will be a simple app to fetch the latest tweets based on a particular twitter hashtag.

    Creating Chrome App

    We’ll start by creating our Chrome app. Once we have create the Chrome app and tested it on the Chrome browser, we’ll port it to Android.

    Source code from this tutorial is available on GitHub.

    Create a project folder called TwitterChromeApp. Inside the project folder create a manifest file called manifest.json which will be the config file for our app. Inside manifest.json we’ll define a few settings required by the Chrome app. Chrome apps require manifest_version to be 2. We’ll also define the name of the app, its version and the path to a background script to execute on launching the app. We’ll be fetching the tweets from an external service url, so we’ll also specify that inside this file. Here is how the manifest.json should look:

    <span>{
    </span>    <span>"manifest_version": 2,
    </span>    <span>"name": "Tweet Chrome App",
    </span>    <span>"version": "1.0",
    </span>    <span>"app": {
    </span>        <span>"background": {
    </span>            <span>"scripts": ["scripts/main.js"]
    </span>        <span>}
    </span>    <span>},
    </span>    <span>"permissions": [
    </span>        <span>"http://twittersearchapi.herokuapp.com/search"
    </span>    <span>]
    </span><span>}</span>

    When the Chrome app launches we will show a window where the tweets from twitter will be displayed. Chrome apps have an event called onLaunched which we use to create the window for our app on app launch.

    Inside the project folder TwitterChromeApp, create another folder called scripts and inside it create the background script called main.js. Add the following code to main.js:

    <span>{
    </span>    <span>"manifest_version": 2,
    </span>    <span>"name": "Tweet Chrome App",
    </span>    <span>"version": "1.0",
    </span>    <span>"app": {
    </span>        <span>"background": {
    </span>            <span>"scripts": ["scripts/main.js"]
    </span>        <span>}
    </span>    <span>},
    </span>    <span>"permissions": [
    </span>        <span>"http://twittersearchapi.herokuapp.com/search"
    </span>    <span>]
    </span><span>}</span>

    Inside the onLaunched event we will create a widow for our Chrome app. Add the following code to main.js:

    chrome<span>.app.runtime.onLaunched.addListener(function() {
    </span>  <span>// creating window for app code will be here
    </span><span>});</span>

    In the above code, we have used the screen object to get the available screen width and height. Based on the actual width of the screen, we set the outer bounds of the new Chrome window to display correctly.

    chrome.app.window.create creates a new window using the html inside the file index.html.

    Inside the project folder TwitterChromeApp create a new file called index.html as shown below:

    chrome<span>.app.runtime.onLaunched.addListener(function() {
    </span>
        <span>var screenWidth = screen.availWidth;
    </span>    <span>var screenHeight = screen.availHeight;
    </span>    <span>var width = 500;
    </span>    <span>var height = 300;
    </span>
        chrome<span>.app.window.create('index.html', {
    </span>        <span>id: "tweetAppID",
    </span>        <span>outerBounds: {
    </span>            <span>width: width,
    </span>            <span>height: height,
    </span>            <span>left: Math.round((screenWidth - width) / 2),
    </span>            <span>top: Math.round((screenHeight - height) / 2)
    </span>        <span>}
    </span>    <span>});
    </span><span>});</span>

    Now try to install the Chrome app in your Chrome browser. Open to Tools -> Extensions (possibly More Tools). Enable developer mode from the checkbox in the top right and then click on Load unpacked extension and select the project folder.

    Run the extension either through the Apps or Extensions window and you should see the below:

    Running Chrome Apps on a Mobile Device Using Cordova

    Next, we’ll make create an ajax call that is triggered when the window launches that retrieves tweets from a service url. We’ll be using a service hosted on Heroku. The service has a few limitations. It only fetches tweets with the hashtag perkytweets, this is enough for our example.

    We’ll use jQuery to make our AJAX call, so download it to the scripts folder and include it in index.html, for example:

    <span><span><!DOCTYPE html></span>
    </span><span><span><span><html</span>></span>
    </span>
    <span><span><span><head</span>></span>
    </span>    <span><span><span><meta</span> charset<span>="utf-8"</span>></span>
    </span>    <span><span><span><title</span>></span>Chrome Tweet App<span><span></title</span>></span>
    </span><span><span><span></head</span>></span>
    </span>
    <span><span><span><body</span>></span>
    </span>    <span><span><span><h1</span>></span>Tweets !!<span><span></h1</span>></span>
    </span>
    <span><span><span></body</span>></span>
    </span>
    <span><span><span></html</span>></span></span>

    Now create a new file called script.js in the scripts folder and create the ajax call as shown:

    <span><span><span><script</span> type<span>="text/javascript"</span> src<span>="scripts/jquery-1.11.1.min.js"</span>></span><span><span></script</span>></span></span>

    Include script.js in index.html:

    <span>$(function() {
    </span>    $<span>.ajax({
    </span>        <span>type: 'GET',
    </span>        <span>url: 'http://twittersearchapi.herokuapp.com/search',
    </span>        <span>success: function(response) {
    </span>            <span>var resObj = JSON.parse(response);
    </span>            <span>console.log(resObj);
    </span>        <span>},
    </span>        <span>error: function(error) {
    </span>            <span>console.log(error);
    </span>        <span>}
    </span>    <span>});
    </span><span>});</span>

    Now open Tools -> extensions (Or More tools) and click reload to reflect the changes. Next click on the app to relaunch it. If you check the Chrome console (just the normal console you access in Chrome), it should show the response from the service URL.

    Next, we’ll display the responses in index.html. We’ll be using Bootstrap for styling the page.

    Create a styles folder inside your project folder and download the Bootstrap css files into the folder. Include the Bootstrap CSS file in index.html.

    <span><span><span><script</span> type<span>="text/javascript"</span> src<span>="scripts/script.js"</span>></span><span><span></script</span>></span></span>

    Include a ul element in index.html to display the tweets. Here is how index.html should look now:

    <span><span><span><link</span> href<span>="styles/bootstrap.min.css"</span> rel<span>="stylesheet"</span>></span></span>

    Inside the AJAX success call back in scripts.js include the following code to append the items fetched from the service call into the ul in index.html.

    <span><span><!DOCTYPE html></span>
    </span><span><span><span><html</span>></span>
    </span>
    <span><span><span><head</span>></span>
    </span>    <span><span><span><meta</span> charset<span>="utf-8"</span>></span>
    </span>    <span><span><span><title</span>></span>Chrome Tweet App<span><span></title</span>></span>
    </span>    <span><span><span><link</span> href<span>="styles/bootstrap.min.css"</span> rel<span>="stylesheet"</span>></span>
    </span>    <span><span><span><script</span> type<span>="text/javascript"</span> src<span>="scripts/jquery-1.11.1.min.js"</span>></span><span><span></script</span>></span> <!-- Check this matches your jQuery version and file name -->
    </span>    <span><span><span><script</span> src<span>="scripts/script.js"</span>></span><span><span></script</span>></span>
    </span>
    <span><span><span></head</span>></span>
    </span>
    <span><span><span><body</span>></span>
    </span>    <span><span><span><h1</span>></span>Tweets !!<span><span></h1</span>></span>
    </span>    <span><span><span><ul</span> id<span>="ulTweets"</span> class<span>="list-group"</span>></span>
    </span>
        <span><span><span></ul</span>></span>
    </span><span><span><span></body</span>></span>
    </span>
    <span><span><span></html</span>></span></span>

    Reload and relaunch the app and you should be able to see a screen full of tweets.

    Creating a Chrome App for Android

    Since we’ll be running our app on Android, make sure you have Java JDK 7 or higher, Android SDK 4.4.2 or higher and Apache Ant installed on your system.

    We’ll also need the cca command line tool. You can install it using

    <span>{
    </span>    <span>"manifest_version": 2,
    </span>    <span>"name": "Tweet Chrome App",
    </span>    <span>"version": "1.0",
    </span>    <span>"app": {
    </span>        <span>"background": {
    </span>            <span>"scripts": ["scripts/main.js"]
    </span>        <span>}
    </span>    <span>},
    </span>    <span>"permissions": [
    </span>        <span>"http://twittersearchapi.herokuapp.com/search"
    </span>    <span>]
    </span><span>}</span>

    Detailed information on setting up your environment for porting Chrome apps to Android and iOS can be found in the official docs.

    Once setting up our environment is complete, we’ll create a new project from our existing TwitterChromeApp to port to Android. Run the following command to create the project:

    chrome<span>.app.runtime.onLaunched.addListener(function() {
    </span>  <span>// creating window for app code will be here
    </span><span>});</span>

    Navigate to TwitterAppForAndroid and run the following command to run the project in the android emulator:

    chrome<span>.app.runtime.onLaunched.addListener(function() {
    </span>
        <span>var screenWidth = screen.availWidth;
    </span>    <span>var screenHeight = screen.availHeight;
    </span>    <span>var width = 500;
    </span>    <span>var height = 300;
    </span>
        chrome<span>.app.window.create('index.html', {
    </span>        <span>id: "tweetAppID",
    </span>        <span>outerBounds: {
    </span>            <span>width: width,
    </span>            <span>height: height,
    </span>            <span>left: Math.round((screenWidth - width) / 2),
    </span>            <span>top: Math.round((screenHeight - height) / 2)
    </span>        <span>}
    </span>    <span>});
    </span><span>});</span>

    Once the emulator successfully launches, you should see the app running inside the emulator.

    Conclusion

    In this tutorial, we saw how to create a simple chrome app and port it to the Android platform. Further information on running Chrome Apps on mobile devices using Apache Cordova can be found in the officials docs.

    What do you think about this Chrome App option for creating a mobile app? Does it offer any advantages over just using a standard HTML, CSS and JavaScript web app inside Cordova?

    Frequently Asked Questions (FAQs) about Running Chrome Apps on Mobile Device Using Cordova

    How can I install Cordova on my system?

    To install Cordova, you need to have Node.js installed on your system. Once Node.js is installed, you can install Cordova using npm (Node Package Manager) by running the command npm install -g cordova in your terminal or command prompt. The -g flag is used to install Cordova globally on your system.

    What are the prerequisites for running Chrome apps on a mobile device using Cordova?

    Before you can run Chrome apps on a mobile device using Cordova, you need to have the following installed on your system: Node.js, Cordova, Chrome Apps for Mobile toolchain, and Android SDK or iOS SDK depending on the platform you are targeting.

    How can I convert my Chrome app into a Cordova project?

    To convert your Chrome app into a Cordova project, you need to use the cca command followed by the create command and the name of your project. For example, cca create MyProject. This will create a new Cordova project in a directory named MyProject.

    How can I add platforms to my Cordova project?

    To add platforms to your Cordova project, you need to use the cordova platform add command followed by the name of the platform. For example, cordova platform add android will add the Android platform to your project.

    How can I build my Cordova project?

    To build your Cordova project, you need to use the cordova build command followed by the name of the platform. For example, cordova build android will build your project for the Android platform.

    How can I run my Cordova project on a device?

    To run your Cordova project on a device, you need to use the cordova run command followed by the name of the platform. For example, cordova run android will run your project on an Android device.

    How can I debug my Cordova project?

    To debug your Cordova project, you can use Chrome DevTools. To do this, you need to navigate to chrome://inspect in your Chrome browser and click on the inspect link under your device.

    What are the limitations of running Chrome apps on a mobile device using Cordova?

    While Cordova allows you to run Chrome apps on a mobile device, there are some limitations. For example, not all Chrome APIs are supported, and there may be differences in behavior between the Chrome app and the Cordova app due to differences in the underlying platforms.

    Can I use Cordova plugins in my Chrome app?

    Yes, you can use Cordova plugins in your Chrome app. To do this, you need to add the plugin to your project using the cordova plugin add command followed by the name of the plugin.

    How can I update my Cordova project?

    To update your Cordova project, you can use the cordova platform update command followed by the name of the platform. For example, cordova platform update android will update the Android platform in your project.

    The above is the detailed content of Running Chrome Apps on a Mobile Device Using Cordova. For more information, please follow other related articles on the PHP Chinese website!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Java vs. JavaScript: Clearing Up the Confusion Java vs. JavaScript: Clearing Up the Confusion Jun 20, 2025 am 12:27 AM

    Java and JavaScript are different programming languages, each suitable for different application scenarios. Java is used for large enterprise and mobile application development, while JavaScript is mainly used for web page development.

    Javascript Comments: short explanation Javascript Comments: short explanation Jun 19, 2025 am 12:40 AM

    JavaScriptcommentsareessentialformaintaining,reading,andguidingcodeexecution.1)Single-linecommentsareusedforquickexplanations.2)Multi-linecommentsexplaincomplexlogicorprovidedetaileddocumentation.3)Inlinecommentsclarifyspecificpartsofcode.Bestpractic

    How to work with dates and times in js? How to work with dates and times in js? Jul 01, 2025 am 01:27 AM

    The following points should be noted when processing dates and time in JavaScript: 1. There are many ways to create Date objects. It is recommended to use ISO format strings to ensure compatibility; 2. Get and set time information can be obtained and set methods, and note that the month starts from 0; 3. Manually formatting dates requires strings, and third-party libraries can also be used; 4. It is recommended to use libraries that support time zones, such as Luxon. Mastering these key points can effectively avoid common mistakes.

    JavaScript vs. Java: A Comprehensive Comparison for Developers JavaScript vs. Java: A Comprehensive Comparison for Developers Jun 20, 2025 am 12:21 AM

    JavaScriptispreferredforwebdevelopment,whileJavaisbetterforlarge-scalebackendsystemsandAndroidapps.1)JavaScriptexcelsincreatinginteractivewebexperienceswithitsdynamicnatureandDOMmanipulation.2)Javaoffersstrongtypingandobject-orientedfeatures,idealfor

    Why should you place  tags at the bottom of the ? Why should you place tags at the bottom of the ? Jul 02, 2025 am 01:22 AM

    PlacingtagsatthebottomofablogpostorwebpageservespracticalpurposesforSEO,userexperience,anddesign.1.IthelpswithSEObyallowingsearchenginestoaccesskeyword-relevanttagswithoutclutteringthemaincontent.2.Itimprovesuserexperiencebykeepingthefocusonthearticl

    JavaScript: Exploring Data Types for Efficient Coding JavaScript: Exploring Data Types for Efficient Coding Jun 20, 2025 am 12:46 AM

    JavaScripthassevenfundamentaldatatypes:number,string,boolean,undefined,null,object,andsymbol.1)Numbersuseadouble-precisionformat,usefulforwidevaluerangesbutbecautiouswithfloating-pointarithmetic.2)Stringsareimmutable,useefficientconcatenationmethodsf

    What is event bubbling and capturing in the DOM? What is event bubbling and capturing in the DOM? Jul 02, 2025 am 01:19 AM

    Event capture and bubble are two stages of event propagation in DOM. Capture is from the top layer to the target element, and bubble is from the target element to the top layer. 1. Event capture is implemented by setting the useCapture parameter of addEventListener to true; 2. Event bubble is the default behavior, useCapture is set to false or omitted; 3. Event propagation can be used to prevent event propagation; 4. Event bubbling supports event delegation to improve dynamic content processing efficiency; 5. Capture can be used to intercept events in advance, such as logging or error processing. Understanding these two phases helps to accurately control the timing and how JavaScript responds to user operations.

    What's the Difference Between Java and JavaScript? What's the Difference Between Java and JavaScript? Jun 17, 2025 am 09:17 AM

    Java and JavaScript are different programming languages. 1.Java is a statically typed and compiled language, suitable for enterprise applications and large systems. 2. JavaScript is a dynamic type and interpreted language, mainly used for web interaction and front-end development.

    See all articles