├── app
├── .gitignore
├── libs
│ └── ddpush-1.0.02.jar
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ └── layout
│ │ │ │ └── activity_main.xml
│ │ ├── aidl
│ │ │ └── service
│ │ │ │ └── keppliveservice
│ │ │ │ └── StrongService.aidl
│ │ ├── java
│ │ │ └── service
│ │ │ │ └── keppliveservice
│ │ │ │ ├── StrongService.aidl
│ │ │ │ ├── Params.java
│ │ │ │ ├── receiver
│ │ │ │ ├── BootAlarmReceiver.java
│ │ │ │ ├── TickAlarmReceiver.java
│ │ │ │ └── KeepLiveReceivers.java
│ │ │ │ ├── DateTimeUtil.java
│ │ │ │ ├── service
│ │ │ │ ├── MyJobService.java
│ │ │ │ ├── KeepLiveService.java
│ │ │ │ └── OnLineService.java
│ │ │ │ ├── Util.java
│ │ │ │ ├── KeepLiveUtils.java
│ │ │ │ └── MainActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── service
│ │ │ └── keppliveservice
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── service
│ │ └── keppliveservice
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── README.md
├── .idea
├── copyright
│ └── profiles_settings.xml
├── modules.xml
├── runConfigurations.xml
├── gradle.xml
├── compiler.xml
└── misc.xml
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/app/libs/ddpush-1.0.02.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Ahuanghaifeng/MessagePush/HEAD/app/libs/ddpush-1.0.02.jar
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # MessagePush
2 | android消息推送,服务保活研究
3 | 详细介绍地址http://blog.csdn.net/u013692888/article/details/77914181
4 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | keppLiveService
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Ahuanghaifeng/MessagePush/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Ahuanghaifeng/MessagePush/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Ahuanghaifeng/MessagePush/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Ahuanghaifeng/MessagePush/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Ahuanghaifeng/MessagePush/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Ahuanghaifeng/MessagePush/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Ahuanghaifeng/MessagePush/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Ahuanghaifeng/MessagePush/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Ahuanghaifeng/MessagePush/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Ahuanghaifeng/MessagePush/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Ahuanghaifeng/MessagePush/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Aug 24 14:20:37 CST 2017
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/aidl/service/keppliveservice/StrongService.aidl:
--------------------------------------------------------------------------------
1 | // StrongService.aidl
2 | package service.keppliveservice;
3 |
4 | // Declare any non-default types here with import statements
5 |
6 | interface StrongService {
7 | /**
8 | * Demonstrates some basic types that you can use as parameters
9 | * and return values in AIDL.
10 | */
11 | void StartService();
12 | void stopService();
13 | }
14 |
--------------------------------------------------------------------------------
/app/src/main/java/service/keppliveservice/StrongService.aidl:
--------------------------------------------------------------------------------
1 | // StrongService.aidl
2 | package service.keppliveservice;
3 |
4 | // Declare any non-default types here with import statements
5 |
6 | interface StrongService {
7 | /**
8 | * Demonstrates some basic types that you can use as parameters
9 | * and return values in AIDL.
10 | */
11 | void StartService();
12 | void stopService();
13 | }
14 |
--------------------------------------------------------------------------------
/app/src/main/java/service/keppliveservice/Params.java:
--------------------------------------------------------------------------------
1 | package service.keppliveservice;
2 |
3 | public class Params {
4 | public static String DEFAULT_PRE_NAME = "defaultAccount";
5 | public static String SERVER_IP = "serverIp";
6 | public static String SERVER_PORT = "serverPort";
7 | public static String PUSH_PORT = "pushPort";
8 | public static String USER_NAME = "userName";
9 | public static String SENT_PKGS = "sentPkgs";
10 | public static String RECEIVE_PKGS = "receivePkgs";
11 | }
12 |
--------------------------------------------------------------------------------
/app/src/test/java/service/keppliveservice/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package service.keppliveservice;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/app/src/main/java/service/keppliveservice/receiver/BootAlarmReceiver.java:
--------------------------------------------------------------------------------
1 | package service.keppliveservice.receiver;
2 |
3 | import android.content.BroadcastReceiver;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.util.Log;
7 |
8 | import service.keppliveservice.service.OnLineService;
9 |
10 |
11 | public class BootAlarmReceiver extends BroadcastReceiver {
12 |
13 | public BootAlarmReceiver() {
14 |
15 | }
16 |
17 | @Override
18 | public void onReceive(Context context, Intent intent) {
19 | Log.e("91ysdk", "开机广播");
20 | Intent startSrv = new Intent(context, OnLineService.class);
21 | startSrv.putExtra("CMD", "TICK");
22 | context.startService(startSrv);
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/service/keppliveservice/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package service.keppliveservice;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("service.keppliveservice", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/java/service/keppliveservice/receiver/TickAlarmReceiver.java:
--------------------------------------------------------------------------------
1 | package service.keppliveservice.receiver;
2 |
3 | import android.content.BroadcastReceiver;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.os.PowerManager.WakeLock;
7 | import android.util.Log;
8 |
9 | import service.keppliveservice.KeepLiveUtils;
10 | import service.keppliveservice.service.OnLineService;
11 |
12 |
13 | public class TickAlarmReceiver extends BroadcastReceiver {
14 |
15 | WakeLock wakeLock;
16 |
17 | public TickAlarmReceiver() {
18 |
19 | }
20 |
21 | @Override
22 | public void onReceive(Context context, Intent intent) {
23 | Log.e("91ysdk", "收到闹钟广播");
24 | if(KeepLiveUtils.hasNetwork(context) == false){
25 | return;
26 | }
27 | Intent startSrv = new Intent(context, OnLineService.class);
28 | startSrv.putExtra("CMD", "TICK");
29 | context.startService(startSrv);
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\Android\Android\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion '23.0.3'
6 | defaultConfig {
7 | applicationId "service.keppliveservice"
8 | minSdkVersion 15
9 | targetSdkVersion 23
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | sourceSets { main { res.srcDirs = ['src/main/res', 'src/main/res/layout'] } }
21 | }
22 |
23 | dependencies {
24 | compile fileTree(include: ['*.jar'], dir: 'libs')
25 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
26 | exclude group: 'com.android.support', module: 'support-annotations'
27 | })
28 | testCompile 'junit:junit:4.12'
29 | compile files('libs/ddpush-1.0.02.jar')
30 | }
31 |
--------------------------------------------------------------------------------
/app/src/main/java/service/keppliveservice/receiver/KeepLiveReceivers.java:
--------------------------------------------------------------------------------
1 | package service.keppliveservice.receiver;
2 |
3 | import android.content.BroadcastReceiver;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.util.Log;
7 |
8 | import service.keppliveservice.KeepLiveUtils;
9 | import service.keppliveservice.service.OnLineService;
10 |
11 | /**
12 | * 网络连接改变锁屏广播和解锁
13 | * Created by haifeng on 2017/8/28.
14 | */
15 | public class KeepLiveReceivers extends BroadcastReceiver{
16 | @Override
17 | public void onReceive(Context context, Intent intent) {
18 | String action = intent.getAction();
19 | Log.i("91ysdk",action);
20 | if(KeepLiveUtils.hasNetwork(context) == false){
21 | return;
22 | }
23 | if (!KeepLiveUtils.isServiceWork(context,"service.keppliveservice.service.OnLineService")){
24 | Intent startSrv = new Intent(context, OnLineService.class);
25 | startSrv.putExtra("CMD", "TICK");
26 | context.startService(startSrv);
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/app/src/main/java/service/keppliveservice/DateTimeUtil.java:
--------------------------------------------------------------------------------
1 | package service.keppliveservice;
2 |
3 | import java.text.ParseException;
4 | import java.text.SimpleDateFormat;
5 | import java.util.Calendar;
6 | import java.util.Date;
7 |
8 |
9 | public class DateTimeUtil {
10 |
11 | public static String DEFAULTFORMAT = "yyyy-MM-dd HH:mm:ss";
12 |
13 | public static String getCurDateTime() {
14 | return getCurDateTime(DEFAULTFORMAT);
15 | }
16 |
17 | /**
18 | * 当前时间
19 | * @param pattern
20 | * @return
21 | */
22 | public static String getCurDateTime(String pattern) {
23 | return formatCalendar(Calendar.getInstance(),pattern);
24 | }
25 |
26 | public static String formatCalendar(Calendar calendar){
27 | return formatCalendar(calendar,DEFAULTFORMAT);
28 | }
29 |
30 | public static String formatCalendar(Calendar calendar, String pattern){
31 | SimpleDateFormat sdf = new SimpleDateFormat(pattern);
32 | //sdf.setTimeZone(TimeZone.getTimeZone(timeZone));
33 | return sdf.format(calendar.getTime());
34 | }
35 |
36 | public static Date parseDate(String date) throws ParseException {
37 | if(date == null)
38 | return null;
39 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
40 | return sdf.parse(date);
41 | }
42 |
43 | public static Date parseDate(String date, String pattern) throws ParseException {
44 | if(date == null)
45 | return null;
46 | SimpleDateFormat sdf = new SimpleDateFormat(pattern);
47 | return sdf.parse(date);
48 | }
49 |
50 | public static String formatDate(Date date, String pattern){
51 | if(date==null)
52 | return "";
53 | Calendar calendar = Calendar.getInstance();
54 | calendar.setTime(date);
55 | return formatCalendar(calendar,pattern);
56 | }
57 |
58 | public static String formatDate(Date date){
59 | return formatDate(date,DEFAULTFORMAT);
60 | }
61 |
62 | public static Calendar parseString(String dateStr, String pattern) throws ParseException {
63 | SimpleDateFormat sdf = new SimpleDateFormat(pattern);
64 | Date date = sdf.parse(dateStr);
65 | Calendar calendar = Calendar.getInstance();
66 | calendar.setTime(date);
67 | return calendar;
68 | }
69 | public static String convertDateTimeStrFormat(String dateStr, String pattern, String newPattern) throws ParseException {
70 | return DateTimeUtil.formatCalendar(DateTimeUtil.parseString(dateStr,pattern),newPattern);
71 | }
72 |
73 | public static Calendar parseString(String dateStr)throws ParseException {
74 | return parseString(dateStr,DEFAULTFORMAT);
75 | }
76 |
77 |
78 | }
79 |
--------------------------------------------------------------------------------
/app/src/main/java/service/keppliveservice/service/MyJobService.java:
--------------------------------------------------------------------------------
1 | package service.keppliveservice.service;
2 |
3 | import android.annotation.TargetApi;
4 | import android.app.job.JobInfo;
5 | import android.app.job.JobParameters;
6 | import android.app.job.JobScheduler;
7 | import android.app.job.JobService;
8 | import android.content.ComponentName;
9 | import android.content.Context;
10 | import android.content.Intent;
11 | import android.os.Build;
12 | import android.util.Log;
13 | import android.widget.Toast;
14 |
15 | import service.keppliveservice.KeepLiveUtils;
16 |
17 | /**
18 | * Created by haifeng on 2017/8/25.
19 | */
20 | @TargetApi(Build.VERSION_CODES.LOLLIPOP)
21 | public class MyJobService extends JobService{
22 |
23 | @Override
24 | public boolean onStartJob(JobParameters params) {
25 | Log.d("91ySdk", "onStartJob");
26 | startService();
27 | return false;
28 | }
29 |
30 | @Override
31 | public boolean onStopJob(JobParameters params) {
32 | Log.i("91ySdk","onStopJob");
33 | return false;
34 | }
35 |
36 | @Override
37 | public int onStartCommand(Intent intent, int flags, int startId) {
38 | Log.i("91ySdk","onStartCommand");
39 | try {
40 | int id = 1;
41 | JobInfo.Builder builder = new JobInfo.Builder(id,
42 | new ComponentName(getPackageName(), MyJobService.class.getName() ));
43 | builder.setPeriodic(60*1000); //间隔5分钟毫秒调用onStartJob函数
44 | builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); //有网络的时候唤醒
45 | JobScheduler jobScheduler = (JobScheduler)this.getSystemService(Context.JOB_SCHEDULER_SERVICE);
46 | int ret = jobScheduler.schedule(builder.build());
47 | } catch (Exception ex) {
48 | ex.printStackTrace();
49 | }
50 | startService();
51 | return super.onStartCommand(intent, flags, startId);
52 | }
53 |
54 | public void startService(){
55 | boolean isOnLineServiceWork = KeepLiveUtils.isServiceWork(this, "service.keppliveservice.service.OnLineService");
56 | boolean isKeepLiveServiceWork = KeepLiveUtils.isServiceWork(this, "service.keppliveservice.service.KeepLiveService");
57 | if(!isOnLineServiceWork||
58 | !isKeepLiveServiceWork){
59 | this.startService(new Intent(this,OnLineService.class));
60 | this.startService(new Intent(this,KeepLiveService.class));
61 | Toast.makeText(this, "进程启动", Toast.LENGTH_SHORT).show();
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
19 |
20 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
36 |
37 |
38 |
39 |
43 |
44 |
45 |
46 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
--------------------------------------------------------------------------------
/app/src/main/java/service/keppliveservice/service/KeepLiveService.java:
--------------------------------------------------------------------------------
1 | package service.keppliveservice.service;
2 |
3 | import android.app.Service;
4 | import android.content.Intent;
5 | import android.os.Handler;
6 | import android.os.IBinder;
7 | import android.os.Message;
8 | import android.os.RemoteException;
9 | import android.widget.Toast;
10 |
11 | import service.keppliveservice.KeepLiveUtils;
12 | import service.keppliveservice.StrongService;
13 |
14 |
15 | /**
16 | * Created by haifeng on 2017/8/24.
17 | */
18 |
19 | public class KeepLiveService extends Service {
20 |
21 | private Handler handler = new Handler() {
22 | public void handleMessage(android.os.Message msg) {
23 | switch (msg.what) {
24 | case 1:
25 | startOnlineService();
26 | break;
27 |
28 | default:
29 | break;
30 | }
31 |
32 | };
33 | };
34 |
35 | @Override
36 | public void onCreate() {
37 |
38 | Toast.makeText(KeepLiveService.this, "Service2 启动中...", Toast.LENGTH_SHORT).show();
39 | startOnlineService();
40 | /*
41 | * 此线程用监听Service2的状态
42 | */
43 | new Thread() {
44 | public void run() {
45 | while (true) {
46 | boolean isRun = KeepLiveUtils.isServiceWork(KeepLiveService.this,"service.keppliveservice.service.OnLineService");
47 | if (!isRun) {
48 | Message msg = Message.obtain();
49 | msg.what = 1;
50 | handler.sendMessage(msg);
51 | }
52 | try {
53 | Thread.sleep(1);
54 | } catch (InterruptedException e) {
55 | e.printStackTrace();
56 | }
57 | }
58 | };
59 | }.start();
60 | }
61 |
62 | @Override
63 | public IBinder onBind(Intent intent) {
64 | return (IBinder) startS1;
65 | }
66 |
67 | @Override
68 | public int onStartCommand(Intent intent, int flags, int startId) {
69 | return START_STICKY;
70 | }
71 |
72 | /**
73 | * 判断Service1是否还在运行,如果不是则启动Service1
74 | */
75 | private void startOnlineService() {
76 | boolean isRun = KeepLiveUtils.isServiceWork(KeepLiveService.this,
77 | "service.keppliveservice.service.OnLineService");
78 | if (isRun == false) {
79 | try {
80 | startS1.StartService();
81 | } catch (RemoteException e) {
82 | e.printStackTrace();
83 | }
84 | }
85 | }
86 |
87 | /**
88 | * 使用aidl 启动Service1
89 | */
90 | private StrongService startS1 = new StrongService.Stub() {
91 |
92 | @Override
93 | public void StartService() throws RemoteException {
94 | Intent i = new Intent(getBaseContext(), OnLineService.class);
95 | getBaseContext().startService(i);
96 | }
97 |
98 | @Override
99 | public void stopService() throws RemoteException {
100 | Intent i = new Intent(getBaseContext(), OnLineService.class);
101 | getBaseContext().stopService(i);
102 | }
103 | };
104 |
105 | @Override
106 | public void onTrimMemory(int level) {
107 | startOnlineService();
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/app/src/main/java/service/keppliveservice/Util.java:
--------------------------------------------------------------------------------
1 | package service.keppliveservice;
2 |
3 | import android.app.ActivityManager;
4 | import android.content.Context;
5 | import android.net.ConnectivityManager;
6 | import android.net.NetworkInfo;
7 |
8 | import java.security.MessageDigest;
9 | import java.util.List;
10 |
11 | public class Util {
12 |
13 | public static char convertDigit(int value) {
14 | value &= 0x0f;
15 | if (value >= 10)
16 | return ((char) (value - 10 + 'a'));
17 | else
18 | return ((char) (value + '0'));
19 | }
20 |
21 | public static String convert(final byte bytes[]) {
22 |
23 | StringBuffer sb = new StringBuffer(bytes.length * 2);
24 | for (int i = 0; i < bytes.length; i++) {
25 | sb.append(convertDigit((int) (bytes[i] >> 4)));
26 | sb.append(convertDigit((int) (bytes[i] & 0x0f)));
27 | }
28 | return (sb.toString());
29 |
30 | }
31 |
32 | public static String convert(final byte bytes[], int pos, int len) {
33 |
34 | StringBuffer sb = new StringBuffer(len * 2);
35 | for (int i = pos; i < pos+len; i++) {
36 | sb.append(convertDigit((int) (bytes[i] >> 4)));
37 | sb.append(convertDigit((int) (bytes[i] & 0x0f)));
38 | }
39 | return (sb.toString());
40 |
41 | }
42 |
43 | public static byte[] md5Byte(String encryptStr) throws Exception {
44 | MessageDigest md = MessageDigest.getInstance("MD5");
45 | md.update(encryptStr.getBytes("UTF-8"));
46 | return md.digest();
47 | }
48 |
49 | public static String md5(String encryptStr) throws Exception {
50 | MessageDigest md = MessageDigest.getInstance("MD5");
51 | md.update(encryptStr.getBytes("UTF-8"));
52 | byte[] digest = md.digest();
53 | StringBuffer md5 = new StringBuffer();
54 | for (int i = 0; i < digest.length; i++) {
55 | md5.append(Character.forDigit((digest[i] & 0xF0) >> 4, 16));
56 | md5.append(Character.forDigit((digest[i] & 0xF), 16));
57 | }
58 |
59 | encryptStr = md5.toString();
60 | return encryptStr;
61 | }
62 |
63 | public static boolean hasNetwork(Context context) {
64 | ConnectivityManager cm =
65 | (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
66 | NetworkInfo netInfo = cm.getActiveNetworkInfo();
67 | if (netInfo != null && netInfo.isConnected()) {
68 | return true;
69 | }
70 | return false;
71 | }
72 |
73 | /**
74 | * 判断某个服务是否正在运行的方法
75 | *
76 | * @param mContext
77 | * @param serviceName
78 | * 是包名+服务的类名(例如:com.beidian.test.service.BasicInfoService )
79 | * @return
80 | */
81 | public static boolean isServiceWork(Context mContext, String serviceName) {
82 | boolean isWork = false;
83 | ActivityManager myAM = (ActivityManager) mContext
84 | .getSystemService(Context.ACTIVITY_SERVICE);
85 | List myList = myAM.getRunningServices(100);
86 | if (myList.size() <= 0) {
87 | return false;
88 | }
89 | for (int i = 0; i < myList.size(); i++) {
90 | String mName = myList.get(i).service.getClassName().toString();
91 | if (mName.equals(serviceName)) {
92 | isWork = true;
93 | break;
94 | }
95 | }
96 | return isWork;
97 | }
98 |
99 | /**
100 | * 判断进程是否运行
101 | *
102 | * @return
103 | */
104 | public static boolean isProessRunning(Context context, String proessName) {
105 |
106 | boolean isRunning = false;
107 | ActivityManager am = (ActivityManager) context
108 | .getSystemService(Context.ACTIVITY_SERVICE);
109 |
110 | List lists = am.getRunningAppProcesses();
111 | for (ActivityManager.RunningAppProcessInfo info : lists) {
112 | if (info.processName.equals(proessName)) {
113 | isRunning = true;
114 | }
115 | }
116 |
117 | return isRunning;
118 | }
119 |
120 | }
121 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/main/java/service/keppliveservice/KeepLiveUtils.java:
--------------------------------------------------------------------------------
1 | package service.keppliveservice;
2 |
3 | import android.app.ActivityManager;
4 | import android.content.ComponentName;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.net.ConnectivityManager;
8 | import android.net.NetworkInfo;
9 | import android.net.Uri;
10 | import android.os.Build;
11 | import android.provider.Settings;
12 | import android.util.Log;
13 |
14 | import java.util.List;
15 |
16 | /**
17 | * Created by haifeng on 2017/8/29.
18 | */
19 |
20 | public class KeepLiveUtils {
21 |
22 | /**
23 | * Get Mobile Type
24 | *
25 | * @return
26 | */
27 | private static String getMobileType() {
28 | return Build.MANUFACTURER;
29 | }
30 |
31 | /**
32 | * GoTo Open Self Setting Layout
33 | * Compatible Mainstream Models 兼容市面主流机型
34 | *
35 | * @param context
36 | */
37 | public static void jumpStartInterface(Context context) {
38 | Intent intent = new Intent();
39 | try {
40 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
41 | Log.e("HLQ_Struggle", "******************当前手机型号为:" + getMobileType());
42 | ComponentName componentName = null;
43 | if (getMobileType().equals("Xiaomi")) { // 红米Note4测试通过
44 | componentName = new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity");
45 | } else if (getMobileType().equals("Letv")) { // 乐视2测试通过
46 | intent.setAction("com.letv.android.permissionautoboot");
47 | } else if (getMobileType().equals("samsung")) { // 三星Note5测试通过
48 | componentName = new ComponentName("com.samsung.android.sm_cn", "com.samsung.android.sm.ui.ram.AutoRunActivity");
49 | } else if (getMobileType().equals("HUAWEI")) { // 华为测试通过
50 | componentName = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity");
51 | } else if (getMobileType().equals("vivo")) { // VIVO测试通过
52 | componentName = ComponentName.unflattenFromString("com.iqoo.secure/.safeguard.PurviewTabActivity");
53 | } else if (getMobileType().equals("Meizu")) { //万恶的魅族
54 | // 通过测试,发现魅族是真恶心,也是够了,之前版本还能查看到关于设置自启动这一界面,系统更新之后,完全找不到了,心里默默Fuck!
55 | // 针对魅族,我们只能通过魅族内置手机管家去设置自启动,所以我在这里直接跳转到魅族内置手机管家界面,具体结果请看图
56 | componentName = ComponentName.unflattenFromString("com.meizu.safe/.permission.PermissionMainActivity");
57 | } else if (getMobileType().equals("OPPO")) { // OPPO R8205测试通过
58 | // componentName = ComponentName.unflattenFromString("com.oppo.safe/.permission.startup.StartupAppListActivity");
59 | componentName = ComponentName.unflattenFromString("com.oppo.safe/.permission.startup.StartupAppListActivity");
60 | Intent intentOppo = new Intent();
61 | intentOppo.setClassName("com.oppo.safe/.permission.startup", "StartupAppListActivity");
62 | if (context.getPackageManager().resolveActivity(intentOppo, 0) == null) {
63 | componentName = ComponentName.unflattenFromString("com.coloros.safecenter/.startupapp.StartupAppListActivity");
64 | }
65 | } else if (getMobileType().equals("ulong")) { // 360手机 未测试
66 | componentName = new ComponentName("com.yulong.android.coolsafe", ".ui.activity.autorun.AutoRunListActivity");
67 | } else {
68 | // 以上只是市面上主流机型,由于公司你懂的,所以很不容易才凑齐以上设备
69 | // 针对于其他设备,我们只能调整当前系统app查看详情界面
70 | // 在此根据用户手机当前版本跳转系统设置界面
71 | if (Build.VERSION.SDK_INT >= 9) {
72 | intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
73 | intent.setData(Uri.fromParts("package", context.getPackageName(), null));
74 | } else if (Build.VERSION.SDK_INT <= 8) {
75 | intent.setAction(Intent.ACTION_VIEW);
76 | intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
77 | intent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
78 | }
79 | }
80 | intent.setComponent(componentName);
81 | context.startActivity(intent);
82 | } catch (Exception e) {//抛出异常就直接打开设置页面
83 | intent = new Intent(Settings.ACTION_SETTINGS);
84 | context.startActivity(intent);
85 | }
86 | }
87 |
88 | public static boolean hasNetwork(Context context) {
89 | ConnectivityManager cm =
90 | (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
91 | NetworkInfo netInfo = cm.getActiveNetworkInfo();
92 | if (netInfo != null && netInfo.isConnected()) {
93 | return true;
94 | }
95 | return false;
96 | }
97 |
98 | /**
99 | * 判断某个服务是否正在运行的方法
100 | *
101 | * @param mContext
102 | * @param serviceName
103 | * 是包名+服务的类名(例如:com.beidian.test.service.BasicInfoService )
104 | * @return
105 | */
106 | public static boolean isServiceWork(Context mContext, String serviceName) {
107 | boolean isWork = false;
108 | ActivityManager myAM = (ActivityManager) mContext
109 | .getSystemService(Context.ACTIVITY_SERVICE);
110 | List myList = myAM.getRunningServices(100);
111 | if (myList.size() <= 0) {
112 | return false;
113 | }
114 | for (int i = 0; i < myList.size(); i++) {
115 | String mName = myList.get(i).service.getClassName().toString();
116 | if (mName.equals(serviceName)) {
117 | isWork = true;
118 | break;
119 | }
120 | }
121 | return isWork;
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
9 |
10 |
15 |
16 |
20 |
21 |
24 |
25 |
31 |
32 |
33 |
34 |
38 |
39 |
42 |
43 |
49 |
50 |
51 |
52 |
53 |
57 |
58 |
61 |
62 |
68 |
69 |
70 |
73 |
74 |
80 |
81 |
88 |
89 |
93 |
94 |
97 |
101 |
102 |
103 |
104 |
108 |
109 |
112 |
116 |
117 |
118 |
119 |
123 |
124 |
127 |
131 |
132 |
133 |
137 |
138 |
141 |
145 |
146 |
147 |
148 |
152 |
153 |
156 |
160 |
161 |
162 |
163 |
167 |
168 |
171 |
175 |
176 |
177 |
181 |
182 |
185 |
189 |
190 |
191 |
194 |
195 |
198 |
199 |
205 |
212 |
213 |
217 |
218 |
221 |
222 |
227 |
228 |
229 |
230 |
237 |
238 |
242 |
243 |
246 |
247 |
252 |
253 |
254 |
255 |
262 |
263 |
264 |
265 |
266 |
267 |
--------------------------------------------------------------------------------
/app/src/main/java/service/keppliveservice/service/OnLineService.java:
--------------------------------------------------------------------------------
1 | package service.keppliveservice.service;
2 |
3 | import android.app.AlarmManager;
4 | import android.app.Notification;
5 | import android.app.NotificationManager;
6 | import android.app.PendingIntent;
7 | import android.app.Service;
8 | import android.content.Context;
9 | import android.content.Intent;
10 | import android.content.SharedPreferences;
11 | import android.os.Handler;
12 | import android.os.IBinder;
13 | import android.os.PowerManager;
14 | import android.os.PowerManager.WakeLock;
15 | import android.os.RemoteException;
16 | import android.widget.Toast;
17 |
18 | import org.ddpush.im.v1.client.appuser.Message;
19 | import org.ddpush.im.v1.client.appuser.UDPClientBase;
20 |
21 | import java.nio.ByteBuffer;
22 |
23 | import service.keppliveservice.DateTimeUtil;
24 | import service.keppliveservice.KeepLiveUtils;
25 | import service.keppliveservice.MainActivity;
26 | import service.keppliveservice.Params;
27 | import service.keppliveservice.R;
28 | import service.keppliveservice.StrongService;
29 | import service.keppliveservice.Util;
30 | import service.keppliveservice.receiver.TickAlarmReceiver;
31 |
32 | public class OnLineService extends Service {
33 |
34 | protected PendingIntent tickPendIntent;
35 | protected TickAlarmReceiver tickAlarmReceiver = new TickAlarmReceiver();
36 | WakeLock wakeLock;
37 | MyUdpClient myUdpClient;
38 | Notification n;
39 | public static OnLineService mOnLineService;
40 |
41 | public class MyUdpClient extends UDPClientBase {
42 |
43 | public MyUdpClient(byte[] uuid, int appid, String serverAddr, int serverPort)
44 | throws Exception {
45 | super(uuid, appid, serverAddr, serverPort);
46 |
47 | }
48 |
49 | @Override
50 | public boolean hasNetworkConnection() {
51 | return Util.hasNetwork(OnLineService.this);
52 | }
53 |
54 |
55 | @Override
56 | public void trySystemSleep() {
57 | tryReleaseWakeLock();
58 | }
59 |
60 | @Override
61 | public void onPushMessage(Message message) {
62 | if(message == null){
63 | return;
64 | }
65 | if(message.getData() == null || message.getData().length == 0){
66 | return;
67 | }
68 | if(message.getCmd() == 16){// 0x10 通用推送信息
69 | notifyUser(16,"DDPush通用推送信息","时间:"+ DateTimeUtil.getCurDateTime(),"收到通用推送信息");
70 | }
71 | if(message.getCmd() == 17){// 0x11 分组推送信息
72 | long msg = ByteBuffer.wrap(message.getData(), 5, 8).getLong();
73 | notifyUser(17,"DDPush分组推送信息",""+msg,"收到通用推送信息");
74 | }
75 | if(message.getCmd() == 32){// 0x20 自定义推送信息
76 | String str = null;
77 | try{
78 | str = new String(message.getData(),5,message.getContentLength(), "UTF-8");
79 | }catch(Exception e){
80 | str = Util.convert(message.getData(),5,message.getContentLength());
81 | }
82 | notifyUser(32,"DDPush自定义推送信息",""+str,"收到自定义推送信息");
83 | }
84 | setPkgsInfo();
85 | }
86 |
87 | }
88 |
89 | public OnLineService() {
90 | }
91 |
92 | @Override
93 | public void onCreate() {
94 | super.onCreate();
95 | this.setTickAlarm();
96 |
97 | PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
98 | wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "OnLineService");
99 |
100 | resetClient();
101 |
102 | notifyRunning();
103 | mOnLineService = this;
104 |
105 | Toast.makeText(OnLineService.this, "Service1 启动中...", Toast.LENGTH_SHORT).show();
106 | startKeepLiveService();
107 | /*
108 | * 此线程用监听Service2的状态
109 | */
110 | new Thread() {
111 | public void run() {
112 | while (true) {
113 | boolean isRun = KeepLiveUtils.isServiceWork(OnLineService.this,"service.keppliveservice.service.KeepLiveService");
114 | if (!isRun) {
115 | android.os.Message msg = android.os.Message.obtain();
116 | msg.what = 1;
117 | handler.sendMessage(msg);
118 | }
119 | try {
120 | Thread.sleep(1);
121 | } catch (InterruptedException e) {
122 | e.printStackTrace();
123 | }
124 | }
125 | };
126 | }.start();
127 | }
128 |
129 | @Override
130 | public int onStartCommand(Intent param, int flags, int startId) {
131 | if(param == null){
132 | return START_STICKY;
133 | }
134 | String cmd = param.getStringExtra("CMD");
135 | if(cmd == null){
136 | cmd = "";
137 | }
138 | if(cmd.equals("TICK")){
139 | if(wakeLock != null && wakeLock.isHeld() == false){
140 | wakeLock.acquire();
141 | }
142 | }
143 | if(cmd.equals("RESET")){
144 | if(wakeLock != null && wakeLock.isHeld() == false){
145 | wakeLock.acquire();
146 | }
147 | resetClient();
148 | }
149 | if(cmd.equals("TOAST")){
150 | String text = param.getStringExtra("TEXT");
151 | if(text != null && text.trim().length() != 0){
152 | Toast.makeText(this, text, Toast.LENGTH_LONG).show();
153 | }
154 | }
155 |
156 | setPkgsInfo();
157 | return START_STICKY;
158 | }
159 |
160 | protected void setPkgsInfo(){
161 | if(this.myUdpClient == null){
162 | return;
163 | }
164 | long sent = myUdpClient.getSentPackets();
165 | long received = myUdpClient.getReceivedPackets();
166 | SharedPreferences account = this.getSharedPreferences(Params.DEFAULT_PRE_NAME, Context.MODE_PRIVATE);
167 | SharedPreferences.Editor editor = account.edit();
168 | editor.putString(Params.SENT_PKGS, ""+sent);
169 | editor.putString(Params.RECEIVE_PKGS, ""+received);
170 | editor.commit();
171 | }
172 |
173 | protected void resetClient(){
174 | SharedPreferences account = this.getSharedPreferences(Params.DEFAULT_PRE_NAME, Context.MODE_PRIVATE);
175 | String serverIp = account.getString(Params.SERVER_IP, "");
176 | String serverPort = account.getString(Params.SERVER_PORT, "");
177 | String pushPort = account.getString(Params.PUSH_PORT, "");
178 | String userName = account.getString(Params.USER_NAME, "");
179 | if(serverIp == null || serverIp.trim().length() == 0
180 | || serverPort == null || serverPort.trim().length() == 0
181 | || pushPort == null || pushPort.trim().length() == 0
182 | || userName == null || userName.trim().length() == 0){
183 | return;
184 | }
185 | if(this.myUdpClient != null){
186 | try{myUdpClient.stop();}catch(Exception e){}
187 | }
188 | try{
189 | myUdpClient = new MyUdpClient(Util.md5Byte(userName), 1, serverIp, Integer.parseInt(serverPort));
190 | myUdpClient.setHeartbeatInterval(50);
191 | myUdpClient.start();
192 | SharedPreferences.Editor editor = account.edit();
193 | editor.putString(Params.SENT_PKGS, "0");
194 | editor.putString(Params.RECEIVE_PKGS, "0");
195 | editor.commit();
196 | }catch(Exception e){
197 | Toast.makeText(this.getApplicationContext(), "操作失败:"+e.getMessage(), Toast.LENGTH_LONG).show();
198 | }
199 | Toast.makeText(this.getApplicationContext(), "ddpush:终端重置", Toast.LENGTH_LONG).show();
200 | }
201 |
202 | protected void tryReleaseWakeLock(){
203 | if(wakeLock != null && wakeLock.isHeld() == true){
204 | wakeLock.release();
205 | }
206 | }
207 |
208 | protected void setTickAlarm(){
209 | AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
210 | Intent intent = new Intent(this,TickAlarmReceiver.class);
211 | int requestCode = 0;
212 | tickPendIntent = PendingIntent.getBroadcast(this,
213 | requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
214 | //小米2s的MIUI操作系统,目前最短广播间隔为5分钟,少于5分钟的alarm会等到5分钟再触发!2014-04-28
215 | long triggerAtTime = System.currentTimeMillis();
216 | int interval = 300 * 1000;
217 | alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, interval, tickPendIntent);
218 | }
219 |
220 | protected void cancelTickAlarm(){
221 | AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
222 | alarmMgr.cancel(tickPendIntent);
223 | }
224 |
225 | //将通知注释掉,去掉隐式通知
226 | protected void notifyRunning(){
227 | // NotificationManager notificationManager=(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
228 | // n = new Notification();
229 | // Intent intent = new Intent(this,MainActivity.class);
230 | // PendingIntent pi = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_ONE_SHOT);
231 | // n.contentIntent = pi;
232 | // n.setLatestEventInfo(this, "DDPushDemoUDP", "正在运行", pi);
233 | // //n.defaults = Notification.DEFAULT_ALL;
234 | // //n.flags |= Notification.FLAG_SHOW_LIGHTS;
235 | // //n.flags |= Notification.FLAG_AUTO_CANCEL;
236 | // n.flags |= Notification.FLAG_ONGOING_EVENT;
237 | // n.flags |= Notification.FLAG_NO_CLEAR;
238 | // //n.iconLevel = 5;
239 | //
240 | // n.icon = R.drawable.ic_launcher;
241 | // n.when = System.currentTimeMillis();
242 | // n.tickerText = "DDPushDemoUDP正在运行";
243 | // notificationManager.notify(0, n);
244 | }
245 |
246 | protected void cancelNotifyRunning(){
247 | NotificationManager notificationManager=(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
248 | notificationManager.cancel(0);
249 | }
250 |
251 | public void notifyUser(int id, String title, String content, String tickerText){
252 | NotificationManager notificationManager=(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
253 | Notification n = new Notification();
254 | Intent intent = new Intent(this,MainActivity.class);
255 | PendingIntent pi = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_ONE_SHOT);
256 | n.contentIntent = pi;
257 |
258 | // n.setLatestEventInfo(this, title, content, pi);
259 | n.defaults = Notification.DEFAULT_ALL;
260 | n.flags |= Notification.FLAG_SHOW_LIGHTS;
261 | n.flags |= Notification.FLAG_AUTO_CANCEL;
262 |
263 | n.icon = R.mipmap.ic_launcher;
264 | n.when = System.currentTimeMillis();
265 | n.tickerText = tickerText;
266 | notificationManager.notify(id, n);
267 | }
268 |
269 | @Override
270 | public void onDestroy() {
271 | super.onDestroy();
272 | //this.cancelTickAlarm();
273 | // cancelNotifyRunning();
274 | this.tryReleaseWakeLock();
275 | }
276 |
277 | private Handler handler = new Handler() {
278 | public void handleMessage(android.os.Message msg) {
279 | switch (msg.what) {
280 | case 1:
281 | startKeepLiveService();
282 | break;
283 |
284 | default:
285 | break;
286 | }
287 |
288 | };
289 | };
290 |
291 | /**
292 | * 使用aidl 启动Service2
293 | */
294 | private StrongService startS2 = new StrongService.Stub() {
295 |
296 | @Override
297 | public void stopService() throws RemoteException {
298 | Intent i = new Intent(getBaseContext(), KeepLiveService.class);
299 | getBaseContext().stopService(i);
300 | }
301 |
302 | @Override
303 | public void StartService() throws RemoteException {
304 | Intent i = new Intent(getBaseContext(), KeepLiveService.class);
305 | getBaseContext().startService(i);
306 | }
307 | };
308 |
309 | /**
310 | * 在内存紧张的时候,系统回收内存时,会回调OnTrimMemory, 重写onTrimMemory当系统清理内存时从新启动Service2
311 | */
312 | @Override
313 | public void onTrimMemory(int level) {
314 | startKeepLiveService();
315 | }
316 |
317 | /**
318 | * 判断Service2是否还在运行,如果不是则启动Service2
319 | */
320 | private void startKeepLiveService() {
321 | boolean isRun = KeepLiveUtils.isServiceWork(OnLineService.this,
322 | "service.keppliveservice.service.KeepLiveService");
323 | if (isRun == false) {
324 | try {
325 | startS2.StartService();
326 | } catch (RemoteException e) {
327 | e.printStackTrace();
328 | }
329 | }
330 | }
331 |
332 | @Override
333 | public IBinder onBind(Intent intent) {
334 | return (IBinder) startS2;
335 | }
336 | }
337 |
--------------------------------------------------------------------------------
/app/src/main/java/service/keppliveservice/MainActivity.java:
--------------------------------------------------------------------------------
1 | package service.keppliveservice;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.IntentFilter;
7 | import android.content.SharedPreferences;
8 | import android.os.Build;
9 | import android.os.Bundle;
10 | import android.os.Handler;
11 | import android.util.Log;
12 | import android.view.MenuItem;
13 | import android.view.View;
14 | import android.widget.Button;
15 | import android.widget.EditText;
16 | import android.widget.TextView;
17 | import android.widget.Toast;
18 |
19 | import org.ddpush.im.v1.client.appserver.Pusher;
20 |
21 | import service.keppliveservice.receiver.KeepLiveReceivers;
22 | import service.keppliveservice.service.KeepLiveService;
23 | import service.keppliveservice.service.MyJobService;
24 | import service.keppliveservice.service.OnLineService;
25 |
26 |
27 | public class MainActivity extends Activity {
28 |
29 | private EditText serverIp;
30 | private EditText serverPort;
31 | private EditText pushPort;
32 | private EditText userName;
33 | private Button startBtn;
34 |
35 | private EditText targetUserName;
36 | private EditText send0x11Data;
37 | private EditText send0x20Data;
38 | private Button send0x10Btn;
39 | private Button send0x11Btn;
40 | private Button send0x20Btn;
41 |
42 | private Handler handler;
43 | private Runnable refresher;
44 |
45 |
46 | @Override
47 | protected void onCreate(Bundle savedInstanceState) {
48 | super.onCreate(savedInstanceState);
49 | setContentView(R.layout.activity_main);
50 |
51 | refresher = new Runnable(){
52 | public void run(){
53 | MainActivity.this.freshCurrentInfo();
54 | }
55 | };
56 | handler = new Handler();
57 | handler.postDelayed(refresher, 1000);
58 |
59 | SharedPreferences account = this.getSharedPreferences(Params.DEFAULT_PRE_NAME, Context.MODE_PRIVATE);
60 | serverIp = (EditText)findViewById(R.id.demo_server_ip);
61 | serverIp.setText(account.getString(Params.SERVER_IP, ""));
62 |
63 | serverPort = (EditText)findViewById(R.id.demo_server_port);
64 | serverPort.setText(account.getString(Params.SERVER_PORT, "9966"));
65 |
66 | pushPort = (EditText)findViewById(R.id.demo_push_port);
67 | pushPort.setText(account.getString(Params.PUSH_PORT, "9999"));
68 |
69 | userName = (EditText)findViewById(R.id.demo_user_name);
70 | userName.setText(account.getString(Params.USER_NAME, ""));
71 |
72 | startBtn = (Button)findViewById(R.id.demo_start_button);
73 | startBtn.setOnClickListener(new View.OnClickListener() {
74 | public void onClick(View v) {
75 | // MainActivity.this.start();
76 | KeepLiveUtils.jumpStartInterface(MainActivity.this);
77 | }
78 | });
79 |
80 | targetUserName = (EditText)findViewById(R.id.demo_target_user_name);
81 | send0x11Data = (EditText)findViewById(R.id.demo_send_0x11_data);
82 | send0x20Data = (EditText)findViewById(R.id.demo_send_0x20_data);
83 | targetUserName = (EditText)findViewById(R.id.demo_target_user_name);
84 | send0x10Btn = (Button)findViewById(R.id.demo_send_0x10_button);
85 | send0x10Btn.setOnClickListener(new View.OnClickListener() {
86 | public void onClick(View v) {
87 | MainActivity.this.send0x10();
88 |
89 | }
90 | });
91 | send0x11Btn = (Button)findViewById(R.id.demo_send_0x11_button);
92 | send0x11Btn.setOnClickListener(new View.OnClickListener() {
93 | public void onClick(View v) {
94 | MainActivity.this.send0x11();
95 |
96 | }
97 | });
98 | send0x20Btn = (Button)findViewById(R.id.demo_send_0x20_button);
99 | send0x20Btn.setOnClickListener(new View.OnClickListener() {
100 | public void onClick(View v) {
101 | MainActivity.this.send0x20();
102 |
103 | }
104 | });
105 |
106 | Intent startSrv = new Intent(this, OnLineService.class);
107 | startService(startSrv);
108 | Intent i2 = new Intent(this, KeepLiveService.class);
109 | startService(i2);
110 | Log.i("91ysdk","version"+Build.VERSION.SDK_INT);
111 | if (Build.VERSION.SDK_INT>=21){
112 | Intent i3 = new Intent(this, MyJobService.class);
113 | startService(i3);
114 | }
115 | }
116 |
117 | protected void start(){
118 | if(serverIp.getText().toString().length() == 0){
119 | Toast.makeText(this.getApplicationContext(), "请输入服务器ip", Toast.LENGTH_SHORT).show();
120 | serverIp.requestFocus();
121 | return;
122 | }
123 | if(serverPort.getText().toString().length() == 0){
124 | Toast.makeText(this.getApplicationContext(), "请输入服务器端口", Toast.LENGTH_SHORT).show();
125 | serverPort.requestFocus();
126 | return;
127 | }
128 | if(pushPort.getText().toString().length() == 0){
129 | Toast.makeText(this.getApplicationContext(), "请输入推送端口", Toast.LENGTH_SHORT).show();
130 | pushPort.requestFocus();
131 | return;
132 | }
133 |
134 | if(userName.getText().toString().length() == 0){
135 | Toast.makeText(this.getApplicationContext(), "请输入用户名", Toast.LENGTH_SHORT).show();
136 | userName.requestFocus();
137 | return;
138 | }
139 | int intServerPort = 0, intPushPort = 0;
140 | try{
141 | intServerPort = Integer.parseInt(serverPort.getText().toString());
142 | }catch(Exception e){
143 | Toast.makeText(this.getApplicationContext(), "端口格式错误", Toast.LENGTH_SHORT).show();
144 | serverPort.requestFocus();
145 | return;
146 | }
147 | try{
148 | intPushPort = Integer.parseInt(pushPort.getText().toString());
149 | }catch(Exception e){
150 | Toast.makeText(this.getApplicationContext(), "端口格式错误", Toast.LENGTH_SHORT).show();
151 | pushPort.requestFocus();
152 | return;
153 | }
154 | //Toast.makeText(this.getApplicationContext(), "开始", Toast.LENGTH_SHORT).show();
155 | saveAccountInfo();
156 | Intent startSrv = new Intent(this, OnLineService.class);
157 | startSrv.putExtra("CMD", "RESET");
158 | this.startService(startSrv);
159 | freshCurrentInfo();
160 | }
161 |
162 | protected void send0x10(){
163 | if(targetUserName.getText().toString().length() == 0){
164 | Toast.makeText(this.getApplicationContext(), "请输入目标用户名", Toast.LENGTH_SHORT).show();
165 | targetUserName.requestFocus();
166 | return;
167 | }
168 | SharedPreferences account = this.getSharedPreferences(Params.DEFAULT_PRE_NAME, Context.MODE_PRIVATE);
169 | String serverIp = account.getString(Params.SERVER_IP, "");
170 | String pushPort = account.getString(Params.PUSH_PORT, "");
171 | int port;
172 | try{
173 | port = Integer.parseInt(pushPort);
174 | }catch(Exception e){
175 | Toast.makeText(this.getApplicationContext(), "推送端口格式错误:"+pushPort, Toast.LENGTH_SHORT).show();
176 | return;
177 | }
178 | byte[] uuid = null;
179 | try{
180 | uuid = Util.md5Byte(targetUserName.getText().toString());
181 | }catch(Exception e){
182 | Toast.makeText(this.getApplicationContext(), "错误:"+e.getMessage(), Toast.LENGTH_SHORT).show();
183 | targetUserName.requestFocus();
184 | return;
185 | }
186 | Thread t = new Thread(new send0x10Task(this,serverIp,port,uuid));
187 | t.start();
188 |
189 | }
190 | protected void send0x11(){
191 | if(targetUserName.getText().toString().length() == 0){
192 | Toast.makeText(this.getApplicationContext(), "请输入目标用户名", Toast.LENGTH_SHORT).show();
193 | targetUserName.requestFocus();
194 | return;
195 | }
196 |
197 | if(send0x11Data.getText().toString().length() == 0){
198 | Toast.makeText(this.getApplicationContext(), "请输入数字作为分类信息", Toast.LENGTH_SHORT).show();
199 | send0x11Data.requestFocus();
200 | return;
201 | }
202 | if("0".equals(send0x11Data.getText().toString().trim())){
203 | Toast.makeText(this.getApplicationContext(), "数字必须非零", Toast.LENGTH_SHORT).show();
204 | send0x11Data.requestFocus();
205 | return;
206 | }
207 | long msg;
208 | try{
209 | msg = Long.parseLong(send0x11Data.getText().toString().trim());
210 | }catch(Exception e){
211 | Toast.makeText(this.getApplicationContext(), "数字格式错误", Toast.LENGTH_SHORT).show();
212 | send0x11Data.requestFocus();
213 | return;
214 | }
215 | SharedPreferences account = this.getSharedPreferences(Params.DEFAULT_PRE_NAME, Context.MODE_PRIVATE);
216 | String serverIp = account.getString(Params.SERVER_IP, "");
217 | String pushPort = account.getString(Params.PUSH_PORT, "");
218 | int port;
219 | try{
220 | port = Integer.parseInt(pushPort);
221 | }catch(Exception e){
222 | Toast.makeText(this.getApplicationContext(), "推送端口格式错误:"+pushPort, Toast.LENGTH_SHORT).show();
223 | return;
224 | }
225 | byte[] uuid = null;
226 | try{
227 | uuid = Util.md5Byte(targetUserName.getText().toString());
228 | }catch(Exception e){
229 | Toast.makeText(this.getApplicationContext(), "错误:"+e.getMessage(), Toast.LENGTH_SHORT).show();
230 | targetUserName.requestFocus();
231 | return;
232 | }
233 | Thread t = new Thread(new send0x11Task(this,serverIp,port,uuid,msg));
234 | t.start();
235 | }
236 | protected void send0x20(){
237 | if(targetUserName.getText().toString().length() == 0){
238 | Toast.makeText(this.getApplicationContext(), "请输入目标用户名", Toast.LENGTH_SHORT).show();
239 | targetUserName.requestFocus();
240 | return;
241 | }
242 |
243 | if(send0x20Data.getText().toString().length() == 0){
244 | Toast.makeText(this.getApplicationContext(), "请输入一串文字", Toast.LENGTH_SHORT).show();
245 | send0x20Data.requestFocus();
246 | return;
247 | }
248 |
249 | SharedPreferences account = this.getSharedPreferences(Params.DEFAULT_PRE_NAME, Context.MODE_PRIVATE);
250 | String serverIp = account.getString(Params.SERVER_IP, "");
251 | String pushPort = account.getString(Params.PUSH_PORT, "");
252 | int port;
253 | try{
254 | port = Integer.parseInt(pushPort);
255 | }catch(Exception e){
256 | Toast.makeText(this.getApplicationContext(), "推送端口格式错误:"+pushPort, Toast.LENGTH_SHORT).show();
257 | return;
258 | }
259 | byte[] uuid = null;
260 | try{
261 | uuid = Util.md5Byte(targetUserName.getText().toString());
262 | }catch(Exception e){
263 | Toast.makeText(this.getApplicationContext(), "错误:"+e.getMessage(), Toast.LENGTH_SHORT).show();
264 | targetUserName.requestFocus();
265 | return;
266 | }
267 | byte[] msg = null;
268 | try{
269 | msg = send0x20Data.getText().toString().getBytes("UTF-8");
270 | }catch(Exception e){
271 | Toast.makeText(this.getApplicationContext(), "错误:"+e.getMessage(), Toast.LENGTH_SHORT).show();
272 | send0x20Data.requestFocus();
273 | return;
274 | }
275 | Thread t = new Thread(new send0x20Task(this,serverIp,port,uuid,msg));
276 | t.start();
277 | }
278 |
279 | protected void saveAccountInfo(){
280 | SharedPreferences account = this.getSharedPreferences(Params.DEFAULT_PRE_NAME, Context.MODE_PRIVATE);
281 | SharedPreferences.Editor editor = account.edit();
282 | editor.putString(Params.SERVER_IP, serverIp.getText().toString());
283 | editor.putString(Params.SERVER_PORT, serverPort.getText().toString());
284 | editor.putString(Params.PUSH_PORT, pushPort.getText().toString());
285 | editor.putString(Params.USER_NAME, userName.getText().toString());
286 | editor.putString(Params.SENT_PKGS, "0");
287 | editor.putString(Params.RECEIVE_PKGS, "0");
288 | editor.commit();
289 |
290 | }
291 |
292 | protected void freshCurrentInfo(){
293 | SharedPreferences account = this.getSharedPreferences(Params.DEFAULT_PRE_NAME, Context.MODE_PRIVATE);
294 | String serverIp = account.getString(Params.SERVER_IP, "");
295 | String serverPort = account.getString(Params.SERVER_PORT, "");
296 | String pushPort = account.getString(Params.PUSH_PORT, "");
297 | String userName = account.getString(Params.USER_NAME, "");
298 | String sentPkgs = account.getString(Params.SENT_PKGS, "0");
299 | String receivePkgs = account.getString(Params.RECEIVE_PKGS, "0");
300 | String uuid = null;
301 |
302 | try{
303 | uuid = Util.md5(userName);
304 | }catch(Exception e){
305 | uuid = "";
306 | }
307 | if(userName == null || userName.length() == 0){
308 | uuid="";
309 | }
310 | ((TextView)findViewById(R.id.demo_cur_server_ip)).setText(serverIp);
311 | ((TextView)findViewById(R.id.demo_cur_server_ip)).postInvalidate();
312 |
313 | ((TextView)findViewById(R.id.demo_cur_server_port)).setText(serverPort);
314 | ((TextView)findViewById(R.id.demo_cur_server_port)).postInvalidate();
315 |
316 | ((TextView)findViewById(R.id.demo_cur_push_port)).setText(pushPort);
317 | ((TextView)findViewById(R.id.demo_cur_push_port)).postInvalidate();
318 |
319 | ((TextView)findViewById(R.id.demo_cur_user_name)).setText(userName);
320 | ((TextView)findViewById(R.id.demo_cur_user_name)).postInvalidate();
321 |
322 | ((TextView)findViewById(R.id.demo_cur_uuid)).setText(uuid);
323 | ((TextView)findViewById(R.id.demo_cur_uuid)).postInvalidate();
324 |
325 | ((TextView)findViewById(R.id.demo_cur_sent_pkgs)).setText(sentPkgs);
326 | ((TextView)findViewById(R.id.demo_cur_sent_pkgs)).postInvalidate();
327 |
328 | ((TextView)findViewById(R.id.demo_cur_receive_pkgs)).setText(receivePkgs);
329 | ((TextView)findViewById(R.id.demo_cur_receive_pkgs)).postInvalidate();
330 |
331 | // try{
332 | // this.findViewById(R.layout.activity_main).postInvalidate();
333 | // }catch(Exception e){
334 | //
335 | // }
336 | }
337 |
338 | @Override
339 | protected void onResume() {
340 | freshCurrentInfo();
341 | super.onResume();
342 | }
343 |
344 | @Override
345 | protected void onDestroy() {
346 | handler.removeCallbacks(refresher);
347 | super.onDestroy();
348 | registerReceiver(this);
349 | }
350 |
351 |
352 | @Override
353 | public boolean onOptionsItemSelected(MenuItem item) {
354 | // Handle action bar item clicks here. The action bar will
355 | // automatically handle clicks on the Home/Up button, so long
356 | // as you specify a parent activity in AndroidManifest.xml.
357 | int id = item.getItemId();
358 | // if (id == R.id.action_settings) {
359 | // return true;
360 | // }
361 | return super.onOptionsItemSelected(item);
362 | }
363 |
364 | class send0x10Task implements Runnable {
365 | private Context context;
366 | private String serverIp;
367 | private int port;
368 | private byte[] uuid;
369 |
370 | public send0x10Task(Context context, String serverIp, int port, byte[] uuid){
371 | this.context = context;
372 | this.serverIp = serverIp;
373 | this.port = port;
374 | this.uuid = uuid;
375 | }
376 |
377 | public void run(){
378 | Pusher pusher = null;
379 | Intent startSrv = new Intent(context, OnLineService.class);
380 | startSrv.putExtra("CMD", "TOAST");
381 | try{
382 | boolean result;
383 | pusher = new Pusher(serverIp,port, 1000*5);
384 | result = pusher.push0x10Message(uuid);
385 | if(result){
386 | startSrv.putExtra("TEXT", "通用信息发送成功");
387 | }else{
388 | startSrv.putExtra("TEXT", "发送失败!格式有误");
389 | }
390 | }catch(Exception e){
391 | e.printStackTrace();
392 | startSrv.putExtra("TEXT", "发送失败!"+e.getMessage());
393 | }finally{
394 | if(pusher != null){
395 | try{pusher.close();}catch(Exception e){};
396 | }
397 | }
398 | context.startService(startSrv);
399 | }
400 | }
401 |
402 | class send0x11Task implements Runnable {
403 | private Context context;
404 | private String serverIp;
405 | private int port;
406 | private byte[] uuid;
407 | private long msg;
408 |
409 | public send0x11Task(Context context, String serverIp, int port, byte[] uuid, long msg){
410 | this.context = context;
411 | this.serverIp = serverIp;
412 | this.port = port;
413 | this.uuid = uuid;
414 | this.msg = msg;
415 | }
416 |
417 | public void run(){
418 | Pusher pusher = null;
419 | Intent startSrv = new Intent(context, OnLineService.class);
420 | startSrv.putExtra("CMD", "TOAST");
421 | try{
422 | boolean result;
423 | pusher = new Pusher(serverIp,port, 1000*5);
424 | result = pusher.push0x11Message(uuid,msg);
425 | if(result){
426 | startSrv.putExtra("TEXT", "分类信息发送成功");
427 | }else{
428 | startSrv.putExtra("TEXT", "发送失败!格式有误");
429 | }
430 | }catch(Exception e){
431 | e.printStackTrace();
432 | startSrv.putExtra("TEXT", "发送失败!"+e.getMessage());
433 | }finally{
434 | if(pusher != null){
435 | try{pusher.close();}catch(Exception e){};
436 | }
437 | }
438 | context.startService(startSrv);
439 | }
440 | }
441 |
442 | class send0x20Task implements Runnable {
443 | private Context context;
444 | private String serverIp;
445 | private int port;
446 | private byte[] uuid;
447 | private byte[] msg;
448 |
449 | public send0x20Task(Context context, String serverIp, int port, byte[] uuid, byte[] msg){
450 | this.context = context;
451 | this.serverIp = serverIp;
452 | this.port = port;
453 | this.uuid = uuid;
454 | this.msg = msg;
455 | }
456 |
457 | public void run(){
458 | Pusher pusher = null;
459 | Intent startSrv = new Intent(context, OnLineService.class);
460 | startSrv.putExtra("CMD", "TOAST");
461 | try{
462 | boolean result;
463 |
464 |
465 | pusher = new Pusher(serverIp,port, 1000*5);
466 | result = pusher.push0x20Message(uuid,msg);
467 | if(result){
468 | startSrv.putExtra("TEXT", "自定义信息发送成功");
469 | }else{
470 | startSrv.putExtra("TEXT", "发送失败!格式有误");
471 | }
472 | }catch(Exception e){
473 | e.printStackTrace();
474 | startSrv.putExtra("TEXT", "发送失败!"+e.getMessage());
475 | }finally{
476 | if(pusher != null){
477 | try{pusher.close();}catch(Exception e){};
478 | }
479 | }
480 | context.startService(startSrv);
481 | }
482 | }
483 |
484 |
485 | //注册锁屏广播
486 | public void registerReceiver(Context context){
487 | IntentFilter filter=new IntentFilter(Intent.ACTION_SCREEN_OFF);
488 | filter.addAction(Intent.ACTION_SCREEN_ON);
489 | context.getApplicationContext().registerReceiver(new KeepLiveReceivers(), filter);
490 | }
491 |
492 | }
493 |
--------------------------------------------------------------------------------