Call Android Java Code

8 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€92.25
List Price:  €129.16
You save:  €36.90
£79.03
List Price:  £110.65
You save:  £31.61
CA$136.41
List Price:  CA$190.98
You save:  CA$54.56
A$150.30
List Price:  A$210.43
You save:  A$60.12
S$134.75
List Price:  S$188.65
You save:  S$53.90
HK$780.13
List Price:  HK$1,092.21
You save:  HK$312.08
CHF 90.94
List Price:  CHF 127.32
You save:  CHF 36.38
NOK kr1,073.42
List Price:  NOK kr1,502.83
You save:  NOK kr429.41
DKK kr688.34
List Price:  DKK kr963.70
You save:  DKK kr275.36
NZ$163.78
List Price:  NZ$229.30
You save:  NZ$65.51
د.إ367.26
List Price:  د.إ514.18
You save:  د.إ146.91
৳11,718.16
List Price:  ৳16,405.89
You save:  ৳4,687.73
₹8,335.38
List Price:  ₹11,669.86
You save:  ₹3,334.48
RM468.70
List Price:  RM656.20
You save:  RM187.50
₦151,484.85
List Price:  ₦212,084.85
You save:  ₦60,600
₨27,847.21
List Price:  ₨38,987.21
You save:  ₨11,140
฿3,628.63
List Price:  ฿5,080.23
You save:  ฿1,451.60
₺3,227.56
List Price:  ₺4,518.72
You save:  ₺1,291.15
B$512.82
List Price:  B$717.98
You save:  B$205.15
R1,828.78
List Price:  R2,560.37
You save:  R731.58
Лв180.35
List Price:  Лв252.50
You save:  Лв72.15
₩135,738.64
List Price:  ₩190,039.53
You save:  ₩54,300.88
₪371.33
List Price:  ₪519.88
You save:  ₪148.54
₱5,772.67
List Price:  ₱8,081.97
You save:  ₱2,309.29
¥15,582.60
List Price:  ¥21,816.27
You save:  ¥6,233.66
MX$1,669.16
List Price:  MX$2,336.89
You save:  MX$667.73
QR364.11
List Price:  QR509.77
You save:  QR145.66
P1,351.57
List Price:  P1,892.25
You save:  P540.68
KSh13,098.69
List Price:  KSh18,338.69
You save:  KSh5,240
E£4,690.49
List Price:  E£6,566.87
You save:  E£1,876.38
ብር5,727.42
List Price:  ብር8,018.62
You save:  ብር2,291.20
Kz83,741.62
List Price:  Kz117,241.62
You save:  Kz33,500
CLP$89,844.01
List Price:  CLP$125,785.21
You save:  CLP$35,941.20
CN¥722.63
List Price:  CN¥1,011.72
You save:  CN¥289.08
RD$5,860.43
List Price:  RD$8,204.83
You save:  RD$2,344.40
DA13,452.89
List Price:  DA18,834.58
You save:  DA5,381.69
FJ$223.44
List Price:  FJ$312.83
You save:  FJ$89.38
Q777.42
List Price:  Q1,088.43
You save:  Q311
GY$20,948.83
List Price:  GY$29,329.21
You save:  GY$8,380.37
ISK kr13,866.61
List Price:  ISK kr19,413.81
You save:  ISK kr5,547.20
DH995.50
List Price:  DH1,393.74
You save:  DH398.24
L1,769.27
List Price:  L2,477.05
You save:  L707.78
ден5,673.92
List Price:  ден7,943.71
You save:  ден2,269.79
MOP$804.52
List Price:  MOP$1,126.37
You save:  MOP$321.84
N$1,823.31
List Price:  N$2,552.71
You save:  N$729.40
C$3,674.63
List Price:  C$5,144.63
You save:  C$1,470
रु13,369.37
List Price:  रु18,717.65
You save:  रु5,348.28
S/371.99
List Price:  S/520.81
You save:  S/148.81
K388.01
List Price:  K543.23
You save:  K155.22
SAR375
List Price:  SAR525.02
You save:  SAR150.01
ZK2,528.85
List Price:  ZK3,540.49
You save:  ZK1,011.64
L459.03
List Price:  L642.66
You save:  L183.63
Kč2,279.78
List Price:  Kč3,191.78
You save:  Kč912
Ft35,756.12
List Price:  Ft50,060
You save:  Ft14,303.88
SEK kr1,076.37
List Price:  SEK kr1,506.96
You save:  SEK kr430.59
ARS$88,615.31
List Price:  ARS$124,064.98
You save:  ARS$35,449.67
Bs691.54
List Price:  Bs968.19
You save:  Bs276.64
COP$383,364.60
List Price:  COP$536,725.77
You save:  COP$153,361.17
₡51,222.42
List Price:  ₡71,713.44
You save:  ₡20,491.01
L2,473.49
List Price:  L3,462.99
You save:  L989.49
₲750,836.16
List Price:  ₲1,051,200.66
You save:  ₲300,364.50
$U3,868.71
List Price:  $U5,416.34
You save:  $U1,547.63
zł393.61
List Price:  zł551.08
You save:  zł157.46
Already have an account? Log In

Transcript

Hello, in this video, we are going to look at how to call Java co native Android Java code. And essentially, you know, get some sort of response back from it. So this is great. If you want to do something Java, maybe it hasn't made its way to flutter yet and to Dart code because you know it nice, still newer than Java. Or maybe you already have libraries some code. This is a great way of integrating pre existing code without having to, you know, convert it over.

So the way you do this is actually pretty simple. We want to do in your dot file, and then we close this data set can in your Docker file, you do import package, locker, and you want to import the service you've got dot and this will allow us to essentially create a service a connection between the doll file and the Java file. The next step for us is to go down to our home Page what we want to do is create the the actual platform connection so static port is platform equals cons and it's a method channel This requires the know where the haters came from. This requires a name and for this name I'm going to put chrome dot Mater dot epic for slash epically really and if you want but i think is good to keep it in the sort of package name format comm dot flutter dot epic and I put epic in there just in case you want to interact with a nother channel as well so you could have you know definitely could have you know sub channel but you know the format is totally up to you.

Next, what we are going to do is create a method and let's avoid Don't want to call it 20 Christopher Princeton asynchronous method. Inside of here, we have a string, which is going to be the value that is returned, we're gonna have a try catch block. And in here we'll say run. platform, dot invoke method. And the method we are invoking isn't printing. We don't need the curly braces.

And we'll handle it all of this on the job. So I put a catch here. And we can just print out the error. If there's any errors. There shouldn't be but in case there is, and we're going to print out the value. Well, this is going to be triggered when I press while I show you see when I load up the button that says reverse in there for our previous tutorials, don't ask me why it's got this is for my animation, though he's told you anyway.

And so we can just say printing So now we're all set up on the DOD side of things, that's really all you need to do. And this value that we get back, you don't have to print it out, you could assign it to some other variable here, you producing calculations, that's totally up to you. But I'm just going to print it out into the console just to keep things simple. Next, if you go to Android, this is where your Angular project is. And if you notice that an iOS will cover all of that in a separate video, you will go to App ignore the red line source main Java and comm dot example dot flutter app. That is my package, go to the main activity, and in here is the default is the D is the default Java code.

So this is where we will be doing all of the magic to be able to run our Java code. So what we want to do here is why is it that final string, and this is only for channel. This is going to be equal to make sure you get this correctly This is going to be equal to exactly this. Copy that. Paste it also is one let you know if you have a little thing saying here, set up SDK, just click it. I've already done it.

A little pop up will open. You'll have various stuff like Android Studio Android does some stuff, click the JDK and you'll be all good to go. If you have any questions, feel free to pop and now so yeah go make sure this channel exactly the thing. And now what we can do is say new method channel. Yet flutter view based on the channel Now, we're going to set method call handler in here, let's say new method call handler dot method call handler. So, this is basically what it looks like all the source code was was a new method channel dot method called handler.

And this method will be called whenever something is sent to this, but because we have done invoke method, we want to make sure that if you have multiple methods, we can handle that because otherwise, loads of them will just be you know get triggered based on this little package name. In here, we can say this method called dot method is equal to so dot equals and then put what you put here, this exact thing where you put invoke method, so if you put open curly brackets, yeah, makes Put them down. If you didn't put it here, make sure you don't put them here. It literally is just a string. And now, to return the value back, you just do result, dot success. And what you put in here is what gets returned back to Amazon, say, buy from Java.

Now we are ready to run it. So if we run it. Now, the thing to bear in mind, when you are doing modifications to your native Android side, you can't use the hot reload or the hot restart, you will have to you know, shut down the application and rebuild either using the run method or the debug method. So that way, it recompile the Android side and puts that into the APK. So that's just something to bear in mind. So it will launch up any moment now and we'll have a button that says reverse again, ignore what it says.

And if I click it we get hi from Java and We have a Nope, we do not have hi from Java anywhere here is right here. And if you had multiple methods, so if we are multiple invoke methods with different method names, you could have an elsif. And then nobody else is saying is the method called equal to this one or that one. That's really all there is to it. Obviously, you can make it more complex than this. You can use all your job or import or your or when the complexities with the data structure, create your own classes, your own Java files, create instances of all of that stuff.

Yeah, you can, you can do that. You can do all of that stuff. And you can import your existing libraries as well. But this is the stepping stone. This is the foundation of connecting Dart to Java. In the next video, I'm going to show you how to return data back not just call a method, but I'm sorry, how to send data and the parameter to Java and then We'll return something back as well.

So that's really if you have any questions, feel free to drop me a message. And as usual, I look forward to seeing you in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.