Created
June 5, 2017 04:51
-
-
Save anhtuank7c/4c5598f5532bb3a2114655fd7ad5e280 to your computer and use it in GitHub Desktop.
Hướng dẫn gọi function native từ js.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.devjobs; | |
import android.app.Dialog; | |
import android.content.ComponentName; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.provider.ContactsContract; | |
import com.facebook.react.bridge.Callback; | |
import com.facebook.react.bridge.ReactApplicationContext; | |
import com.facebook.react.bridge.ReactContextBaseJavaModule; | |
import com.facebook.react.bridge.ReactMethod; | |
/** | |
* Created by anhtuank7c on 6/5/17. | |
*/ | |
public class CrabstudioUtils extends ReactContextBaseJavaModule { | |
public CrabstudioUtils(ReactApplicationContext reactContext) { | |
super(reactContext); | |
} | |
@Override | |
public String getName() { | |
return "CrabstudioUtils"; | |
} | |
@ReactMethod | |
public void openContact() { | |
Intent intent = new Intent(); | |
intent.setComponent(new ComponentName("com.android.contacts", "com.android.contacts.DialtactsContactsEntryActivity")); | |
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
getReactApplicationContext().getApplicationContext().startActivity(intent); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.devjobs; | |
import com.facebook.react.ReactPackage; | |
import com.facebook.react.bridge.JavaScriptModule; | |
import com.facebook.react.bridge.NativeModule; | |
import com.facebook.react.bridge.ReactApplicationContext; | |
import com.facebook.react.uimanager.ViewManager; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
/** | |
* Created by anhtuank7c on 6/5/17. | |
*/ | |
public class CrabstudioUtilsPackge implements ReactPackage { | |
@Override | |
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) { | |
List<NativeModule> modules = new ArrayList<>(); | |
modules.add(new CrabstudioUtils(reactContext)); | |
return modules; | |
} | |
@Override | |
public List<Class<? extends JavaScriptModule>> createJSModules() { | |
return Collections.emptyList(); | |
} | |
@Override | |
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) { | |
return Collections.emptyList(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
Text, | |
NativeModules | |
} from 'react-native'; | |
const CrabstudioUtils = NativeModules.CrabstudioUtils; | |
Sau đó cần dùng ở đâu thì gọi ra ở đó: | |
<TouchableOpacity onPress={() => CrabstudioUtils.openContact()}> | |
<Text>Open contact</Text> | |
</TouchableOpacity> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sửa file MainApplication.java, cho package bên dưới vào | |
@Override | |
protected List<ReactPackage> getPackages() { | |
return Arrays.<ReactPackage>asList( | |
new MainReactPackage(), | |
new CrabstudioUtilsPackge() // HERE | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment