英文:
Android Java Service to detect screen state
问题
First ask:
要正确每秒输出屏幕状态,请将以下代码段更改如下:
public class MyService extends Service {
private static final String TAG = "TAGG";
private boolean screenOn = true; // 初始状态假定为屏幕开启
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public int xMin = 1 * 60 * 24 * 30; // 工作一个月
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
CountDownTimer countDownTimer = new CountDownTimer(
60000 * xMin, 1000) {
@Override
public void onTick(long millisUntilFinished) {
Log.e(TAG, "onTick: " + millisUntilFinished / 1000);
if (screenOn) {
Log.d(TAG, "onTick: ON");
} else {
Log.d(TAG, "onTick: OFF");
}
}
@Override
public void onFinish() {
Log.e(TAG, "onFinish: ");
}
}.start();
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
// 注册处理屏幕开启和屏幕关闭逻辑的广播接收器
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
// 在广播接收器中更新屏幕状态
public class ScreenReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOn = true;
}
}
}
}
第二个问题:
您的一般想法是创建一个服务,当按下按钮后启动该服务,然后每秒检查/修改系统参数,只有在屏幕打开时才执行此操作(应用可以最小化)。当屏幕关闭时,最好暂停服务,但是找不到方法。您已经知道如何检查系统参数。也许有一种比我的方法更简单的方法吗?
这个问题涉及到Android系统的一些复杂性,但您可以尝试以下方法来实现您的目标:
-
使用
PowerManager
来检查屏幕状态:
在MyService
中,您可以使用PowerManager
来检查屏幕状态,如果屏幕关闭,则暂停您的操作。示例代码如下:PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); boolean screenOn = powerManager.isInteractive();
-
使用
JobScheduler
:
考虑使用JobScheduler
来执行定期任务,以检查和修改系统参数。这样,您可以根据屏幕状态来触发您的任务。当屏幕关闭时,可以取消或延迟您的任务。这将更有效地处理后台任务。
这两种方法都可以帮助您更好地控制屏幕状态下的任务执行。请根据您的需求选择其中之一。
英文:
I am PHP, in oher words newby in JAVA.
I wrote service which every second checks if the screen is on or off, but need help, cuz at last step my Log.d shows "on off on off.." instead of showing "on on on..." when screen is on (and "off off off..." when screen is OFF.
Simple main activity:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "TAGG";
private Button startServiceButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startServiceButton = findViewById(R.id.startService);
startServiceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startService(new Intent(getApplication(), MyService.class));
}
});
}
}
..and receiver:
public class ScreenReceiver extends BroadcastReceiver {
private boolean screenOff;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
}
Intent i = new Intent(context, MyService.class);
i.putExtra("screen_state", screenOff);
context.startService(i);
}
}
.. and here is the problem:
public class MyService extends Service {
private static final String TAG = "TAGG";
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public int xMin = 1*60*24*30;//work a month
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
CountDownTimer countDownTimer = new CountDownTimer(
60000*xMin,1000){
@Override
public void onTick(long millisUntilFinished) {
Log.e(TAG, "onTick: "+millisUntilFinished/1000 );
boolean screenOn = intent.getBooleanExtra("screen_state", false);
if (!screenOn) {
Log.d(TAG, "onTick: OFF");
} else {
Log.d(TAG, "onTick: ON");
}
}
@Override
public void onFinish() {
Log.e(TAG, "onFinish: " );
}
}.start();
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
// REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
}
First main ask:
Please tell me how to correctly output state of screen each second.
Second ask:
ps General idea - is to create a service which starts after button press and then checks/modifies every second system parameter only when the screen is on (the app can be minimised). When screen is off it would be good to pause service, but did not found out how. How to check system parameter I know. Maybe there is simple way to do this then my way?
答案1
得分: 2
在 Activity 中:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isInteractive();
在 Fragment 中:
PowerManager pm = (PowerManager) requireContext().getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isInteractive();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论