Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

power #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
power
foungyu committed Jul 10, 2015
commit d525443f5c9b3dbec59f27b03e73fd7654afcdbe
Binary file added app/app-release.apk
Binary file not shown.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -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{
60 changes: 60 additions & 0 deletions app/src/main/java/com/mamahow/runningappchecker/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
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";
protected PowerManager.WakeLock mWakeLock;

//Variable to store brightness value
private int brightness = 255;
//Content resolver used as a handle to the system's settings
private ContentResolver cResolver;
//Window object, that will store a reference to the current window
private Window window;


@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -22,5 +38,49 @@ 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();

//Brightness
//Get the content resolver
cResolver = getContentResolver();

//Get the current window
window = getWindow();

try
{
// To handle the auto
android.provider.Settings.System.putInt(cResolver,
android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
//Get the current system brightness
brightness = android.provider.Settings.System.getInt(cResolver, android.provider.Settings.System.SCREEN_BRIGHTNESS);
Log.i("brightness", String.valueOf(brightness));
android.provider.Settings.System.putInt(cResolver, android.provider.Settings.System.SCREEN_BRIGHTNESS, brightness);
//Get the current window attributes
WindowManager.LayoutParams layoutpars = window.getAttributes();
//Set the brightness of this window
layoutpars.screenBrightness = 255;
//Apply attribute changes to this window
window.setAttributes(layoutpars);

}
catch (Settings.SettingNotFoundException e)
{
//Throw an error case it couldn't be retrieved
Log.e("Error", "Cannot access system brightness");
e.printStackTrace();
}

}

@Override
public void onDestroy() {
this.mWakeLock.release();
super.onDestroy();
}
}
76 changes: 4 additions & 72 deletions app/src/main/java/com/mamahow/runningappchecker/MyService.java
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@


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

@@ -22,15 +22,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;
}

@@ -72,7 +71,7 @@ private void _runningChecker() {
}
// delay
try {
Thread.sleep(15000);
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
@@ -89,17 +88,13 @@ private boolean isRunning() {
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();
}
@@ -110,67 +105,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;
// }
// }

}