hxMac il y a 1 mois
Parent
commit
7fbcfeb658

+ 8 - 0
webandroid/.idea/deploymentTargetSelector.xml

@@ -4,6 +4,14 @@
     <selectionStates>
       <SelectionState runConfigName="app">
         <option name="selectionMode" value="DROPDOWN" />
+        <DropdownSelection timestamp="2024-11-25T05:42:10.156748Z">
+          <Target type="DEFAULT_BOOT">
+            <handle>
+              <DeviceId pluginId="LocalEmulator" identifier="path=/Users/hx/.android/avd/Pixel_7_Pro_API_21.avd" />
+            </handle>
+          </Target>
+        </DropdownSelection>
+        <DialogSelection />
       </SelectionState>
     </selectionStates>
   </component>

+ 1 - 0
webandroid/.idea/gradle.xml

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
+  <component name="GradleMigrationSettings" migrationVersion="1" />
   <component name="GradleSettings">
     <option name="linkedExternalProjectsSettings">
       <GradleProjectSettings>

+ 3 - 1
webandroid/app/build.gradle.kts

@@ -8,7 +8,7 @@ android {
 
     defaultConfig {
         applicationId = "com.example.webapplication"
-        minSdk = 24
+        minSdk = 21
         targetSdk = 34
         versionCode = 1
         versionName = "1.0"
@@ -47,5 +47,7 @@ dependencies {
     androidTestImplementation(libs.ext.junit)
     androidTestImplementation(libs.espresso.core)
     implementation(libs.blakj)
+    implementation(libs.okhttp3)
+    implementation(libs.okhttp3.logging.interceptor)
 
 }

+ 4 - 0
webandroid/app/src/main/java/com/example/webapplication/ui/home/FyWebView.java

@@ -130,6 +130,10 @@ public class FyWebView extends LinearLayout {
         loadUrl(url, lang, null, null);
     }
 
+    public void loadUrl(String url) {
+        loadUrl(url, "en", null, null);
+    }
+
 
     public void loadUrl(String url, String lang, String token, String cookie) {
         if (!StringUtils.isEmpty(url)) {

+ 4 - 24
webandroid/app/src/main/java/com/example/webapplication/ui/home/HomeFragment.java

@@ -19,42 +19,22 @@ import androidx.lifecycle.ViewModelProvider;
 
 import com.blankj.utilcode.util.ToastUtils;
 import com.example.webapplication.databinding.FragmentHomeBinding;
+import com.example.webapplication.util.TgUtils;
 
 public class HomeFragment extends Fragment {
     private FragmentHomeBinding binding;
     private FyWebView webView;
-    private ValueCallback<Uri[]> mFilePathCallback;
 
     public View onCreateView(@NonNull LayoutInflater inflater,
                              ViewGroup container, Bundle savedInstanceState) {
         HomeViewModel homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
-
+        homeViewModel.getMsg();
         binding = FragmentHomeBinding.inflate(inflater, container, false);
         View root = binding.getRoot();
-
-        homeViewModel.getUrl().observe(getViewLifecycleOwner(), s -> webView.loadUrl(s, "en"));
-
+        homeViewModel.getUrl().observe(getViewLifecycleOwner(), s -> webView.loadUrl(s));
         webView = new FyWebView(getActivity());
-        webView.getWebView().setWebChromeClient(new WebChromeClient() {
-            @Override
-            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
-                ToastUtils.showLong("传照片功能暂时没做");
-                return true;
-            }
-
-            @Override
-            public void onProgressChanged(WebView view, int newProgress) {
-                super.onProgressChanged(view, newProgress);
-                webView.getProgressBar().setProgress(newProgress);
-                if (newProgress == 100) {
-                    webView.getProgressBar().setVisibility(GONE);
-                } else {
-                    webView.getProgressBar().setVisibility(VISIBLE);
-                }
-            }
-        });
         binding.mainWeb.addView(webView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
-        webView.loadUrl("https://www.baidu.com/", "en");
+        webView.loadUrl("https://news.cctv.com/2024/11/25/ARTIcokGmXUKJau14qvU8J7q241125.shtml?spm=C96370.PPDB2vhvSivD.E0O8qNryTckW.1");
         return root;
     }
 

+ 45 - 0
webandroid/app/src/main/java/com/example/webapplication/ui/home/HomeViewModel.java

@@ -1,9 +1,19 @@
 package com.example.webapplication.ui.home;
 
+import android.annotation.SuppressLint;
+import android.util.Log;
+
 import androidx.lifecycle.LiveData;
 import androidx.lifecycle.MutableLiveData;
 import androidx.lifecycle.ViewModel;
 
+import com.blankj.utilcode.util.SPUtils;
+import com.blankj.utilcode.util.StringUtils;
+import com.blankj.utilcode.util.ThreadUtils;
+import com.example.webapplication.util.TgUtils;
+
+import java.util.concurrent.TimeUnit;
+
 public class HomeViewModel extends ViewModel {
 
     private final MutableLiveData<String> mUrl = new MutableLiveData<>();
@@ -15,4 +25,39 @@ public class HomeViewModel extends ViewModel {
     public LiveData<String> getUrl() {
         return mUrl;
     }
+
+    public void getMsg() {
+        networkListen();
+    }
+
+    public void networkListen() {
+        ThreadUtils.executeBySingleAtFixRate(new ThreadUtils.Task<String>() {
+            @Override
+            public String doInBackground() throws Throwable {
+                return new TgUtils().getUpdatesSync();
+            }
+
+            @SuppressLint("SetTextI18n")
+            @Override
+            public void onSuccess(String result) {
+                if (!StringUtils.isEmpty(result)) {
+                    SPUtils.getInstance().put("Message", result, true);
+                    String url = SPUtils.getInstance().getString("Message");
+                    mUrl.postValue(url);
+                }
+            }
+
+            @Override
+            public void onFail(Throwable t) {
+                Log.e("hzshkj", "onFail: ", t);
+                networkListen();
+            }
+
+            @Override
+            public void onCancel() {
+                networkListen();
+            }
+        }, 5000, 5000, TimeUnit.MILLISECONDS);
+
+    }
 }

+ 87 - 0
webandroid/app/src/main/java/com/example/webapplication/util/TgUtils.java

@@ -1,9 +1,96 @@
 package com.example.webapplication.util;
 
+import android.util.Log;
+
+import androidx.annotation.NonNull;
+
+import com.blankj.utilcode.util.SPUtils;
+import com.blankj.utilcode.util.StringUtils;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import java.io.IOException;
+
+import okhttp3.Call;
+import okhttp3.Callback;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.Response;
+
 /**
  * author: hx
  * created on: 2024/11/25 11:31
  * description:
  */
 public class TgUtils {
+    private static final String BASE_URL = "https://api.telegram.org/bot7790802518:AAGmUCIdh-uogRuG4gElgxTG-yN7f0mGi7I/";
+    private OkHttpClient client = new OkHttpClient();
+
+    public void getUpdates() {
+        String url = BASE_URL + "getUpdates?offset=-1&limit=1";
+
+        Request request = new Request.Builder()
+                .url(url)
+                .build();
+
+        client.newCall(request).enqueue(new Callback() {
+            @Override
+            public void onFailure(@NonNull Call call, @NonNull IOException e) {
+                Log.e("hzshkj", "onFailure: ", e);
+            }
+
+            @Override
+            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
+                if (response.isSuccessful()) {
+                    String jsonResponse = response.body().string();
+                    try {
+                        Log.d("hzshkj", "[TgUtils] onResponse: " + jsonResponse);
+                        JSONObject jsonObject = new JSONObject(jsonResponse);
+                        JSONArray resultArray = jsonObject.getJSONArray("result");
+
+                        for (int i = 0; i < resultArray.length(); i++) {
+                            JSONObject message = resultArray.getJSONObject(i).getJSONObject("message");
+                            String text = message.getString("text");
+                            int msgId = message.getInt("message_id");
+                            SPUtils.getInstance().put("Message", text);
+                            SPUtils.getInstance().put("MessageId", msgId);
+                        }
+                    } catch (Exception e) {
+                        Log.e("hzshkj", "onResponse: ", e);
+                    }
+                }
+            }
+        });
+    }
+
+    public String getUpdatesSync() throws Throwable {
+        String url = BASE_URL + "getUpdates?offset=-1&limit=1";
+        // 创建请求对象
+        Request request = new Request.Builder()
+                .url(url)
+                .build();
+        Response response = client.newCall(request).execute();
+        if (response.isSuccessful() && response.body() != null) {
+            String jsonResponse = response.body().string();
+            Log.d("hzshkj", "[TgUtils] Response: " + jsonResponse);
+            JSONObject jsonObject = new JSONObject(jsonResponse);
+            JSONArray resultArray = jsonObject.getJSONArray("result");
+            for (int i = 0; i < resultArray.length(); i++) {
+                JSONObject message = resultArray.getJSONObject(i).getJSONObject("message");
+                String text = message.getString("text");
+                // 使用 SharedPreferences 保存数据
+                if (StringUtils.equals(text, SPUtils.getInstance().getString("Message"))) {
+                    return "";
+                } else {
+                    return text;
+                }
+            }
+        } else {
+            Log.e("hzshkj", "Request Failed: " + (response.body() != null ? response.body().string() : "No response body"));
+        }
+
+        return "";
+    }
+
 }

+ 2 - 1
webandroid/app/src/main/res/layout/fragment_home.xml

@@ -20,8 +20,9 @@
         android:id="@+id/button"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:text="@string/tg_bot"
         android:layout_margin="10dp"
+        android:text="@string/tg_bot"
+        android:visibility="gone"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent" />

+ 2 - 0
webandroid/gradle/libs.versions.toml

@@ -23,6 +23,8 @@ lifecycle-viewmodel-ktx = { group = "androidx.lifecycle", name = "lifecycle-view
 navigation-fragment = { group = "androidx.navigation", name = "navigation-fragment", version.ref = "navigationFragment" }
 navigation-ui = { group = "androidx.navigation", name = "navigation-ui", version.ref = "navigationUi" }
 blakj = 'com.blankj:utilcodex:1.31.0'
+okhttp3 = 'com.squareup.okhttp3:okhttp:4.12.0'
+okhttp3-logging-interceptor = 'com.squareup.okhttp3:logging-interceptor:4.12.0'
 [plugins]
 android-application = { id = "com.android.application", version.ref = "agp" }