GVKun编程网logo

如何正确读取Android上的AsyncStorage(android读取文件内容的首要方法)

26

对于如何正确读取Android上的AsyncStorage感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解android读取文件内容的首要方法,并且为您提供关于@react-native-c

对于如何正确读取Android上的AsyncStorage感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解android读取文件内容的首要方法,并且为您提供关于@react-native-community/async-storage在Android上的手动link问题、Android app如何正确读写系统sys设备节点、android import android-async-http-master有错误、Android Studio调试forAsBackground代码的AsyncTask的宝贵知识。

本文目录一览:

如何正确读取Android上的AsyncStorage(android读取文件内容的首要方法)

如何正确读取Android上的AsyncStorage(android读取文件内容的首要方法)

似乎可以从Android(本机)访问AsyncStorage;但我仍然在努力做这项工作.

在我的React Native应用程序中,我有类似的东西:

商店

const config = {
  ...
}

export const reducers = {
  ...
  user: user
}


let rootReducer = combineReducers(reducers)
let reducer = persistReducer(config, rootReducer)

用户减速机

export class Actions {
    ...
}

const initialState = {
      first_name: : null,
      last_name: : null,
      email: null,
      addresses: [
          { ... }
      ]
    }
  }
}

export const reducer = (state = initialState, action) => {
  switch (action.type) {
    ...
  }
}

从商店获取用户

const user = store.getState().user
console.log(user.first_name) // Steve
...

现在,当我尝试从Android获得相同的用户时,问题开始了,我所有的挫败尝试都是这样:

try {
    readableDatabase = ReactDatabasesupplier.getInstance(getReactApplicationContext()).getReadableDatabase();
    catalystLocalStorage = readableDatabase.query("catalystLocalStorage", new String[]{"value"}, "key = ?", new String[] { "full_name" }, null, null, null);
    String[] names = catalystLocalStorage.getColumnNames();
    if (catalystLocalStorage.movetoFirst()) {
        final String value = catalystLocalStorage.getString(catalystLocalStorage.getColumnIndex("value"));
        Log.d(TAG, value);
    }
} finally {
    if (catalystLocalStorage != null) {
        catalystLocalStorage.close();
    }

    if (readableDatabase != null) {
        readableDatabase.close();
    }
}

由于用户是一个对象,我不知道这是否是获取’first_name’的正确方法,我尝试得到’用户’但没有用.

我开始认为我应该使用react-native-sqlite-storage来了解我的数据结构,但我不知道我是否能够在Android上访问该数据库.

PS:我想访问AsyncStorage而不是SharedPreferences

Lib版本

react-native: 0.48.4
redux: 3.7.2
redux-persist: 5.4.0

有些问题不起作用

> Access AsyncStorage from native code
> Can I access data stored in React Native’s AsyncStorage from java layer?
> React Native: How to use Java code to fetch data via React Native async storage

解决方法:

AsyncStorage它是一个独特的JSON对象,而我期待的是许多行的键和值;这就是为什么我变空了.

所以首先我继续查询

catalystLocalStorage = readableDatabase.query("catalystLocalStorage", new String[]{"key", "value"}, null, null, null, null, null);

然后我检查以避免空指针

if (catalystLocalStorage.movetoFirst()) {

然后我做了取

do {
    // JSONObject will ask for try catch
    JSONObject obj = new JSONObject(catalystLocalStorage.getString(catalystLocalStorage.getColumnIndex("value")));
} while(catalystLocalStorage.movetoNext());

我相信可能有更好的方法来做到这一点,但是对,我知道我很好.

如果你有更好的想法,请留下你的想法.

UPDATE

import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;
import android.util.Log;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.modules.storage.ReactDatabasesupplier;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;

public class AsyncStorage {

    public static String TAG = "RNAsyncStorage";
    public ReactApplicationContext context;
    public ArrayList<JSONObject> collection;
    public JSONObject data;
    public JSONObject user;

    Cursor catalystLocalStorage = null;
    sqliteDatabase readableDatabase = null;

    public AsyncStorage (ReactApplicationContext context) {
        this.context = context;
        this.collection = new ArrayList<JSONObject>();
        this.fetch();
        this.setUser();
    }

    public void fetch() {
        try {
            readableDatabase = ReactDatabasesupplier.getInstance(context).getReadableDatabase();
            catalystLocalStorage = readableDatabase.query("catalystLocalStorage", new String[]{"key", "value"}, null, null, null, null, null);

            if (catalystLocalStorage.movetoFirst()) {
                do {
                    try {
                        // one row with all AsyncStorage: { "user": { ... }, ... }
                        String json = catalystLocalStorage.getString(catalystLocalStorage.getColumnIndex("value"));
                        JSONObject obj = new JSONObject(json);

                        String user = obj.getString("user");

                        JSONObject res = new JSONObject();
                        res.put("user", new JSONObject(user));

                        collection.add(res);
                    } catch(Exception e) {
                        // do something
                    }
                } while(catalystLocalStorage.movetoNext());
            }
        } finally {
            if (catalystLocalStorage != null) {
                catalystLocalStorage.close();
            }

            if (readableDatabase != null) {
                readableDatabase.close();
            }

            data = this.collection.get(0);
        }
    }

    public String getFullname () {
        try {
            return user.getString("fullname");
        } catch (Exception e) {
            return "";
        }
    }
}

打电话给上课

AsyncStorage as = new AsyncStorage(context);
as.getFullname()

@react-native-community/async-storage在Android上的手动link问题

@react-native-community/async-storage在Android上的手动link问题

PS C:\Users\linjin\Desktop\RN_APP> react-native link @react-native-community/async-storage
error Something went wrong while linking. Error: Expected "/* Begin ", "/* End ", "\"", or [A-Za-z0-9_.] but "<" found.
Please file an issue here: https://github.com/react-native-community/react-native-cli/issues
error Expected "/* Begin ", "/* End ", "\"", or [A-Za-z0-9_.] but "<" found.

 

解决方案

1.在项目结构android/settings.gradle中添加如下代码:
rootProject.name = ''MyApp''

include '':app''

+ include '':@react-native-community_async-storage''
+ project('':@react-native-community_async-storage'').projectDir = new File(rootProject.projectDir, ''../node_modules/@react-native-community/async-storage/android'')

 

2.在项目结构android/app/build.gradle添加如下dependencies:
dependencies {
  ...
+ implementation project('':@react-native-community_async-storage'')
}

 

3.在MainApplication.java添加如下代码:
package com.myapp;

+ import com.reactnativecommunity.asyncstorage.AsyncStoragePackage;

...

@Override
protected List<ReactPackage> getPackages() {
    return Arrays.<ReactPackage>asList(
        new MainReactPackage(),
+       new AsyncStoragePackage()    
    );
}

Android app如何正确读写系统sys设备节点

Android app如何正确读写系统sys设备节点

http://www.jianshu.com/p/9da8cdb4e684

http://blog.csdn.net/wh_19910525/article/details/45170755

http://blog.csdn.net/lei1217/article/details/48377009

[Description]
Android L APP 如何获取sys file system 中节点的写权限
 
[Keyword]
L SELinux sys write

[Android Version]
Version >= android 5.0
 
[Solution]
Google 默认禁止app , 包括system app, radio app 等直接写/sys 下面的文件, 认为这个是有安全风险的。如果直接放开SELinux 权限, 会导致CTS 无法通过.

通常遇到此类情况,你有两种做法:
(1). 通过system server service 或者 init 启动的service 读写, 然后app 通过binder/socket 等方式连接APP 访问. 此类安全可靠, 并且可以在service 中做相关的安全审查, 推崇这种方法.

(2). 修改对应节点的SELinux Security Label, 为特定的APP, 如system app, radio, bluetooth 等内置APP开启权限, 但严禁为untrsted app 开启权限. 具体的做法下面以 system app 控制/sys/class/leds/lcd-backlight/brightness 来说明.

1. 在device/mediatek/common/sepolicy/file.te 定义brightness SELinux type
type sys_lcd_brightness_file, fs_type,sysfs_type;

2. 在device/mediatek/common/sepolicy/file_contexts 绑定 brightness 对应的label, 注意对应的节点是实际节点,而不是链接.以及整个目录路径中也绝不能包含链接(无数同仁有犯这个错误,特意提醒)
/sys/devices/platform/leds-mt65xx/leds/lcd-backlight/brightness u:object_r:sys_lcd_brightness_file:s0

3. 在device/mediatek/common/sepolicy/system_app.te 中申请权限.
allow system_app sys_lcd_brightness_file:file rw_file_perms;

4. 为其它的process 申请相关的权限,如system_server, 在device/mediatek/common/sepolicy/system_server.te
allow system_server sys_lcd_brightness_file:file rw_file_perms;

原则上我们都推崇使用第一种方式处理.

android import android-async-http-master有错误

android import android-async-http-master有错误

我从github获得了一个名为android-async-http-master的android项目,这是下载链接:https://github.com/loopj/android-async-http
但是当我将项目导入eclipse时,它有一个错误.

我得到这样的错误:

import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.HeaderElement;

所以我的问题是:项目包的名称是com.loopj.android.http,其中是cz.msebera.android.httpclient包?

解决方法:

你必须下载这个jar cz.msebera.android,然后导入你的项目

如果您使用android studio,只需在.gradle依赖项中添加此编译’cz.msebera.android:httpclient:4.3.6′

希望它有所帮助

Android Studio调试forAsBackground代码的AsyncTask

Android Studio调试forAsBackground代码的AsyncTask

我在AsyncTask类的doInBackground()代码中放了一些断点.但是当我调试应用程序时,控制不会超过doInBackground().请帮我解决这个问题.

解决方法

将以下代码片段放在doInBackground的开头:

android.os.Debug.waitForDebugger();

那么当你在该线程中设置断点时,eclipse会找到它.

在你的模拟器中,

>转到开发者设置.
>点击选择调试应用程序
>选择要调试的应用程序
>将启用“等待调试器”复选框.选中此复选框

现在再试一次运行该应用程序.

今天关于如何正确读取Android上的AsyncStorageandroid读取文件内容的首要方法的介绍到此结束,谢谢您的阅读,有关@react-native-community/async-storage在Android上的手动link问题、Android app如何正确读写系统sys设备节点、android import android-async-http-master有错误、Android Studio调试forAsBackground代码的AsyncTask等更多相关知识的信息可以在本站进行查询。

本文标签: