码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2020-07-21 22:26:20      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:service   gets   art   class   contex   util   sem   ibinder   dem   

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>

 

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

标签:service   gets   art   class   contex   util   sem   ibinder   dem   

原文地址:https://www.cnblogs.com/zoro-zero/p/13356781.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!