如何让设备保持清醒?
How to keep the device awake?
我在我的 app.and 中使用了一个 volley 库 json 占位符 url api call.i 正在测量之后的响应时间api call.i 也想保留设备或频道 awake.which 是实现此方法的方法。
主要ActivityClass
-我尝试使用 Flag_keep_screen_on 但它仍然对我不起作用。
package com.example.volleydemo;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.ThreadLocalRandom;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button Click;
TextView responseText, timerText, timeLast;
Date endTime;
long startTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
endTime = Calendar.getInstance().getTime();
Click = findViewById(R.id.button);
Click.setOnClickListener(this);
responseText = findViewById(R.id.textView);
timerText = findViewById(R.id.textView2);
timeLast = findViewById(R.id.textView3);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
@SuppressLint("SetTextI18n")
@Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.button) {
timerText.setVisibility(View.VISIBLE);
timeLast.setVisibility(View.VISIBLE);
startTime = System.currentTimeMillis();
timeLast.setText("Request made at : " + getDate(startTime, "dd/MM/yyyy hh:mm:ss.SS"));
int randomInt = ThreadLocalRandom.current().nextInt(1, 200);
RequestQueue requestQueue;
requestQueue = Volley.newRequestQueue(getApplicationContext());
@SuppressLint("SetTextI18n") JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
"https://jsonplaceholder.typicode.com/todos/" + randomInt,
null, response -> {
try {
long elapsedTime = System.currentTimeMillis() - startTime;
timerText.setText("Request Completed within : "
+ convertSecondsToHMmSs(elapsedTime));
responseText.setVisibility(View.VISIBLE);
responseText.setText(" ID" + response.getString("id") + "\n Title-" +
response.getString("title") + "\n Completed-" +
response.getString("completed") + "\n User ID"
+ response.getString("userId") + "\n");
} catch (JSONException e) {
e.printStackTrace();
}
}, error -> Log.d("myapp", "something went wrong"));
requestQueue.add(jsonObjectRequest);
}
}
@SuppressLint("DefaultLocale")
public static String convertSecondsToHMmSs(long seconds) {
long s = seconds % 60;
long m = (seconds / 60) % 60;
long h = (seconds / (60 * 60)) % 24;
return String.format("%d Minute:%02d Second:%02d MilliSecond", h, m, s);
}
public static String getDate(long milliSeconds, String dateFormat) {
@SuppressLint("SimpleDateFormat") SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
}
XML 代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/Click"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/app_name"
android:textStyle="bold"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/app_name"
android:textStyle="bold"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/app_name"
android:textStyle="bold"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
</androidx.constraintlayout.widget.ConstraintLayout>
清单文件-
我在这里使用了唤醒锁权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.volleydemo">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<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.VolleyDemo">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
你可以试试这个
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyApp::MyWakelockTag");
wakeLock.acquire();
释放唤醒锁使用wakelock.release()
参考保持设备唤醒https://developer.android.com/training/scheduling/wakelock
Add Permission in Manifest
<uses-permission android:name="android.permission.WAKE_LOCK" />
Code
override fun onCreate(savedInstanceState: Bundle?) {
............
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK,
"MyApp::WakeLog");
wakeLock.acquire();
}
@Override
public void onResume() {
wakeLock.aquire();
}
@Override
public void onDestroy() {
wakeLock.release();
}
试试这个
仅将此添加到您的 Activity
import android.view.WindowManager;
@Override
public void onResume() {
super.onResume();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
@Override
public void onPause() {
super.onPause();
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
我在我的 app.and 中使用了一个 volley 库 json 占位符 url api call.i 正在测量之后的响应时间api call.i 也想保留设备或频道 awake.which 是实现此方法的方法。
主要ActivityClass -我尝试使用 Flag_keep_screen_on 但它仍然对我不起作用。
package com.example.volleydemo;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.ThreadLocalRandom;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button Click;
TextView responseText, timerText, timeLast;
Date endTime;
long startTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
endTime = Calendar.getInstance().getTime();
Click = findViewById(R.id.button);
Click.setOnClickListener(this);
responseText = findViewById(R.id.textView);
timerText = findViewById(R.id.textView2);
timeLast = findViewById(R.id.textView3);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
@SuppressLint("SetTextI18n")
@Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.button) {
timerText.setVisibility(View.VISIBLE);
timeLast.setVisibility(View.VISIBLE);
startTime = System.currentTimeMillis();
timeLast.setText("Request made at : " + getDate(startTime, "dd/MM/yyyy hh:mm:ss.SS"));
int randomInt = ThreadLocalRandom.current().nextInt(1, 200);
RequestQueue requestQueue;
requestQueue = Volley.newRequestQueue(getApplicationContext());
@SuppressLint("SetTextI18n") JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
"https://jsonplaceholder.typicode.com/todos/" + randomInt,
null, response -> {
try {
long elapsedTime = System.currentTimeMillis() - startTime;
timerText.setText("Request Completed within : "
+ convertSecondsToHMmSs(elapsedTime));
responseText.setVisibility(View.VISIBLE);
responseText.setText(" ID" + response.getString("id") + "\n Title-" +
response.getString("title") + "\n Completed-" +
response.getString("completed") + "\n User ID"
+ response.getString("userId") + "\n");
} catch (JSONException e) {
e.printStackTrace();
}
}, error -> Log.d("myapp", "something went wrong"));
requestQueue.add(jsonObjectRequest);
}
}
@SuppressLint("DefaultLocale")
public static String convertSecondsToHMmSs(long seconds) {
long s = seconds % 60;
long m = (seconds / 60) % 60;
long h = (seconds / (60 * 60)) % 24;
return String.format("%d Minute:%02d Second:%02d MilliSecond", h, m, s);
}
public static String getDate(long milliSeconds, String dateFormat) {
@SuppressLint("SimpleDateFormat") SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
}
XML 代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/Click"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/app_name"
android:textStyle="bold"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/app_name"
android:textStyle="bold"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/app_name"
android:textStyle="bold"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
</androidx.constraintlayout.widget.ConstraintLayout>
清单文件- 我在这里使用了唤醒锁权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.volleydemo">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<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.VolleyDemo">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
你可以试试这个
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyApp::MyWakelockTag");
wakeLock.acquire();
释放唤醒锁使用wakelock.release()
参考保持设备唤醒https://developer.android.com/training/scheduling/wakelock
Add Permission in Manifest
<uses-permission android:name="android.permission.WAKE_LOCK" />
Code
override fun onCreate(savedInstanceState: Bundle?) {
............
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK,
"MyApp::WakeLog");
wakeLock.acquire();
}
@Override
public void onResume() {
wakeLock.aquire();
}
@Override
public void onDestroy() {
wakeLock.release();
}
试试这个
仅将此添加到您的 Activity
import android.view.WindowManager;
@Override
public void onResume() {
super.onResume();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
@Override
public void onPause() {
super.onPause();
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}