Broadcast组件——收发广播应用——捕获屏幕的变更事件——竖屏与横屏切换
============================================================================================
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <TextView android:id="@+id/tv_monitor" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请旋转手机使屏幕在竖屏与横屏之间切换" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout>
代码:
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.res.Configuration; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView tv_monitor; // 声明一个文本视图对象 private String mDesc = ""; // 屏幕变更的描述说明 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_monitor = findViewById(R.id.tv_monitor); } // 在配置项变更时触发。比如屏幕方向发生变更等等 // 有的手机需要在系统的“设置→显示”菜单开启“自动旋转屏幕”,或者从顶部下拉,找到“自动旋转”图标并开启 @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // 判断当前的屏幕方向 switch (newConfig.orientation) { case Configuration.ORIENTATION_PORTRAIT: // 切换到竖屏 mDesc = String.format("%s%s %s\n", mDesc, DateUtil.getNowTime(), "当前屏幕为竖屏方向"); tv_monitor.setText(mDesc); break; case Configuration.ORIENTATION_LANDSCAPE: // 切换到横屏 mDesc = String.format("%s%s %s\n", mDesc, DateUtil.getNowTime(), "当前屏幕为横屏方向"); tv_monitor.setText(mDesc); break; default: break; } } }
mainifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication"> <uses-permission android:name="android.permission.VIBRATE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MyApplication"> <activity android:name=".MainActivity" android:configChanges="orientation|screenLayout|screenSize" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
DateUtil:
package com.example.myapplication; import android.annotation.SuppressLint; import android.text.TextUtils; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; @SuppressLint("SimpleDateFormat") public class DateUtil { // 获取当前的日期时间 public static String getNowDateTime(String formatStr) { String format = formatStr; if (TextUtils.isEmpty(format)) { format = "yyyyMMddHHmmss"; } SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(new Date()); } // 获取当前的时间 public static String getNowTime() { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); return sdf.format(new Date()); } // 获取当前的时间(精确到毫秒) public static String getNowTimeDetail() { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS"); return sdf.format(new Date()); } public static String getNowDate() { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); return sdf.format(new Date()); } public static String getDate(Calendar calendar) { Date date = calendar.getTime(); // 创建一个日期格式化的工具 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 将当前日期时间按照指定格式输出格式化后的日期时间字符串 return sdf.format(date); } public static String getMonth(Calendar calendar) { Date date = calendar.getTime(); // 创建一个日期格式化的工具 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); // 将当前日期时间按照指定格式输出格式化后的日期时间字符串 return sdf.format(date); } public static Date formatString(String strTime) { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { date = sdf.parse(strTime); } catch (Exception e) { e.printStackTrace(); } return date; } }
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp" > <TextView android:id="@+id/tv_life" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="left" android:textColor="#000000" android:textSize="17sp" /> </LinearLayout>
act:
package com.example.chapter09; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import com.example.chapter09.util.DateUtil; public class ActTestActivity extends AppCompatActivity { private final static String TAG = "ActTestActivity"; private TextView tv_life; // 声明一个文本视图对象 private String mStr = ""; private void refreshLife(String desc) { // 刷新生命周期的日志信息 Log.d(TAG, desc); mStr = String.format("%s%s %s %s\n", mStr, DateUtil.getNowTimeDetail(), TAG, desc); tv_life.setText(mStr); } @Override protected void onCreate(Bundle savedInstanceState) { // 创建活动页面 super.onCreate(savedInstanceState); setContentView(R.layout.activity_act_test); tv_life = findViewById(R.id.tv_life); refreshLife("onCreate"); } @Override protected void onStart() { // 开始活动页面 super.onStart(); refreshLife("onStart"); } @Override protected void onStop() { // 停止活动页面 super.onStop(); refreshLife("onStop"); } @Override protected void onResume() { // 恢复活动页面 super.onResume(); refreshLife("onResume"); } @Override protected void onPause() { // 暂停活动页面 super.onPause(); refreshLife("onPause"); } @Override protected void onRestart() { // 重启活动页面 super.onRestart(); refreshLife("onRestart"); } @Override protected void onDestroy() { // 销毁活动页面 super.onDestroy(); refreshLife("onDestroy"); } }