├── index.js ├── package.json ├── README.md ├── BackHome.java ├── AppReactPackage.java ├── postInstall.js └── configureReactBackHome.js /index.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-back-home", 3 | "version": "0.2.13", 4 | "description": "使得app返回桌面,后台运行", 5 | "main": "index.js", 6 | "scripts": { 7 | "postinstall": "node postInstall.js" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "keywords": [ 12 | "react", 13 | "back", 14 | "home", 15 | "desktop" 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 功能介绍: 2 | - React Native app返回桌面,后台运行插件。 3 | 4 | ## 适用平台: 5 | - React Native Android 6 | 7 | ## 安装步骤: 8 | - 1、运行 npm install react-back-home --save 9 | - 2、运行 npm run configureReactBackHome 10 | 11 | ## 使用方法: 12 | - 1、头文件定义:import { NativeModules } from 'react-native' 13 | - 2、调用:NativeModules.BackHome.go(); 14 | 15 | ## Github地址: 16 | - https://github.com/yuejunquan/react-back-home 17 | - 如果此插件帮助了您,请顺便给颗星星 -------------------------------------------------------------------------------- /BackHome.java: -------------------------------------------------------------------------------- 1 | package com.backHome; 2 | 3 | import android.content.Intent; 4 | import android.support.v4.app.ActivityCompat; 5 | 6 | import com.facebook.react.bridge.ReactApplicationContext; 7 | import com.facebook.react.bridge.ReactContextBaseJavaModule; 8 | import com.facebook.react.bridge.ReactMethod; 9 | 10 | /** 11 | * Created by yjq on 2017/5/8. 12 | */ 13 | 14 | public class BackHome extends ReactContextBaseJavaModule { 15 | public BackHome(ReactApplicationContext reactContext) { 16 | super(reactContext); 17 | } 18 | 19 | @Override 20 | public String getName() { 21 | return "BackHome"; 22 | } 23 | 24 | @ReactMethod 25 | public void go() { 26 | //启动一个意图,回到桌面 27 | Intent backHome = new Intent(Intent.ACTION_MAIN); 28 | backHome.addCategory(Intent.CATEGORY_HOME); 29 | ActivityCompat.startActivity(getCurrentActivity(), backHome, null); 30 | } 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /AppReactPackage.java: -------------------------------------------------------------------------------- 1 | package com.backHome; 2 | 3 | import com.facebook.react.ReactPackage; 4 | import com.facebook.react.bridge.JavaScriptModule; 5 | import com.facebook.react.bridge.NativeModule; 6 | import com.facebook.react.bridge.ReactApplicationContext; 7 | import com.facebook.react.uimanager.ViewManager; 8 | 9 | import java.util.ArrayList; 10 | import java.util.Collections; 11 | import java.util.List; 12 | 13 | public class AppReactPackage implements ReactPackage { 14 | 15 | @Override 16 | public List> createJSModules() { 17 | return Collections.emptyList(); 18 | } 19 | 20 | @Override 21 | public List createViewManagers(ReactApplicationContext reactContext) { 22 | return Collections.emptyList(); 23 | } 24 | 25 | @Override 26 | public List createNativeModules( 27 | ReactApplicationContext reactContext) { 28 | List modules = new ArrayList<>(); 29 | 30 | modules.add(new BackHome(reactContext)); 31 | 32 | return modules; 33 | } 34 | } -------------------------------------------------------------------------------- /postInstall.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var spath = require('path'); 3 | var os = require('os'); 4 | 5 | function getAllfiles(dir, findOne) { 6 | // if (arguments.length < 2) throw new TypeError('Bad arguments number'); 7 | 8 | if (typeof findOne !== 'function') { 9 | throw new TypeError('The argument "findOne" must be a function'); 10 | } 11 | 12 | eachFileSync(spath.resolve(dir), findOne); 13 | } 14 | 15 | function eachFileSync (dir, findOne) { 16 | var stats = fs.statSync(dir); 17 | var files = fullPath(dir, fs.readdirSync(dir)); 18 | files.forEach(function (item) { 19 | findOne(item, stats); 20 | }); 21 | } 22 | 23 | function fullPath (dir, files) { 24 | return files.map(function (f) { 25 | return spath.join(dir, f); 26 | }); 27 | } 28 | 29 | getPackageJson("./../..", function (f, s) { 30 | var isPackageJson = f.match(/package\.json/); 31 | if (isPackageJson != null) { 32 | console.log("find package.json file: " + f); 33 | if (isFile(f) == false) { 34 | console.log("configure package.json error!!"); 35 | return; 36 | } 37 | var rf = fs.readFileSync(f, "utf-8"); 38 | var searchKey = rf.match(/\n.*\"scripts\"\: \{\n/); 39 | 40 | if (/configureReactBackHome/.test(rf)) { 41 | return; 42 | } 43 | 44 | if (searchKey != null) { 45 | rf = rf.replace(searchKey[0], searchKey[0] + " \"configureReactBackHome\"\: \"node node_modules\/react-back-home\/configureReactBackHome\.js\"\,\n"); 46 | fs.writeFileSync(f, rf, "utf-8"); 47 | } 48 | } 49 | }); 50 | 51 | function getPackageJson(dir, findOne) { 52 | if (typeof findOne !== 'function') { 53 | throw new TypeError('The argument "findOne" must be a function'); 54 | } 55 | 56 | eachFileSync(spath.resolve(dir), findOne); 57 | } 58 | 59 | function isFile(path){ 60 | return exists(path) && fs.statSync(path).isFile(); 61 | } 62 | 63 | function exists(path){ 64 | return fs.existsSync(path) || path.existsSync(path); 65 | } 66 | 67 | function isDir(path){ 68 | return exists(path) && fs.statSync(path).isDirectory(); 69 | } 70 | -------------------------------------------------------------------------------- /configureReactBackHome.js: -------------------------------------------------------------------------------- 1 | // 这个地方是更新配置文件 的脚本 2 | var fs = require('fs'); 3 | var spath = require('path'); 4 | var os = require('os'); 5 | //修改MainApplication.java 6 | setMainApplication("./android", function (path, s) { 7 | //找到MainApplication.java 8 | var isSettingGradle = path.match(/MainApplication\.java/); 9 | if (isSettingGradle != null) {//如果文件不为空 10 | console.log("文件路径:" + path); 11 | if (isFile(path) == false) { 12 | console.log("MainApplication.java文件不存在!!"); 13 | return; 14 | } 15 | var rf = fs.readFileSync(path, "utf-8"); 16 | //添加new AppReactPackage() 17 | var isAlreadyWrite = rf.match(/.*new AppReactPackage().*/); 18 | if (isAlreadyWrite == null) {//如果没有添加xxx,便执行 19 | var searchKey = rf.match(/new MainReactPackage()../); 20 | if (searchKey != null) {//如果没有找到文件里指定的位置 21 | rf = rf.replace(searchKey[0], searchKey[0]+ "\n,new AppReactPackage()"); 22 | fs.writeFileSync(path, rf, "utf-8"); 23 | } else { 24 | throw new TypeError("没有在MainApplication.java里找到指定的位置"); 25 | } 26 | } 27 | //添加import com.backHome.AppReactPackage; 28 | var isAlreadyWrite = rf.match(/.*import com.backHome.AppReactPackage;.*/); 29 | if (isAlreadyWrite == null) {//如果没有添加xxx,便执行 30 | var searchKey = rf.match(/import android.app.Application;/); 31 | if (searchKey != null) {//如果没有找到文件里指定的位置 32 | rf = rf.replace(searchKey[0], searchKey[0]+ "\nimport com.backHome.AppReactPackage;"); 33 | fs.writeFileSync(path, rf, "utf-8"); 34 | } else { 35 | throw new TypeError("没有在MainApplication.java里找到指定的位置"); 36 | } 37 | } 38 | 39 | } 40 | }); 41 | function setMainApplication(dir, findOne) { 42 | if (typeof findOne !== 'function') { 43 | throw new TypeError('源代码错误'); 44 | } 45 | eachFileSync(spath.resolve(dir), findOne); 46 | } 47 | 48 | //在com下面创建backHome文件夹 49 | createFile(); 50 | function createFile() { 51 | var pathh = "./android/app/src/main/java/com/backHome" 52 | var nodePath="./node_modules/react-back-home/"; 53 | if (!fs.existsSync(pathh)) { 54 | fs.mkdirSync(pathh); 55 | } 56 | var AppReactPackage = fs.readFileSync(nodePath+"AppReactPackage.java", "utf-8"); 57 | var BackHome = fs.readFileSync(nodePath+"BackHome.java", "utf-8"); 58 | if (!fs.existsSync(pathh + '/AppReactPackage.java')) {//如果文件不存在就创建 59 | fs.appendFileSync(pathh + '/AppReactPackage.java', AppReactPackage); 60 | } 61 | if (!fs.existsSync(pathh + '/BackHome.java')) {//如果文件不存在就创建 62 | fs.appendFileSync(pathh + '/BackHome.java', BackHome) 63 | } 64 | 65 | 66 | } 67 | 68 | 69 | 70 | //判断文件是否存在 71 | function isFile(path) { 72 | return exists(path) && fs.statSync(path).isFile(); 73 | } 74 | // 判断文件 75 | function exists(path) { 76 | return fs.existsSync(path) || path.existsSync(path); 77 | } 78 | //遍历文件夹 79 | function eachFileSync(dir, findOne) { 80 | var stats = fs.statSync(dir); 81 | findOne(dir, stats); 82 | 83 | // 遍历子目录 84 | if (stats.isDirectory()) { 85 | var files = fullPath(dir, fs.readdirSync(dir)); 86 | // console.log(dir); 87 | files.forEach(function (f) { 88 | eachFileSync(f, findOne); 89 | }); 90 | } 91 | } 92 | //查询出路径下的文件+文件夹 93 | function fullPath(dir, files) { 94 | return files.map(function (f) { 95 | return spath.join(dir, f); 96 | }); 97 | } --------------------------------------------------------------------------------