Browse Source

1,增加了MemoryMoniter
2, 添加了定位测试

lyn 5 years ago
parent
commit
01d34e984a

+ 9 - 1
app/build.gradle

@@ -31,6 +31,12 @@ android {
         targetCompatibility 'VERSION_1_8'
     }
 
+    sourceSets {
+        main {
+            jniLibs.srcDirs = ['libs']
+        }
+    }
+
     // Gradle automatically adds 'android.test.runner' as a dependency.
     useLibrary 'android.test.runner'
 
@@ -39,7 +45,7 @@ android {
 }
 
 dependencies {
-    implementation fileTree(dir: 'libs', include: ['*.jar'])
+    implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])
     implementation 'com.android.support:appcompat-v7:28.0.0'
     implementation 'com.android.support.constraint:constraint-layout:1.1.3'
     implementation 'androidx.appcompat:appcompat:1.0.2'
@@ -74,4 +80,6 @@ dependencies {
     implementation 'com.shuyu:GSYVideoPlayer:7.0.2'
     implementation 'com.yanzhenjie.nohttp:nohttp:1.1.11'
     implementation 'com.liulishuo.filedownloader:library:1.7.6'
+
+
 }

+ 1 - 0
app/src/main/AndroidManifest.xml

@@ -2,6 +2,7 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.haochuan.hciptvbasic">
 
+    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
     <application
         android:name=".BaseApp"
         android:allowBackup="true"

+ 3 - 1
app/src/main/java/com/haochuan/hciptvbasic/BaseApp.java

@@ -3,6 +3,7 @@ package com.haochuan.hciptvbasic;
 import android.app.Application;
 
 import com.liulishuo.filedownloader.FileDownloader;
+import com.tt.memorymonitorlib.MemoryMonitor;
 import com.yanzhenjie.nohttp.InitializationConfig;
 import com.yanzhenjie.nohttp.NoHttp;
 import com.yanzhenjie.nohttp.URLConnectionNetworkExecutor;
@@ -14,7 +15,8 @@ public class BaseApp extends Application {
     public void onCreate() {
         super.onCreate();
         initHttp();
-        FileDownloader.setupOnApplicationOnCreate(this);
+        FileDownloader.setupOnApplicationOnCreate(this);   //初始化文件下载插件
+        MemoryMonitor.getInstance().init(this);//初始化memory monitor
     }
 
     private void initHttp() {

+ 42 - 0
app/src/main/java/com/haochuan/hciptvbasic/BaseWebActivity.java

@@ -7,8 +7,11 @@ package com.haochuan.hciptvbasic;
 
 
 import android.annotation.SuppressLint;
+import android.app.ActivityManager;
+import android.content.Context;
 import android.os.Build;
 import android.os.Bundle;
+import android.os.Handler;
 import android.view.ViewGroup;
 import android.view.WindowManager;
 import android.webkit.WebSettings;
@@ -28,6 +31,8 @@ import com.haochuan.hciptvbasic.webview.PlayerToJS;
 import com.haochuan.hciptvbasic.webview.HCWebChromeClient;
 import com.haochuan.hciptvbasic.webview.ToolToJS;
 
+import java.util.List;
+
 public abstract class BaseWebActivity extends AppCompatActivity {
     private WebView webView;                                    //整个应用唯一的webview
     private PlayerToJS playerToJS;                              //PlayerToJS类实例
@@ -95,6 +100,13 @@ public abstract class BaseWebActivity extends AppCompatActivity {
     @Override
     protected void onStop(){
         super.onStop();
+        boolean isForeground = isRunningForeground(this);
+        if (!isForeground) {
+            Handler handler = new Handler(getMainLooper());
+            handler.postDelayed(() -> {
+                exit();
+            }, 500);
+        }
     }
 
     @Override
@@ -220,6 +232,36 @@ public abstract class BaseWebActivity extends AppCompatActivity {
         getToolToJS().logToJs(log);
     }
 
+    /**
+     * 判断应用是否处于前台
+     *
+     * @return <code>true</code>为前台,反之为后台
+     */
+    public boolean isRunningForeground(Context context) {
+        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
+        if (activityManager == null) return false;
+        List<ActivityManager.RunningAppProcessInfo> appProcessInfos = activityManager.getRunningAppProcesses();
+        // 枚举进程
+        for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessInfos) {
+            if (appProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
+                if (appProcessInfo.processName.equals(context.getApplicationInfo().processName)) {
+                    Logger.d("应用处于前台状态");
+                    return true;
+                }
+            }
+        }
+        Logger.d("应用退到后台");
+        return false;
+    }
+
+    /*
+     * 退出应用
+     * */
+    public void exit() {
+        android.os.Process.killProcess(android.os.Process.myPid());   //获取PID
+        System.exit(0);
+    }
+
     /*------------------------子类获取实例接口------------------------------*/
 
     /**

+ 2 - 0
app/src/main/java/com/haochuan/hciptvbasic/MainActivity.java

@@ -11,6 +11,8 @@ public class MainActivity extends BaseWebActivity {
         super.onCreate(savedInstanceState);
     }
 
+
+
     /**-------------------BaseWebActivity重载函数 start--------------------------*/
 
     /*

+ 109 - 0
app/src/main/java/com/haochuan/hciptvbasic/Util/MyLocation.java

@@ -0,0 +1,109 @@
+package com.haochuan.hciptvbasic.Util;
+
+import android.Manifest;
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.location.Location;
+import android.location.LocationListener;
+import android.location.LocationManager;
+import android.os.Bundle;
+import android.util.Log;
+
+import androidx.core.app.ActivityCompat;
+
+import java.util.List;
+
+public class MyLocation {
+    LocationManager locationManager;
+    String TAG = "MyLocation";
+
+    LocationListener mListener = new LocationListener() {
+        @Override
+        public void onStatusChanged(String provider, int status, Bundle extras) {
+        }
+
+        @Override
+        public void onProviderEnabled(String provider) {
+        }
+
+        @Override
+        public void onProviderDisabled(String provider) {
+        }
+
+        // 如果位置发生变化,重新显示
+        @Override
+        public void onLocationChanged(Location location) {
+            showLocation(location);
+        }
+    };
+
+    public MyLocation(Context context){
+        locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
+    }
+
+    public void showProviders(){
+        if(locationManager == null){
+            Log.e(TAG,"LocationManager is null");
+            return;
+        }
+        List<String> list = locationManager.getAllProviders();
+        if(list != null){
+            for(String name : list){
+                Log.d(TAG,"provider name:" + name);
+            }
+        }
+    }
+
+    public void getLocation(Context context){
+        if(locationManager == null){
+            Log.e(TAG,"LocationManager is null");
+            return;
+        }
+        List<String> providers = locationManager.getProviders(true);
+        String locationProvider;
+        if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
+            //如果是网络定位
+            locationProvider = LocationManager.NETWORK_PROVIDER;
+        } else if (providers.contains(LocationManager.GPS_PROVIDER)) {
+            //如果是GPS定位
+            locationProvider = LocationManager.GPS_PROVIDER;
+        } else if (providers.contains(LocationManager.PASSIVE_PROVIDER)) {
+            //如果是PASSIVE定位
+            locationProvider = LocationManager.PASSIVE_PROVIDER;
+        }
+        else {
+            Log.e(TAG, "没有可用的位置提供器");
+            return;
+        }
+
+        //3.获取上次的位置,一般第一次运行,此值为null
+        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
+                && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
+            // TODO: Consider calling
+            //    ActivityCompat#requestPermissions
+            // here to request the missing permissions, and then overriding
+            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
+            //                                          int[] grantResults)
+            // to handle the case where the user grants the permission. See the documentation
+            // for ActivityCompat#requestPermissions for more details.
+            return;
+        }
+        Location location = locationManager.getLastKnownLocation(locationProvider);
+        if (location != null) {
+            showLocation(location);
+        } else {
+            // 监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace
+            locationManager.requestLocationUpdates(locationProvider, 1000, 200, mListener);
+        }
+    }
+
+    /**
+     * 获取经纬度
+     * @param location
+     */
+    private void showLocation(Location location) {
+        String longtitude=String.valueOf(location.getLongitude());
+        String latitude=String.valueOf(location.getLatitude());
+        Log.e("经纬度信息:",longtitude+"  "+latitude);
+    }
+}

+ 23 - 0
app/src/main/java/com/haochuan/hciptvbasic/test/TestActivity.java

@@ -5,6 +5,7 @@ import android.widget.Button;
 
 import com.haochuan.hciptvbasic.BaseWebActivity;
 import com.haochuan.hciptvbasic.R;
+import com.haochuan.hciptvbasic.Util.MyLocation;
 
 public class TestActivity extends BaseWebActivity {
 
@@ -62,6 +63,8 @@ public class TestActivity extends BaseWebActivity {
         Button exitBtn = findViewById(R.id.exit_btn);
         exitBtn.setOnClickListener(v -> getPlayerToJS().exit());
 
+        //显示位置
+        getLocations();
 
         /*String intentJson = new ToolToJS(this,getWebView()).getIntentJson();
         Logger.d("intentJson:" + intentJson);*/
@@ -74,5 +77,25 @@ public class TestActivity extends BaseWebActivity {
         //toolToJS.download("http://202.99.114.74:56251/dudu_youxi/h5/gameList/apk/jiSuKuangBiao.apk");
     }
 
+    @Override
+    protected void onStart(){
+        super.onStart();
+        //MemoryMonitor.getInstance().start(FloatCurveView.MEMORY_TYPE_PSS);
+    }
+
+    @Override
+    protected void onStop(){
+        super.onStop();
+        //MemoryMonitor.getInstance().stop();
+    }
+
+    /*
+    * 获取位置信息
+    * */
+    private void getLocations(){
+        MyLocation myLocation = new MyLocation(this);
+        myLocation.getLocation(this);
+    }
+
 
 }

+ 12 - 0
app/src/main/java/com/haochuan/hciptvbasic/webview/ToolToJS.java

@@ -114,6 +114,7 @@ public class ToolToJS {
     }
 
 
+
     /*
     * 下载
     * */
@@ -177,6 +178,17 @@ public class ToolToJS {
         ((Activity) context).runOnUiThread(() -> toolsUtil.uninstall(context,pkgName));
     }
 
+    /*
+     * 退出app
+     * */
+    @JavascriptInterface
+    public void appExit(){
+        ((Activity) context).runOnUiThread(() -> {
+            android.os.Process.killProcess(android.os.Process.myPid());   //获取PID
+            System.exit(0);
+        });
+    }
+
     /*---------------------------通过客户端请求接口------------------------*/
 
     /*

+ 2 - 2
gradlew

@@ -78,14 +78,14 @@ if [ -n "$JAVA_HOME" ] ; then
         die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
 
 Please set the JAVA_HOME variable in your environment to match the
-location of your Java installation."
+myLocation of your Java installation."
     fi
 else
     JAVACMD="java"
     which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
 
 Please set the JAVA_HOME variable in your environment to match the
-location of your Java installation."
+myLocation of your Java installation."
 fi
 
 # Increase the maximum file descriptors if we can.

+ 2 - 2
gradlew.bat

@@ -27,7 +27,7 @@ echo.
 echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
 echo.
 echo Please set the JAVA_HOME variable in your environment to match the
-echo location of your Java installation.
+echo myLocation of your Java installation.
 
 goto fail
 
@@ -41,7 +41,7 @@ echo.
 echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
 echo.
 echo Please set the JAVA_HOME variable in your environment to match the
-echo location of your Java installation.
+echo myLocation of your Java installation.
 
 goto fail