如何以程式設計方式關閉 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 初始化按鈕的變數。之後,我們為按鈕添加了一個點選偵聽器。在按鈕的點選偵聽器內部,我們開啟一個關閉意圖以關閉手機。然後,我們將標誌設定為我們的意圖,然後啟動我們的活動。
新增上述程式碼後,我們現在只需點選頂部的綠色圖示即可在移動裝置上執行我們的應用程式。
注意 - 確保您已連線到您的真實裝置或模擬器。
輸出
注意 - 當您點選電源按鈕時,裝置將關閉。
結論
在本文中,我們瞭解瞭如何透過點選 Android 中的按鈕以程式設計方式關閉 Android 手機。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP