From 7c8f07a0faa6df0163232aa5407128e11f3d257a Mon Sep 17 00:00:00 2001 From: wintmain Date: Tue, 8 Oct 2024 13:03:10 +0800 Subject: [PATCH 1/2] [foundation] Clean original learning code-1008 --- .../foundation/src/main/AndroidManifest.xml | 28 +- .../foundation/prejob/upDemo/Ep7_Runable.kt | 292 ++++++++++++++++++ .../foundation/prejob/upDemo/ep7_1.java | 81 ----- .../foundation/prejob/upDemo/ep7_2.java | 117 ------- .../foundation/prejob/upDemo/ep7_3.java | 54 ---- .../foundation/prejob/upDemo/ep7_6.java | 100 ------ 6 files changed, 309 insertions(+), 363 deletions(-) create mode 100644 app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/Ep7_Runable.kt delete mode 100644 app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/ep7_1.java delete mode 100644 app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/ep7_2.java delete mode 100644 app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/ep7_3.java delete mode 100644 app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/ep7_6.java diff --git a/app-catalog/samples/foundation/src/main/AndroidManifest.xml b/app-catalog/samples/foundation/src/main/AndroidManifest.xml index eec4100..728187d 100644 --- a/app-catalog/samples/foundation/src/main/AndroidManifest.xml +++ b/app-catalog/samples/foundation/src/main/AndroidManifest.xml @@ -286,6 +286,10 @@ android:exported="true" android:theme="@style/Theme.AppCompat.DayNight" /> + + @@ -298,24 +302,26 @@ android:exported="true" android:theme="@style/Theme.AppCompat.DayNight" /> - - - - - - - + - - - + + + diff --git a/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/Ep7_Runable.kt b/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/Ep7_Runable.kt new file mode 100644 index 0000000..6d6308b --- /dev/null +++ b/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/Ep7_Runable.kt @@ -0,0 +1,292 @@ +/* + * Copyright 2023-2024 wintmain + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.wintmain.foundation.prejob.upDemo + +import android.media.MediaPlayer +import android.os.Bundle +import android.os.Handler +import android.os.Looper +import android.os.Message +import android.util.Log +import android.widget.Button +import android.widget.LinearLayout +import android.widget.TextView +import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import com.google.android.catalog.framework.annotations.Sample +import com.wintmain.foundation.R +import java.io.IOException +import java.util.Random + +@Sample(name = "ep7_1", description = "线程示例", tags = ["A-Self_demos"]) +class ep7_1 : AppCompatActivity(), Runnable { + var i: Int = 0 // 循环变量 + private var thread: Thread? = null // 声明线程对象 + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + val linearLayout = LinearLayout(this) + setContentView(linearLayout) + if (supportActionBar != null) { + supportActionBar!!.setDisplayHomeAsUpEnabled(true) // 显示返回箭头 + supportActionBar!!.setHomeButtonEnabled(true) // 设置返回键可点击 + } + + val button1 = Button(this) + button1.text = "开启线程" + val button2 = Button(this) + button2.text = "中断线程" + linearLayout.addView(button1) + linearLayout.addView(button2) + + button1.setOnClickListener { + i = 0 + thread = Thread(this@ep7_1) // 创建一个线程 + thread!!.start() // 开启线程 + } + + button2.setOnClickListener { + if (thread != null) { + thread!!.interrupt() // 中断线程 + thread = null + } + Toast.makeText(this, "提示:中断线程", Toast.LENGTH_LONG).show() + } + } + + // 处理返回键事件 + override fun onSupportNavigateUp(): Boolean { + onBackPressed() // 或者你可以 finish() 当前 Activity + return true + } + + override fun onDestroy() { + if (thread != null) { + thread!!.interrupt() + thread = null + } + super.onDestroy() + } + + override fun run() { + while (!Thread.currentThread().isInterrupted) { + i++ + if (i % 1000 == 0) { + Log.i("循环变量:", i.toString()) + } + } + } +} + +@Sample(name = "ep7_2", description = "线程示例", tags = ["A-Self_demos"]) +class ep7_2 : AppCompatActivity() { + private var thread: Thread? = null + private var mediaplayer: MediaPlayer? = null + private var path: String? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + val linearLayout = LinearLayout(this) + setContentView(linearLayout) + if (supportActionBar != null) { + supportActionBar!!.setDisplayHomeAsUpEnabled(true) // 显示返回箭头 + supportActionBar!!.setHomeButtonEnabled(true) // 设置返回键可点击 + } + + val button1 = Button(this) + button1.text = "点我播放音乐" + + linearLayout.addView(button1) + + button1.setOnClickListener { + button1.isEnabled = false // 设置按钮不可用 + + // 创建一个用于播放背景音乐的线程 + thread = + Thread { + try { + display() // 播放背景音乐 + } catch (e: IOException) { + e.printStackTrace() + } + } + thread!!.start() // 开启线程 + } + } + + // 处理返回键事件 + override fun onSupportNavigateUp(): Boolean { + onBackPressed() // 或者你可以 finish() 当前 Activity + return true + } + + @Throws(IOException::class) + private fun display() { + if (mediaplayer != null) { + mediaplayer!!.release() // 释放资源 + } + + // 本地播放 + // mediaplayer = MediaPlayer.create(ep7_2.this,R.raw.xxx); + + // 网络链接 + path = + ("http://218.205.239.34/MIGUM2.0/v1.0/content/sub/listenSong.do?toneFlag=LQ&" + + "netType=00©rightId=0&contentId=600913000000560388&resourceType=2&channel=0") + mediaplayer = MediaPlayer() + mediaplayer!!.setDataSource(path) + mediaplayer!!.prepare() + mediaplayer!!.start() + + // 为MediaPlayer添加播放完成事件监听器 + mediaplayer!!.setOnCompletionListener { + try { + Thread.sleep(5000) + display() // 重新播放音乐 + } catch (e: InterruptedException) { + e.printStackTrace() + } catch (e: IOException) { + e.printStackTrace() + } + } + } + + // 停止播放背景音乐并释放资源 + override fun onDestroy() { + if (mediaplayer != null) { + mediaplayer!!.stop() // 停止播放 + mediaplayer!!.release() // 释放资源 + mediaplayer = null + } + if (thread != null) { + thread = null + } + super.onDestroy() + } +} + +@Sample(name = "ep7_3", description = "线程示例", tags = ["A-Self_demos"]) +class ep7_3 : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + val thread: LooperThread = LooperThread() + thread.start() + } + + internal inner class LooperThread : Thread() { + var handler1: Handler? = null // 声明一个对象 + + override fun run() { + super.run() + Looper.prepare() // 初始化Looper对象 + handler1 = Handler1() + val m = (handler1 as Handler).obtainMessage() // 获取一个消息 + m.what = 0x11 // 设置Message的what属性的值 + (handler1 as Handler).sendMessage(m) // 发送消息 + Looper.loop() // 启动looper + } + } + + class Handler1 : Handler() { + override fun handleMessage(msg: Message) { + Log.i("Wintmain_Looper", msg.what.toString()) + } + } +} + +@Sample(name = "ep7_6", description = "线程示例", tags = ["A-Self_demos"]) +class ep7_6 : AppCompatActivity() { + var bgcolor: IntArray = intArrayOf( + R.color.color1, + R.color.color2, + R.color.color3, + R.color.color4, + R.color.color5, + R.color.color6, + R.color.color7 + ) + private var handler: Handler? = null // 创建Handler对象 + private var index = 0 // 当前颜色值 + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.ep7_6) + if (supportActionBar != null) { + supportActionBar!!.setDisplayHomeAsUpEnabled(true) // 显示返回箭头 + supportActionBar!!.setHomeButtonEnabled(true) // 设置返回键可点击 + } + + // 整体布局 + val linearLayout = findViewById(R.id.line1) + + val height = this.resources.displayMetrics.heightPixels // 获取屏幕的高度 + for (i in tv.indices) { + tv[i] = TextView(this) // 创建一个文本框对象 + tv[i]!!.width = this.resources.displayMetrics.widthPixels // 设置文本框的宽度 + tv[i]!!.height = height / tv.size // 设置文本框的高度 + linearLayout.addView(tv[i]) // 将TextView组件添加到线性布局管理器中 + } + + val t = + Thread { + while (!Thread.currentThread().isInterrupted) { + val m = handler!!.obtainMessage() // 获取一个Message + m.what = 0x101 // 设置消息标识 + handler!!.sendMessage(m) // 发送消息 + try { + Thread.sleep(Random().nextInt(1000).toLong()) // 休眠1秒钟 + } catch (e: InterruptedException) { + e.printStackTrace() // 输出异常信息 + } + } + } + t.start() // 开启线程 + + handler = + object : Handler() { + override fun handleMessage(msg: Message) { + var temp = 0 // 临时变量 + if (msg.what == 0x101) { + for (i in tv.indices) { + temp = Random().nextInt(bgcolor.size) // 产生一个随机数 + // 去掉重复的并且相邻的颜色 + if (index == temp) { + temp++ + if (temp == bgcolor.size) { + temp = 0 + } + } + index = temp + // 为文本框设置背景 + tv[i]!!.setBackgroundColor(resources.getColor(bgcolor[index])) + } + } + super.handleMessage(msg) + } + } + } + + // 处理返回键事件 + override fun onSupportNavigateUp(): Boolean { + onBackPressed() // 或者你可以 finish() 当前 Activity + return true + } + + companion object { + var tv: Array = arrayOfNulls(14) // TextView数组 + } +} diff --git a/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/ep7_1.java b/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/ep7_1.java deleted file mode 100644 index 791c665..0000000 --- a/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/ep7_1.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2023-2024 wintmain - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.wintmain.foundation.prejob.upDemo; - -import android.os.Bundle; -import android.util.Log; -import android.view.View; -import android.widget.Button; -import android.widget.LinearLayout; -import androidx.appcompat.app.AppCompatActivity; - -public class ep7_1 extends AppCompatActivity implements Runnable { - int i; // 循环变量 - private Thread thread; // 声明线程对象 - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - LinearLayout linearLayout = new LinearLayout(this); - setContentView(linearLayout); - Button button1 = new Button(this); - button1.setText("开启线程"); - Button button2 = new Button(this); - button2.setText("中断线程"); - linearLayout.addView(button1); - linearLayout.addView(button2); - - button1.setOnClickListener( - new View.OnClickListener() { - @Override - public void onClick(View view) { - i = 0; - thread = new Thread(ep7_1.this); // 创建一个线程 - thread.start(); // 开启线程 - } - }); - - button2.setOnClickListener( - new View.OnClickListener() { - @Override - public void onClick(View view) { - if (thread != null) { - thread.interrupt(); // 中断线程 - thread = null; - } - Log.i("提示:", "中断线程"); - } - }); - } - - @Override - protected void onDestroy() { - if (thread != null) { - thread.interrupt(); - thread = null; - } - super.onDestroy(); - } - - @Override - public void run() { - while (!Thread.currentThread().isInterrupted()) { - i++; - Log.i("循环变量:", String.valueOf(i)); - } - } -} diff --git a/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/ep7_2.java b/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/ep7_2.java deleted file mode 100644 index dbd458b..0000000 --- a/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/ep7_2.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright 2023-2024 wintmain - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.wintmain.foundation.prejob.upDemo; - -/** - * @Description @Author wintmain @Date 2022-06-12 22:58:39 - */ - -import android.app.Activity; -import android.media.MediaPlayer; -import android.os.Bundle; -import android.view.View; -import android.widget.Button; -import android.widget.LinearLayout; -import androidx.annotation.Nullable; - -import java.io.IOException; - -public class ep7_2 extends Activity { - private Thread thread; - private MediaPlayer mediaplayer; - private String path; - - @Override - protected void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - LinearLayout linearLayout = new LinearLayout(this); - setContentView(linearLayout); - Button button1 = new Button(this); - button1.setText("点我播放音乐"); - - linearLayout.addView(button1); - - button1.setOnClickListener( - new View.OnClickListener() { - @Override - public void onClick(View v) { - button1.setEnabled(false); // 设置按钮不可用 - - // 创建一个用于播放背景音乐的线程 - thread = - new Thread( - new Runnable() { - @Override - public void run() { - try { - display(); // 播放背景音乐 - } catch (IOException e) { - e.printStackTrace(); - } - } - }); - thread.start(); // 开启线程 - } - }); - } - - private void display() throws IOException { - if (mediaplayer != null) { - mediaplayer.release(); // 释放资源 - } - // 本地播放 - // mediaplayer = MediaPlayer.create(ep7_2.this,R.raw.xxx); - - // 网络链接 - path = - "http://218.205.239.34/MIGUM2.0/v1.0/content/sub/listenSong" - + ".do?toneFlag=LQ&netType=00©rightId=0&contentId=600913000000560388" - + "&resourceType=2&channel=0"; - mediaplayer = new MediaPlayer(); - mediaplayer.setDataSource(path); - mediaplayer.prepare(); - mediaplayer.start(); - - // 为MediaPlayer添加播放完成事件监听器 - mediaplayer.setOnCompletionListener( - new MediaPlayer.OnCompletionListener() { - @Override - public void onCompletion(MediaPlayer mp) { - try { - Thread.sleep(5000); - display(); // 重新播放音乐 - } catch (InterruptedException | IOException e) { - e.printStackTrace(); - } - } - }); - } - - // 停止播放背景音乐并释放资源 - @Override - protected void onDestroy() { - if (mediaplayer != null) { - mediaplayer.stop(); // 停止播放 - mediaplayer.release(); // 释放资源 - mediaplayer = null; - } - if (thread != null) { - thread = null; - } - super.onDestroy(); - } -} diff --git a/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/ep7_3.java b/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/ep7_3.java deleted file mode 100644 index ce6f017..0000000 --- a/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/ep7_3.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2023-2024 wintmain - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.wintmain.foundation.prejob.upDemo; - -import android.app.Activity; -import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; -import android.os.Message; -import android.util.Log; -import androidx.annotation.Nullable; - -public class ep7_3 extends Activity { - @Override - protected void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - LooperThread thread = new LooperThread(); - thread.start(); - } - - class LooperThread extends Thread { - public Handler handler1; // 声明一个对象 - - @Override - public void run() { - super.run(); - Looper.prepare(); // 初始化Looper对象 - handler1 = - new Handler() { - public void handleMessage(Message msg) { - Log.i("Wintmain_Looper", String.valueOf(msg.what)); - } - }; - Message m = handler1.obtainMessage(); // 获取一个消息 - m.what = 0x11; // 设置Message的what属性的值 - handler1.sendMessage(m); // 发送消息 - Looper.loop(); // 启动looper - } - } -} diff --git a/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/ep7_6.java b/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/ep7_6.java deleted file mode 100644 index e32ffb6..0000000 --- a/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/ep7_6.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright 2023-2024 wintmain - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.wintmain.foundation.prejob.upDemo; - -import android.app.Activity; -import android.os.Bundle; -import android.os.Handler; -import android.os.Message; -import android.widget.LinearLayout; -import android.widget.TextView; -import androidx.annotation.Nullable; -import com.wintmain.foundation.R; - -import java.util.Random; - -public class ep7_6 extends Activity { - public static TextView[] tv = new TextView[14]; // TextView数组 - int[] bgcolor = - new int[]{ - R.color.color1, - R.color.color2, - R.color.color3, - R.color.color4, - R.color.color5, - R.color.color6, - R.color.color7 - }; - private Handler handler; // 创建Handler对象 - private int index = 0; // 当前颜色值 - - @Override - protected void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.ep7_6); - // 整体布局 - LinearLayout linearLayout = findViewById(R.id.line1); - - int height = this.getResources().getDisplayMetrics().heightPixels; // 获取屏幕的高度 - for (int i = 0; i < tv.length; i++) { - tv[i] = new TextView(this); // 创建一个文本框对象 - tv[i].setWidth(this.getResources().getDisplayMetrics().widthPixels); // 设置文本框的宽度 - tv[i].setHeight(height / tv.length); // 设置文本框的高度 - linearLayout.addView(tv[i]); // 将TextView组件添加到线性布局管理器中 - } - - Thread t = - new Thread( - () -> { - while (!Thread.currentThread().isInterrupted()) { - Message m = handler.obtainMessage(); // 获取一个Message - m.what = 0x101; // 设置消息标识 - handler.sendMessage(m); // 发送消息 - try { - Thread.sleep(new Random().nextInt(1000)); // 休眠1秒钟 - } catch (InterruptedException e) { - e.printStackTrace(); // 输出异常信息 - } - } - }); - t.start(); // 开启线程 - - handler = - new Handler() { - @Override - public void handleMessage(Message msg) { - int temp = 0; // 临时变量 - if (msg.what == 0x101) { - for (int i = 0; i < tv.length; i++) { - temp = new Random().nextInt(bgcolor.length); // 产生一个随机数 - // 去掉重复的并且相邻的颜色 - if (index == temp) { - temp++; - if (temp == bgcolor.length) { - temp = 0; - } - } - index = temp; - // 为文本框设置背景 - tv[i].setBackgroundColor(getResources().getColor(bgcolor[index])); - } - } - super.handleMessage(msg); - } - }; - } -} From 116a43af67a405f6ccbab93ab86eca63f6001823 Mon Sep 17 00:00:00 2001 From: wintmain Date: Tue, 8 Oct 2024 14:04:52 +0800 Subject: [PATCH 2/2] [foundation] Clean original learning code-1008-2 --- .../foundation/src/main/AndroidManifest.xml | 62 ++-- .../upDemo/AnotherBroadcastReceiver.java | 29 -- .../prejob/upDemo/BootCompleteReceiver.java | 29 -- .../foundation/prejob/upDemo/Ep8_Receiver.kt | 269 ++++++++++++++++++ .../prejob/upDemo/LocalBroadcastActivity.java | 69 ----- .../prejob/upDemo/MyBroadcastReceiver.java | 29 -- .../foundation/prejob/upDemo/Receiver1.java | 29 -- .../foundation/prejob/upDemo/Receiver2.java | 29 -- .../foundation/prejob/upDemo/Receiver3.java | 30 -- .../foundation/prejob/upDemo/Receiver4.java | 29 -- .../foundation/prejob/upDemo/ep9_1.java | 63 ---- .../foundation/prejob/upDemo/ep9_3.java | 63 ---- .../foundation/prejob/upDemo/ep9_4.java | 80 ------ 13 files changed, 310 insertions(+), 500 deletions(-) delete mode 100644 app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/AnotherBroadcastReceiver.java delete mode 100644 app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/BootCompleteReceiver.java create mode 100644 app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/Ep8_Receiver.kt delete mode 100644 app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/LocalBroadcastActivity.java delete mode 100644 app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/MyBroadcastReceiver.java delete mode 100644 app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/Receiver1.java delete mode 100644 app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/Receiver2.java delete mode 100644 app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/Receiver3.java delete mode 100644 app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/Receiver4.java delete mode 100644 app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/ep9_1.java delete mode 100644 app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/ep9_3.java delete mode 100644 app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/ep9_4.java diff --git a/app-catalog/samples/foundation/src/main/AndroidManifest.xml b/app-catalog/samples/foundation/src/main/AndroidManifest.xml index 728187d..9b3dbbe 100644 --- a/app-catalog/samples/foundation/src/main/AndroidManifest.xml +++ b/app-catalog/samples/foundation/src/main/AndroidManifest.xml @@ -318,32 +318,52 @@ android:exported="true" android:theme="@style/Theme.AppCompat.DayNight" /> + + + + + + + + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/AnotherBroadcastReceiver.java b/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/AnotherBroadcastReceiver.java deleted file mode 100644 index 87a5833..0000000 --- a/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/AnotherBroadcastReceiver.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2023-2024 wintmain - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.wintmain.foundation.prejob.upDemo; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.widget.Toast; - -public class AnotherBroadcastReceiver extends BroadcastReceiver { - @Override - public void onReceive(Context context, Intent intent) { - Toast.makeText(context, "received in 其他BroadcastReceiver", Toast.LENGTH_SHORT).show(); - } -} diff --git a/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/BootCompleteReceiver.java b/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/BootCompleteReceiver.java deleted file mode 100644 index a584e9a..0000000 --- a/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/BootCompleteReceiver.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2023-2024 wintmain - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.wintmain.foundation.prejob.upDemo; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.widget.Toast; - -public class BootCompleteReceiver extends BroadcastReceiver { - @Override - public void onReceive(Context context, Intent intent) { - Toast.makeText(context, "已开机", Toast.LENGTH_SHORT).show(); - } -} diff --git a/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/Ep8_Receiver.kt b/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/Ep8_Receiver.kt new file mode 100644 index 0000000..78ca4fd --- /dev/null +++ b/app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/prejob/upDemo/Ep8_Receiver.kt @@ -0,0 +1,269 @@ +/* + * Copyright 2023-2024 wintmain + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.wintmain.foundation.prejob.upDemo + +import android.app.Activity +import android.content.BroadcastReceiver +import android.content.ComponentName +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import android.net.ConnectivityManager +import android.os.Build.VERSION_CODES +import android.os.Bundle +import android.widget.Button +import android.widget.LinearLayout +import android.widget.Toast +import androidx.annotation.RequiresApi +import androidx.appcompat.app.AppCompatActivity +import androidx.localbroadcastmanager.content.LocalBroadcastManager +import com.google.android.catalog.framework.annotations.Sample +import com.wintmain.foundation.R + +@Sample(name = "LocalBroadcastActivity", description = "广播示例", tags = ["A-Self_demos"]) +class LocalBroadcastActivity : AppCompatActivity() { + private var localBroadcastTest: LocalBroadcastTest? = null + private var localBroadcastManager: LocalBroadcastManager? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + val linearLayout = LinearLayout(this) + setContentView(linearLayout) + if (supportActionBar != null) { + supportActionBar!!.setDisplayHomeAsUpEnabled(true) // 显示返回箭头 + supportActionBar!!.setHomeButtonEnabled(true) // 设置返回键可点击 + } + + val button = Button(this) + button.text = "测试本地广播" + linearLayout.addView(button) + // 实例化对象 + localBroadcastManager = LocalBroadcastManager.getInstance(this@LocalBroadcastActivity) + localBroadcastTest = LocalBroadcastTest() + // 发送广播 + button.setOnClickListener { + val intent = Intent() + intent.setAction("LOCAL") + localBroadcastManager!!.sendBroadcast(intent) + } + // 动态注册接收器 + val intentFilter = IntentFilter() + intentFilter.addAction("LOCAL") + localBroadcastManager!!.registerReceiver(localBroadcastTest!!, intentFilter) + } + + // 处理返回键事件 + override fun onSupportNavigateUp(): Boolean { + onBackPressed() // 或者你可以 finish() 当前 Activity + return true + } + + internal inner class LocalBroadcastTest : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + Toast.makeText(context, "本地广播接收器已接受", Toast.LENGTH_SHORT).show() + } + } +} + +@Sample(name = "广播动态注册", description = "监听网络变化示例", tags = ["A-Self_demos"]) +class ep9_1 : AppCompatActivity() { + private var networkChangeReceiver: NetworkChangeReceiver? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.ep9_1) + if (supportActionBar != null) { + supportActionBar!!.setDisplayHomeAsUpEnabled(true) // 显示返回箭头 + supportActionBar!!.setHomeButtonEnabled(true) // 设置返回键可点击 + } + + val intentFilter = IntentFilter() + intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE") + networkChangeReceiver = NetworkChangeReceiver() + registerReceiver(networkChangeReceiver, intentFilter) + } + + // 处理返回键事件 + override fun onSupportNavigateUp(): Boolean { + onBackPressed() // 或者你可以 finish() 当前 Activity + return true + } + + override fun onDestroy() { + super.onDestroy() + unregisterReceiver(networkChangeReceiver) + } + + internal inner class NetworkChangeReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + val connectivityManager = + getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager + val info = connectivityManager.activeNetworkInfo + if (info != null && info.isAvailable) { + Toast.makeText(context, "网络已连接", Toast.LENGTH_SHORT).show() + } else { + Toast.makeText(context, "网络已断开", Toast.LENGTH_SHORT).show() + } + } + } +} + +@Sample(name = "广播", description = "发送自定义广播示例", tags = ["A-Self_demos"]) +class ep9_3 : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.ep9_3) + if (supportActionBar != null) { + supportActionBar!!.setDisplayHomeAsUpEnabled(true) // 显示返回箭头 + supportActionBar!!.setHomeButtonEnabled(true) // 设置返回键可点击 + } + + val button1 = findViewById