lmw
2023-03-11 4df5bb59e5fe9f9d140e5610f7772dd8a05a28d4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package cn.sinata.rxnetty;
 
import android.Manifest;
import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
 
import androidx.core.content.ContextCompat;
 
import java.util.List;
 
/**
 *
 * Created by liaoxiang on 17/1/22.
 */
 
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class NJobService extends android.app.job.JobService {
 
    @Override
    public void onCreate() {
        super.onCreate();
        jobScheduler();
    }
 
    public void jobScheduler() {
        try {
            int id = 2010;
            JobInfo.Builder builder = new JobInfo.Builder(id,
                    new ComponentName(getPackageName(), NJobService.class.getName()));
            builder.setPeriodic(60000);
            builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_BOOT_COMPLETED) == PackageManager.PERMISSION_GRANTED) {
                builder.setPersisted(true);
            }
            JobScheduler jobScheduler = (JobScheduler) this.getSystemService(Context.JOB_SCHEDULER_SERVICE);
            jobScheduler.schedule(builder.build());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.err.println("-----p name ---0--->"+getPackageName());
        //如果app已经没有运行,停止
        if (!isAppRun(this, getPackageName())) {
            stopSelf();
            return START_NOT_STICKY;
        }
        if (Build.VERSION.SDK_INT >= 24) {
            if (!isServiceWork(this, "cn.sinata.rxnetty.CoreService")) {
                this.startService(new Intent(this.getApplicationContext(), CoreService.class));
//                this.jobFinished(params, false);
            }
        }
        return START_NOT_STICKY;
    }
 
    @Override
    public boolean onStartJob(JobParameters params) {
        System.err.println("-----p name --1---->"+getPackageName());
        //如果app已经没有运行,停止
        if (!isAppRun(this, getPackageName())) {
            stopSelf();
            return false;
        }
        if (!isServiceWork(this, "cn.sinata.rxnetty.CoreService")) {
            this.startService(new Intent(this.getApplicationContext(), CoreService.class));
            this.jobFinished(params, false);
        }
        return false;
    }
 
    @Override
    public boolean onStopJob(JobParameters params) {
        return false;
    }
 
    // 判断服务是否正在运行
    public boolean isServiceWork(Context mContext, String serviceName) {
        boolean isWork = false;
        ActivityManager myAM = (ActivityManager) mContext
                .getSystemService(Context.ACTIVITY_SERVICE);
        if (myAM == null) {
            return false;
        }
        List<ActivityManager.RunningServiceInfo> myList = myAM.getRunningServices(100);
        if (myList.size() <= 0) {
            return false;
        }
        for (int i = 0; i < myList.size(); i++) {
            String mName = myList.get(i).service.getClassName();
            if (mName.equals(serviceName)) {
                isWork = true;
                break;
            }
        }
        return isWork;
    }
 
    // 判断app是否正在运行
    public boolean isAppRun(Context mContext, String packegeName) {
        boolean isWork = false;
        ActivityManager myAM = (ActivityManager) mContext
                .getSystemService(Context.ACTIVITY_SERVICE);
        if (myAM == null) {
            return false;
        }
        List<ActivityManager.RunningTaskInfo> myList = myAM.getRunningTasks(100);
        if (myList.size() <= 0) {
            return false;
        }
        for (int i = 0; i < myList.size(); i++) {
            String mName = myList.get(i).baseActivity.getPackageName();
            if (mName.equals(packegeName)) {
                isWork = true;
                break;
            }
        }
        return isWork;
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        System.err.println("-------onDestroy--job-->");
    }
}