4
4
5
5
package com .example .flutter ;
6
6
7
- import android .app .Activity ;
8
7
import android .content .Context ;
9
- import android .content .Intent ;
10
- import android .content .pm .ApplicationInfo ;
11
8
import android .content .pm .PackageManager ;
12
9
import android .location .Location ;
13
10
import android .location .LocationManager ;
14
11
import android .os .Bundle ;
15
- import android .util .Log ;
16
- import android .view .View ;
17
- import android .widget .Button ;
18
- import android .widget .TextView ;
19
12
20
13
import io .flutter .app .FlutterActivity ;
21
- import io .flutter .view .FlutterMain ;
14
+ import io .flutter .plugin .common .FlutterMethodChannel ;
15
+ import io .flutter .plugin .common .FlutterMethodChannel .MethodCallHandler ;
16
+ import io .flutter .plugin .common .FlutterMethodChannel .Response ;
17
+ import io .flutter .plugin .common .MethodCall ;
22
18
import io .flutter .view .FlutterView ;
23
19
24
- import java .io .File ;
25
- import org .json .JSONException ;
26
- import org .json .JSONObject ;
27
-
28
20
public class ExampleActivity extends FlutterActivity {
29
- private static final String TAG = "ExampleActivity" ;
30
21
private FlutterView flutterView ;
31
22
32
23
@ Override
33
24
public void onCreate (Bundle savedInstanceState ) {
34
25
super .onCreate (savedInstanceState );
35
-
36
- flutterView = getFlutterView ();
37
- flutterView .addOnMessageListener ("getLocation" ,
38
- new FlutterView .OnMessageListener () {
39
- @ Override
40
- public String onMessage (FlutterView view , String message ) {
41
- return onGetLocation (message );
26
+ new FlutterMethodChannel (getFlutterView (), "geo" ).setMethodCallHandler (new MethodCallHandler () {
27
+ @ Override
28
+ public void onMethodCall (MethodCall call , Response response ) {
29
+ if (call .method .equals ("getLocation" )) {
30
+ if (!(call .arguments instanceof String )) {
31
+ throw new IllegalArgumentException ("Invalid argument type, String expected" );
32
+ }
33
+ getLocation ((String ) call .arguments , response );
34
+ } else {
35
+ throw new IllegalArgumentException ("Unknown method " + call .method );
42
36
}
43
- });
37
+ }
38
+ });
44
39
}
45
40
46
- private String onGetLocation (String json ) {
47
- String provider ;
48
- try {
49
- JSONObject message = new JSONObject (json );
50
- provider = message .getString ("provider" );
51
- } catch (JSONException e ) {
52
- Log .e (TAG , "JSON exception" , e );
53
- return null ;
54
- }
55
-
41
+ private void getLocation (String provider , Response response ) {
56
42
String locationProvider ;
57
43
if (provider .equals ("network" )) {
58
44
locationProvider = LocationManager .NETWORK_PROVIDER ;
59
45
} else if (provider .equals ("gps" )) {
60
46
locationProvider = LocationManager .GPS_PROVIDER ;
61
47
} else {
62
- return null ;
48
+ throw new IllegalArgumentException ( "Unknown provider " + provider ) ;
63
49
}
64
-
65
50
String permission = "android.permission.ACCESS_FINE_LOCATION" ;
66
- Location location = null ;
67
51
if (checkCallingOrSelfPermission (permission ) == PackageManager .PERMISSION_GRANTED ) {
68
52
LocationManager locationManager = (LocationManager ) getSystemService (Context .LOCATION_SERVICE );
69
- location = locationManager .getLastKnownLocation (locationProvider );
70
- }
71
-
72
- JSONObject reply = new JSONObject ();
73
- try {
53
+ Location location = locationManager .getLastKnownLocation (locationProvider );
74
54
if (location != null ) {
75
- reply .put ("latitude" , location .getLatitude ());
76
- reply .put ("longitude" , location .getLongitude ());
55
+ response .success (new double [] { location .getLatitude (), location .getLongitude () });
77
56
} else {
78
- reply .put ("latitude" , 0 );
79
- reply .put ("longitude" , 0 );
57
+ response .error ("unknown" , "Location unknown" , null );
80
58
}
81
- } catch (JSONException e ) {
82
- Log .e (TAG , "JSON exception" , e );
83
- return null ;
59
+ } else {
60
+ response .error ("permission" , "Access denied" , null );
84
61
}
85
-
86
- return reply .toString ();
87
62
}
88
- }
63
+ }
0 commit comments