Skip to content

Commit

Permalink
docs: improve Android custom code section (#1857)
Browse files Browse the repository at this point in the history
  • Loading branch information
RoderickQiu authored and jcesarmobile committed Oct 15, 2019
1 parent f2a5240 commit 557dfb1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
28 changes: 26 additions & 2 deletions site/docs-md/android/custom-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ url: /docs/android/custom-code
contributors:
- mlynch
- jcesarmobile
- RoderickQiu
---

# Custom Native Android Code
Expand All @@ -17,9 +18,11 @@ contributors:

The easiest way to build custom native code that needs to be accessible in the WebView is to build
a local Capacitor plugin for it. In this case, building the plugin is as simple as building a class
that inherits from `com.getcapacitor.Plugin` and uses the `@NativePlugin()` and `@PluginMethod()` annotations:
that inherits from `com.getcapacitor.Plugin` and uses the `@NativePlugin()` and `@PluginMethod()` annotations.

`com/example/myapp/CustomNativePlugin.java`:
Here is a simple example:

`com/example/myapp/CustomNativePlugin.java` in `android/app/src/main/java`:

```java
package com.example.myapp;
Expand All @@ -35,8 +38,15 @@ public class CustomNativePlugin extends Plugin {
@PluginMethod()
public void customCall(PluginCall call) {
String message = call.getString("message");
// More code here...
call.success();
}

@PluginMethod()
public void customFunction(PluginCall call) {
// More code here...
call.resolve();
}
}
```

Expand All @@ -61,6 +71,20 @@ public class MainActivity extends BridgeActivity {
}
```

Then you can use your functions in your webView code:

```javascript
// Other codes...
import { Plugins } from "@capacitor/core";
const { CustomNativePlugin } = Plugins;
// Other codes...
CustomNativePlugin.customCall({ message: "CUSTOM MESSAGE" });
CustomNativePlugin.customFunction();
// Other codes...
```

For more usages of plugin APIs, have a look at [Capacitor Android Plugin Guide](https://capacitor.ionicframework.com/docs/plugins/android/).

## Private Native Code

If your code doesn't need to be accessible from the WebView, then simply add your code anywhere it needs to go. With Capacitor, you have full
Expand Down
8 changes: 4 additions & 4 deletions site/docs-md/guides/community.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ contributors:

[Capacitor Workflow for iOS and Android Apps ›](https://www.youtube.com/watch?v=oXbRcpsytGQ)

[Using the Capacitor in Vue.js app](https://akhromieiev.com/tutorials/using-capacitor-in-vue-js-app/)
[Using the Capacitor in Vue.js app ›](https://akhromieiev.com/tutorials/using-capacitor-in-vue-js-app/)

## APIs

Expand All @@ -30,11 +30,11 @@ contributors:

[Splash Screen: Creating a Dynamic/Adaptable Splash Screen for Capacitor (Android) ›](https://www.joshmorony.com/creating-a-dynamic-universal-splash-screen-for-capacitor-android/)

[Using the Capacitor Share API in IOS app](https://akhromieiev.com/tutorials/using-the-capacitor-share-api-in-ios-app/)
[Using the Capacitor Share API in IOS app ›](https://akhromieiev.com/tutorials/using-the-capacitor-share-api-in-ios-app/)

[Using the Capacitor Browser API to open urls](https://akhromieiev.com/tutorials/using-the-capacitor-browser-api-to-open-links/)
[Using the Capacitor Browser API to open urls ›](https://akhromieiev.com/tutorials/using-the-capacitor-browser-api-to-open-links/)

[Network Detection with Capacitor in Ionic project](https://akhromieiev.com/tutorials/network-detection-capacitor/)
[Network Detection with Capacitor in Ionic project ›](https://akhromieiev.com/tutorials/network-detection-capacitor/)

## Integrations

Expand Down

0 comments on commit 557dfb1

Please sign in to comment.