大屏广告机Android 系统自动启动

在淘宝了买了一个触屏网络智能终端也就是那种广告机回来,因为想要装上自己的安卓系统,做成自动启动,取代他自带的系统。于是用React Native 开发了一个,添加了自动启动功能.初始化项目后,修改了 /android/app/src/main/AndroidMainfest.xml文件参考代码如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.offlineadv"
    android:installLocation="internalOnly"  
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED"  
    >

<uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
  <uses-permission android:name="android.permission.CAMERA" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />  

     <application
    android:name=".MainApplication"
    android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:allowBackup="false"
    android:theme="@style/AppTheme">
    <activity
      android:name=".MainActivity"
      android:label="@string/app_name"
      android:configChanges="keyboard|keyboardHidden|orientation|screenSize" 
      android:windowSoftInputMode="adjustResize"
      android:noHistory="true"
        android:excludeFromRecents="true"  
        >
      <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    <receiver
      android:name=".BootBroadcastReceiver" 
      android:enabled="true" 
      android:exported="true">
        <intent-filter>
            <!--注册开机广播地址-->
            <action android:name="android.intent.action.BOOT_COMPLETED"/> 
            <action android:name="android.intent.action.ACTION_BOOT_COMPLETED"/>            
            <category android:name="android.intent.category.HOME" />
            <!-- <data android:scheme="file"/> -->
        </intent-filter>  
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_MOUNTED"/>  
            <action android:name="android.intent.action.MEDIA_UNMOUNTED"/> 
            <data android:scheme="file">
            </data>
        </intent-filter>        
    </receiver>   
  </application>
</manifest>

/android/app/src/main/java/com/offlineadv/目录新建一个文件MainApplication.java

package com.offlineadv;

import android.app.Application;
import android.content.Context;
import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.soloader.SoLoader;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost =
      new ReactNativeHost(this) {
        @Override
        public boolean getUseDeveloperSupport() {
          return BuildConfig.DEBUG;
        }

        @Override
        protected List<ReactPackage> getPackages() {
          @SuppressWarnings("UnnecessaryLocalVariable")
          List<ReactPackage> packages = new PackageList(this).getPackages();
          // Packages that cannot be autolinked yet can be added manually here, for example:
          // packages.add(new MyReactNativePackage());
          return packages;
        }

        @Override
        protected String getJSMainModuleName() {
          return "index";
        }
      };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
    initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
  }

  /**
   * Loads Flipper in React Native templates. Call this in the onCreate method with something like
   * initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
   *
   * @param context
   * @param reactInstanceManager
   */
  private static void initializeFlipper(
      Context context, ReactInstanceManager reactInstanceManager) {
    if (BuildConfig.DEBUG) {
      try {
        /*
         We use reflection here to pick up the class that initializes Flipper,
        since Flipper library is not available in release mode
        */
        Class<?> aClass = Class.forName("com.offlineadv.ReactNativeFlipper");
        aClass
            .getMethod("initializeFlipper", Context.class, ReactInstanceManager.class)
            .invoke(null, context, reactInstanceManager);
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      } catch (NoSuchMethodException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      }
    }
  }
}

这个文件头部的package 是我们应用名字,编译后装到大屏机试了一下,成功!

代码GIT地址:https://github.com/championgo/react-native-code.git

Related Posts