LocationActivity.java 2.5 KB
Newer Older
B
Blankj 已提交
1 2 3
package com.blankj.androidutilcode.activities;

import android.app.Activity;
C
cmj 已提交
4
import android.content.ComponentName;
B
Blankj 已提交
5
import android.content.Context;
C
cmj 已提交
6 7
import android.content.Intent;
import android.content.ServiceConnection;
B
Blankj 已提交
8
import android.os.Bundle;
C
cmj 已提交
9
import android.os.IBinder;
B
Blankj 已提交
10 11 12
import android.widget.TextView;

import com.blankj.androidutilcode.R;
C
cmj 已提交
13
import com.blankj.androidutilcode.services.LocationService;
B
Blankj 已提交
14 15 16 17 18 19 20 21 22 23 24

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/10/13
 *     desc  : Location工具类测试
 * </pre>
 */
public class LocationActivity extends Activity {

C
cmj 已提交
25 26
    private TextView        tvAboutLocation;
    private LocationService mLocationService;
B
Blankj 已提交
27

B
Blankj 已提交
28 29 30
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
C
cmj 已提交
31
        setContentView(R.layout.activity_location);
B
Blankj 已提交
32

B
Blankj 已提交
33 34
        tvAboutLocation = (TextView) findViewById(R.id.tv_about_location);

C
cmj 已提交
35
        bindService(new Intent(this, LocationService.class), conn, Context.BIND_AUTO_CREATE);
C
cmj 已提交
36
    }
B
Blankj 已提交
37

C
cmj 已提交
38 39 40
    @Override
    protected void onDestroy() {
        super.onDestroy();
C
cmj 已提交
41
        unbindService(conn);
B
Blankj 已提交
42
    }
C
cmj 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {

        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mLocationService = ((LocationService.LocationBinder) service).getService();
            mLocationService.setOnGetLocationListener(new LocationService.OnGetLocationListener() {
                @Override
                public void getLocation(final String lastLatitude, final String lastLongitude, final String latitude, final String longitude, final String country, final String locality, final String street) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvAboutLocation.setText("lastLatitude: " + lastLatitude +
                                    "\nlastLongitude: " + lastLongitude +
                                    "\nlatitude: " + latitude +
                                    "\nlongitude: " + longitude +
                                    "\ngetCountryName: " + country +
                                    "\ngetLocality: " + locality +
                                    "\ngetStreet: " + street
                            );
                        }
                    });
                }
            });
        }
    };
B
Blankj 已提交
73
}