Skip to content

Commit

Permalink
Kotlin KTX Test
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanCo committed Mar 12, 2018
1 parent 4e64a84 commit c6cc130
Show file tree
Hide file tree
Showing 48 changed files with 1,059 additions and 18 deletions.
19 changes: 19 additions & 0 deletions BaiduLocationSample/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ethanco.baidulocationsample">

<!-- 这个权限用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<!-- 这个权限用于访问GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<!-- 获取运营商信息,用于支持提供运营商信息相关的接口-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<!-- 用于读取手机当前的状态-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<!-- 访问网络,网络定位需要上网-->
<uses-permission android:name="android.permission.INTERNET" />
<!-- SD卡读取权限,用户写入离线定位数据-->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>

<application
android:name=".App"
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

import android.app.Application;

import com.ethanco.lib.baidu.BaiduFacede;

/**
* Application
*
Expand All @@ -17,7 +15,5 @@ public class App extends Application {
@Override
public void onCreate() {
super.onCreate();

BaiduFacede.init(this);
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,99 @@
package com.ethanco.baidulocationsample;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.Poi;
import com.ethanco.lib.baidu.BaiduFacede;
import com.ethanco.lib.baidu.ILocationListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements ILocationListener {
private static final int SDK_PERMISSION_REQUEST = 111233;
private TextView tvInfo;
public static final String TAG = "Z-MainActivity";
private String permissionInfo = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnLocation = (Button) findViewById(R.id.btn_location);
getPersimmions();
tvInfo = (TextView) findViewById(R.id.tv_info);

BaiduFacede.addLocationListener(this);
btnLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BaiduFacede.init(getApplication());
BaiduFacede.addLocationListener(MainActivity.this);
}
});
}

@TargetApi(23)
private void getPersimmions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ArrayList<String> permissions = new ArrayList<String>();
/***
* 定位权限为必须权限,用户如果禁止,则每次进入都会申请
*/
// 定位精确位置
if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if(checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
/*
* 读写权限和电话状态权限非必要权限(建议授予)只会申请一次,用户同意或者禁止,只会弹一次
*/
// 读写权限
if (addPermission(permissions, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
permissionInfo += "Manifest.permission.WRITE_EXTERNAL_STORAGE Deny \n";
}
// 读取电话状态权限
if (addPermission(permissions, Manifest.permission.READ_PHONE_STATE)) {
permissionInfo += "Manifest.permission.READ_PHONE_STATE Deny \n";
}

if (permissions.size() > 0) {
requestPermissions(permissions.toArray(new String[permissions.size()]), SDK_PERMISSION_REQUEST);
}
}
}

@TargetApi(23)
private boolean addPermission(ArrayList<String> permissionsList, String permission) {
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { // 如果应用没有获得对应权限,则添加到列表中,准备批量申请
if (shouldShowRequestPermissionRationale(permission)){
return true;
}else{
permissionsList.add(permission);
return false;
}

}else{
return true;
}
}

@TargetApi(23)
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}

@Override
Expand All @@ -44,7 +115,6 @@ public String generateLocationInfoV2(BDLocation location){
* 时间也可以使用systemClock.elapsedRealtime()方法 获取的是自从开机以来,每次回调的时间;
* location.getTime() 是指服务端出本次结果的时间,如果位置不发生变化,则时间不变
*/

sb.append(location.getTime());
sb.append("\nlocationID : ");// LocationID
sb.append(location.getLocationID());
Expand Down
6 changes: 6 additions & 0 deletions BaiduLocationSample/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
android:orientation="vertical"
android:layout_height="match_parent">

<Button
android:id="@+id/btn_location"
android:text="定位"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:textIsSelectable="true"
android:id="@+id/tv_info"
Expand Down
3 changes: 3 additions & 0 deletions PettyTest2018/ArchitectureTest/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions PettyTest2018/ArchitectureTest/.idea/vcs.xml

This file was deleted.

4 changes: 2 additions & 2 deletions PettyTest2018/ArchitectureTest/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.heiko.architecturetest"
minSdkVersion 17
targetSdkVersion 26
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
9 changes: 9 additions & 0 deletions PettyTest2018/KotlinKtx/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild
6 changes: 6 additions & 0 deletions PettyTest2018/KotlinKtx/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions PettyTest2018/KotlinKtx/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

130 changes: 130 additions & 0 deletions PettyTest2018/KotlinKtx/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions PettyTest2018/KotlinKtx/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions PettyTest2018/KotlinKtx/.idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions PettyTest2018/KotlinKtx/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
Loading

0 comments on commit c6cc130

Please sign in to comment.