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.30
List Price:  €129.22
You save:  €36.92
£79.14
List Price:  £110.81
You save:  £31.66
CA$135.92
List Price:  CA$190.30
You save:  CA$54.37
A$153.17
List Price:  A$214.44
You save:  A$61.27
S$134.71
List Price:  S$188.59
You save:  S$53.88
HK$782.26
List Price:  HK$1,095.19
You save:  HK$312.93
CHF 90.56
List Price:  CHF 126.79
You save:  CHF 36.22
NOK kr1,076.34
List Price:  NOK kr1,506.92
You save:  NOK kr430.58
DKK kr688.47
List Price:  DKK kr963.88
You save:  DKK kr275.41
NZ$166.49
List Price:  NZ$233.09
You save:  NZ$66.60
د.إ367.19
List Price:  د.إ514.08
You save:  د.إ146.89
৳11,000.23
List Price:  ৳15,400.77
You save:  ৳4,400.53
₹8,335.05
List Price:  ₹11,669.40
You save:  ₹3,334.35
RM473.25
List Price:  RM662.57
You save:  RM189.32
₦139,656.03
List Price:  ₦195,524.03
You save:  ₦55,868
₨27,872.43
List Price:  ₨39,022.53
You save:  ₨11,150.09
฿3,637.73
List Price:  ฿5,092.97
You save:  ฿1,455.24
₺3,222.80
List Price:  ₺4,512.05
You save:  ₺1,289.25
B$497.67
List Price:  B$696.75
You save:  B$199.08
R1,888.37
List Price:  R2,643.79
You save:  R755.42
Лв180.67
List Price:  Лв252.95
You save:  Лв72.27
₩134,942
List Price:  ₩188,924.20
You save:  ₩53,982.20
₪366.06
List Price:  ₪512.51
You save:  ₪146.44
₱5,625.18
List Price:  ₱7,875.48
You save:  ₱2,250.29
¥15,118.78
List Price:  ¥21,166.90
You save:  ¥6,048.12
MX$1,663.06
List Price:  MX$2,328.35
You save:  MX$665.29
QR365.44
List Price:  QR511.63
You save:  QR146.19
P1,373.02
List Price:  P1,922.28
You save:  P549.26
KSh13,148.68
List Price:  KSh18,408.68
You save:  KSh5,260
E£4,723.53
List Price:  E£6,613.14
You save:  E£1,889.60
ብር5,694.53
List Price:  ብር7,972.57
You save:  ብር2,278.04
Kz83,612.73
List Price:  Kz117,061.17
You save:  Kz33,448.44
CLP$98,024.19
List Price:  CLP$137,237.79
You save:  CLP$39,213.60
CN¥722.49
List Price:  CN¥1,011.52
You save:  CN¥289.02
RD$5,928.03
List Price:  RD$8,299.48
You save:  RD$2,371.45
DA13,469.95
List Price:  DA18,858.47
You save:  DA5,388.52
FJ$225.25
List Price:  FJ$315.36
You save:  FJ$90.11
Q781.84
List Price:  Q1,094.62
You save:  Q312.77
GY$20,987.69
List Price:  GY$29,383.60
You save:  GY$8,395.91
ISK kr13,817.61
List Price:  ISK kr19,345.21
You save:  ISK kr5,527.60
DH1,010.57
List Price:  DH1,414.84
You save:  DH404.26
L1,763.81
List Price:  L2,469.40
You save:  L705.59
ден5,677.01
List Price:  ден7,948.05
You save:  ден2,271.03
MOP$807.66
List Price:  MOP$1,130.76
You save:  MOP$323.09
N$1,895.98
List Price:  N$2,654.45
You save:  N$758.46
C$3,689.21
List Price:  C$5,165.04
You save:  C$1,475.83
रु13,357.88
List Price:  रु18,701.57
You save:  रु5,343.68
S/371.02
List Price:  S/519.45
You save:  S/148.42
K383.60
List Price:  K537.05
You save:  K153.45
SAR375.02
List Price:  SAR525.04
You save:  SAR150.02
ZK2,548.47
List Price:  ZK3,567.96
You save:  ZK1,019.49
L458.83
List Price:  L642.38
You save:  L183.55
Kč2,334.38
List Price:  Kč3,268.22
You save:  Kč933.84
Ft36,425.52
List Price:  Ft50,997.18
You save:  Ft14,571.66
SEK kr1,059.50
List Price:  SEK kr1,483.35
You save:  SEK kr423.84
ARS$85,666.43
List Price:  ARS$119,936.42
You save:  ARS$34,269.99
Bs695.09
List Price:  Bs973.16
You save:  Bs278.06
COP$389,673.99
List Price:  COP$545,559.18
You save:  COP$155,885.18
₡50,509.65
List Price:  ₡70,715.54
You save:  ₡20,205.88
L2,474.33
List Price:  L3,464.17
You save:  L989.83
₲736,638.54
List Price:  ₲1,031,323.43
You save:  ₲294,684.88
$U3,781
List Price:  $U5,293.55
You save:  $U1,512.55
zł398.17
List Price:  zł557.46
You save:  zł159.28
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.