Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 1 addition & 57 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

Binary file added app/app-release.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build/intermediates/classes/debug" />
<output-test url="file://$MODULE_DIR$/build/intermediates/classes/androidTest/debug" />
<exclude-output />
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mamahow.runningappchecker" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import android.util.Log;

/**
* Created by tiggly on 6/29/15.
* Created by fangyu on 6/29/15.
*/

public class BootUpReciever extends BroadcastReceiver{
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/mamahow/runningappchecker/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package com.mamahow.runningappchecker;

import android.content.ContentResolver;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;



public class MainActivity extends AppCompatActivity {

private static final String TAG = "RunningAppChecker";



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -22,5 +31,20 @@ protected void onCreate(Bundle savedInstanceState) {
Intent startIntent = new Intent(this, MyService.class);
startService(startIntent);

//Keep wakeup
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

// final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
// this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
// this.mWakeLock.acquire();



}

@Override
public void onDestroy() {

super.onDestroy();
}
}
96 changes: 23 additions & 73 deletions app/src/main/java/com/mamahow/runningappchecker/MyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import java.util.List;
import java.net.URL;

import android.app.PendingIntent;
import android.app.AlarmManager;
import android.os.SystemClock;

/**
* Created by harryhow on 6/27/15.
* Created by fangyu on 6/27/15.
*/
public class MyService extends Service{

Expand All @@ -22,15 +25,14 @@ public class MyService extends Service{
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate() executed");
//runningChecker();
new RunningChecker().execute(null, null, null);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand() executed");
new RunningChecker().execute(null, null, null);
// mamahow: you need to return START_STICKY to make sure service will restart without new intent
// fangyu: you need to return START_STICKY to make sure service will restart without new intent
return START_STICKY;
}

Expand All @@ -41,6 +43,20 @@ public void onDestroy() {
Log.d(TAG, "onDestroy() executed");
}

@Override
public void onTaskRemoved(Intent rootIntent){
Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
restartServiceIntent.setPackage(getPackageName());

PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000,
restartServicePendingIntent);

super.onTaskRemoved(rootIntent);
}

@Override
public IBinder onBind(Intent intent) {
Expand All @@ -58,6 +74,7 @@ protected Long doInBackground(URL... urls) {
private void _runningChecker() {
int count = 0;
while(true){
Log.i(TAG, "aaa");
while(!isRunning()) {
count++;

Expand All @@ -72,7 +89,7 @@ private void _runningChecker() {
}
// delay
try {
Thread.sleep(15000);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Expand All @@ -82,24 +99,20 @@ private void _runningChecker() {
private boolean isRunning() {
Context context = getBaseContext();

//Log.i(TAG, "Checking if app is running");
Log.i(TAG, "Checking if app is running");
ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> services = activityManager.getRunningAppProcesses();

boolean isForeground = false;

for (int i=0; i<services.size(); i++){
//Log.i(TAG, "Name:" + services.get(i).processName.toString());
// TODO: change package name for your own app
if (services.get(i).processName.toString().equalsIgnoreCase("com.tiggly.tales")) {
//Log.i(TAG, "found my app!");
if (services.get(i).importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
//Log.i(TAG, "App is on foreground, good!");
isForeground = true;

// delay
try {
Thread.sleep(15000);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Expand All @@ -110,67 +123,4 @@ private boolean isRunning() {
return isForeground;
}

// Change to use Binder, using the following code
// class MyBinder extends Binder {
//
// // Use AsyncTask or Runnable?
// public void runningChecker() {
// int count = 0;
// while(true){
// while(!isRunning()) {
// count++;
// Log.i(TAG, "App is NOT running, bring back my app again (" + count + ")");
//
// // invoke intent
// Context context = getBaseContext();
// // TODO: change package name for your own app
// Intent intent = getPackageManager().getLaunchIntentForPackage("com.mamahow.helloandroidstudio");
//
// //Intent intent = new Intent(context, MainActivity.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
// startActivity(intent);
//
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// break;
// }
// }
// }
//
// boolean isRunning() {
// Context context = getBaseContext();
//
// //Log.i(TAG, "Checking if app is running");
// ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
// List<ActivityManager.RunningAppProcessInfo> services = activityManager.getRunningAppProcesses();
//
//
// boolean isForeground = false;
//
// for (int i=0; i<services.size(); i++){
// //Log.i(TAG, "Name:" + services.get(i).processName.toString());
// // TODO: change package name for your own app
// if (services.get(i).processName.toString().equalsIgnoreCase("com.mamahow.helloandroidstudio")) {
// //Log.i(TAG, "found my app!");
// if (services.get(i).importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
// Log.i(TAG, "App is on foreground, good!");
// isForeground = true;
//
// // delay
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// }
// }
//
// return isForeground;
// }
// }

}