如何以编程方式关闭 Android 手机?
简介
Android 手机设计了广泛的功能和特性,包括电源按钮,允许用户打开和关闭设备。Android 设备中的电源按钮用于打开和关闭移动设备。在本文中,我们将了解如何以编程方式关闭 Android 手机。
实现
我们将创建一个简单的项目,其中我们将显示一个简单的文本视图以显示标题,我们将在其中显示简单的标题。除了这个文本视图,我们还将创建一个按钮。点击此按钮,我们将关闭手机。
步骤 1:在 Android Studio 中创建一个新项目
导航到 Android Studio,如下面的屏幕所示。在下面的屏幕中,点击“新建项目”以创建一个新的 Android Studio 项目

点击“新建项目”后,您将看到下面的屏幕。

在此屏幕中,我们只需选择“空活动”并点击“下一步”。点击“下一步”后,您将看到下面的屏幕。

在此屏幕中,我们只需指定项目名称。然后包名将自动生成。
注意 - 确保将语言选择为 Java。
指定所有详细信息后,点击“完成”以创建一个新的 Android Studio 项目。
创建项目后,我们将看到两个打开的文件,即 activity_main.xml 和 MainActivity.java 文件。
步骤 3:使用 activity_main.xml
导航到 activity_main.xml。如果此文件不可见。要打开此文件。在左侧窗格中导航到 app>res>layout>activity_main.xml 以打开此文件。打开此文件后。将以下代码添加到其中。代码中添加了注释以详细了解。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context=".MainActivity"> <!-- on below line creating a text view for displaying a heading--> <TextView android:id="@+id/idTVHeading" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="10dp" android:padding="4dp" android:text="Programatically Switch Off Android Phone" android:textAlignment="center" android:textColor="@color/black" android:textSize="20sp" android:textStyle="bold" /> <!-- creating a button to switch off the device--> <Button android:id="@+id/idBtnSwitchOff" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/idTVHeading" android:layout_centerInParent="true" android:layout_margin="10dp" android:text="Switch Off" android:textAllCaps="false" /> </RelativeLayout>
说明 - 在上面的代码中,我们创建了一个相对布局作为根布局,并在其中创建了一个简单的文本视图,我们在其中显示一个简单的文本视图,我们在其中显示应用程序的标题。在此文本视图之后,我们创建了一个按钮,我们将使用它来关闭手机。
步骤 4:使用 MainActivity.java 文件
导航到 MainActivity.java。如果此文件不可见。要打开此文件。在左侧窗格中导航到 app>res>layout>MainActivity.java 以打开此文件。打开此文件后。将以下代码添加到其中。代码中添加了注释以详细了解。
package com.example.androidjavaapp; import androidx.appcompat.app.AppCompatActivity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.ContextWrapper; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.RequestFuture; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.concurrent.TimeUnit; public class MainActivity extends AppCompatActivity { // on below line we are creating variable for button. private Button switchOffBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // on below line initializing web view with id. switchOffBtn = findViewById(R.id.idBtnSwitchOff); // on below line adding click listener for our switch off button. switchOffBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // on below line we are setting our shut down intent. Intent intent = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN"); // on below line we are setting confirm t false. intent.putExtra("android.intent.extra.KEY_CONFIRM", false); // on below line we are adding a flag for new task. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // on below line we are starting a new activity. startActivity(intent); } }); } }
说明 - 在上面的代码中,我们首先为按钮创建一个变量。之后,我们将看到一个 onCreate 方法。在此方法内部,我们将看到一个 setContentView 方法。此方法将从 activity_main.xml 文件加载 UI。之后,我们正在从我们在 activity_main.xml 文件中指定的 id 初始化按钮的变量。之后,我们为按钮添加了一个点击侦听器。在按钮的点击侦听器内部,我们打开一个关闭意图以关闭手机。然后,我们将标志设置为我们的意图,然后启动我们的活动。
添加上述代码后,我们现在只需点击顶部的绿色图标即可在移动设备上运行我们的应用程序。
注意 - 确保您已连接到您的真实设备或模拟器。
输出

注意 - 当您点击电源按钮时,设备将关闭。
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
结论
在本文中,我们了解了如何通过点击 Android 中的按钮以编程方式关闭 Android 手机。