react native 初始化项目指定version

react-native init projectname --version 0.29.2

react native realm

定义model

var Realm = require('realm');
const CarSchema = {
    name: 'Car',
    properties: {
    make:  'string',    
    model: 'string',
    miles: {type: 'int', default: 0},
    }
};
const PersonSchema = {
    name: 'Person',
    properties: {
    name:     'string',
    birthday: 'date',
    cars:     {type: 'list', objectType: 'Car'},
    picture:  {type: 'data', optional: true}, // optional property
    }
};

// Initialize a Realm with Car and Person models
let realm = new Realm({schema: [CarSchema, PersonSchema]});

创建model

let realm = new Realm({schema: [CarSchema]);

realm.write(() => {
realm.create('Car', {make: 'Honda', model: 'Accord', drive: 'awd'});
});

通过主键创建更新对象

realm.write(() => {
// Create a book object
realm.create('Book', {id: 1, title: 'Recipes', price: 35});

// Update book with new price keyed off the id
realm.create('Book', {id: 1, price: 55}, true);
});

查询

let hondas = realm.objects('Car').filtered('make = "Honda"');
let cars = realm.objects('Car');

// get first 5 Car objects
let firstCars = cars.slice(0, 5);

node 返回json数据

var http = require('http');

var data = {key: 'value', hello: 'world'};

var srv = http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'application/json'});
  res.end(JSON.stringify(data));
});

srv.listen(8080, function() {
  console.log('listening on localhost:8080');
});

iOS 真机调试

进入xcode,菜单栏选择xcode –> preferences (快捷键 command + ,)在Accounts选项卡添加自己的Apple ID,然后按步骤操作。

  • react native TextInput 可以设置padding = 0,这样外部包裹view的的高度就等于TextInput的高度。
  • 使用友盟推送的时候,点击通知跳转到相关的界面,原生android 里可以打开指定的Activity,react native里没有activity给我们

    用,这时候可以在原生监听通知消息,通过DeviceEventEmitter发送事件和参数给JS端,然后通过navigator跳转

    原生中发送事件会调用如下代码:

       private void sendEvent(ReactContext reactContext,
                   String eventName,
                   @Nullable WritableMap params) {
      reactContext
      .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
      .emit(eventName, params);
    }
    

    如何在Activity/Application中获取ReactContext实例?

    reactContext = getReactNativeHost().getReactInstanceManager().getCurrentReactContext()
    
  • 如何实现reactnative android 中按返回键不完全退出App?

       onBackAndroid = () => {
    const nav = this.navigator;
    const routers = nav.getCurrentRoutes();
    if (routers.length > 1) {
      nav.pop();
      return true;
    }
    ExitModule.exit();//调用原生的moveTaskToBack()方法
    return true;
      };
    

  • react native 中 pageA->pageB 想在pageB返回时刷新pageA的内容

    方案一:

    props传递
    

    方案二:

    DeviceEventEmitter
    
  • sina微博 授权未安装客户端登陆出错:

    android.content.ActivityNotFoundException: Unable to find explicit activity class {edu.gdut.myweibo/
    
    com.sina.weibo.sdk.component.WeiboSdkBrowser}; have you declared this 
    
    activity in your AndroidManifest.xml?
    

    在Manifest.xml中注入SDK的WebBrowser

    <activity
        android:name="com.sina.weibo.sdk.component.WeiboSdkBrowser"
        android:configChanges="keyboardHidden|orientation"
        android:exported="false"
        android:windowSoftInputMode="adjustResize" >
    </activity>
    
  • react native 再次点击退出

    onBackAndroid() {
       let routers = this.nav.getCurrentRoutes();
     if (this.nav && routers.length > 1) {
           this.nav.pop();
          return true;
    }
    
    let current = new Date().getTime();
        if (current - last > 2000) {
      last = current;
      ToastAndroid.show('再次点击退出', 1000);
      return true;
        } else {
          return false;
            }
      }
    

AndroidStudio工程

android studio 下引入有.so文件的包,出现如下错误:

java.lang.UnsatisfiedLinkError: 
   dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/net.comet.example-1/base.apk"],
   nativeLibraryDirectories=[/data/app/net.comet.example-1/lib/arm64, /vendor/lib64, /system/lib64]]] 
   couldn't find "libffmpeg.so"
 at java.lang.Runtime.loadLibrary(Runtime.java:366)
 at java.lang.System.loadLibrary(System.java:989)
 at com.dtr.zbar.build.ZBarDecoder.<clinit>(ZBarDecoder.java:6)
 at me.ele.hbdteam.widget.ScanView$1.onPreviewFrame(ScanView.java:263)
 at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1565)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:155)
 at android.app.ActivityThread.main(ActivityThread.java:5696)
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

解决思路是:

  • 1、把libffmpeg.so拷贝一份到armeabi-v7a目录
  • 2、在app.gradle增加jniLibs声明

    sourceSets.main {
            jniLibs.srcDirs = ['libs']
    }
    

SurfaceView

播放视频的时候只有声音没有图像,主要的原因是,在调用MediaPlayer开始播放视频时
SurfaceView却还未创建成功,解决办法是判断SurfaceView创建成功后再调用MediaPlayer的播放,
通过surfaceHolder的addCallBack()方法来监听surfaceCreated