Android Service在后台一直运行监测案例

1、定义 Service

复制代码
package com.example.scangundemo_as;

import android.app.ActivityManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.res.Resources;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import java.util.List;
import java.util.logging.Logger;

public class APPService extends Service {

    private static final String packageName = "com.example.scangundemo_as";
    private static final String className = "MainActivity";

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("Service","-------create-------");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("Service","-------onDestroy-------");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("Service","-----service onStartCommand...");
        new Thread(){
            @Override
            public void run() {
                super.run();
                while(true){
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //一直运行
                    boolean isRunningForeground = isRunningForeground(APPService.this);
//                    Log.i("Service","-----service isRunningForeground="+isRunningForeground);
                    if(!isRunningForeground){
                        Log.i("Service","-----start activity-----");
                        startActivityTool(APPService.this);
                    }
                }
            }
        }.start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
public static boolean isRunningForeground (Context context) { ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); ComponentName cn = am.getRunningTasks(1).get(0).topActivity; String currentPackageName = cn.getPackageName(); if(!TextUtils.isEmpty(currentPackageName) && currentPackageName.equals(context.getPackageName())) { return true ; } return false ; } public static void startActivityTool(Context context){ Intent intent = new Intent(); intent.setClass(context, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); context.startActivity(intent); } }
复制代码

2、启动Service

  //启动service
Intent intent = new Intent(this,APPService.class);
startService(intent);

3、清单文件AndroidManifest.xml 声明Service

  <service android:name=".APPService" android:enabled="true" android:exported="true">
  </service>

 

posted @   大空白纸  阅读(908)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示