带有前台服务的定时器任务

问题描述 投票:0回答:1

我正在主线程中执行计时器任务,并且每 15 秒更新一次用户界面。我还在前台服务中运行另一个计时器任务(当用户打开它时。它的目标是备份数据等后台操作),其中包含一个通知,该通知使计时器任务代码即使在应用程序关闭时也能继续运行。 无论如何,我是否可以拥有前台服务,但停止该计时器任务。

我已经做了类似的事情,但它不起作用,因为它说用户由于该通知而在屏幕上:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isInteractive();
if(!isScreenOn){
    MainActivity.timer.cancel();
    MainActivity.timer.purge();
 }
java android background-service timertask foreground-service
1个回答
0
投票

你可以使用BroadcastReceiver来做到这一点。

这里是一些使用BroadcastReceiver的设置。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">

    <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/AppTheme">
        <!-- Other components -->

        <receiver
            android:name=".ScreenStateReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.SCREEN_OFF" />
                <action android:name="android.intent.action.SCREEN_ON" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

将此权限添加到清单中

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

ScreenStateReceiver.java 文件

public class ScreenStateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
            MainActivity.stopTimerTask();
        } else if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) {
            MainActivity.startTimerTaskIfNecessary();
        }
    }
}

// In your MainActivity
public static void startTimerTaskIfNecessary() {
    if (timer == null) {
        timer = new Timer();
        // Schedule your task here
    }
}

public static void stopTimerTask() {
    if (timer != null) {
        timer.cancel();
        timer.purge();
        timer = null;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private ScreenStateReceiver screenStateReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (screenStateReceiver == null) {
            screenStateReceiver = new ScreenStateReceiver();
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            filter.addAction(Intent.ACTION_SCREEN_ON);
            registerReceiver(screenStateReceiver, filter);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (screenStateReceiver != null) {
            unregisterReceiver(screenStateReceiver);
            screenStateReceiver = null;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.