LocationActivity.java 2.8 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 36 37 38 39 40 41 42 43
        tvAboutLocation.setText("lastLatitude: unknown" +
                "\nlastLongitude: unknown" +
                "\nlatitude: unknown" +
                "\nlongitude: unknown" +
                "\ngetCountryName: unknown" +
                "\ngetLocality: unknown" +
                "\ngetStreet: unknown"
        );

C
cmj 已提交
44
        bindService(new Intent(this, LocationService.class), conn, Context.BIND_AUTO_CREATE);
C
cmj 已提交
45
    }
B
Blankj 已提交
46

C
cmj 已提交
47 48
    @Override
    protected void onDestroy() {
C
cmj 已提交
49
        unbindService(conn);
C
cmj 已提交
50
        super.onDestroy();
B
Blankj 已提交
51
    }
C
cmj 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

    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 已提交
82
}