nigatigga

heartbeat service

Aug 27th, 2014
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. package com.ibc.android.demo.appslist.app;
  2.  
  3. import android.app.Activity;
  4. import android.app.ActivityManager;
  5. import android.app.AlarmManager;
  6. import android.app.PendingIntent;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.os.Bundle;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12.  
  13. import com.spicycurryman.getdisciplined10.app.R;
  14.  
  15. import java.util.ArrayList;
  16. import java.util.List;
  17.  
  18. public class LockScreenActivity extends Activity {
  19.     private static final String TAG = LockScreenActivity.class.getSimpleName();
  20.     EditText pin;
  21.     TextView pinMsg;
  22.     String firstPin;
  23.     private ArrayList newArrayList = null;
  24.  
  25.  
  26.     @Override
  27.     protected void onCreate(Bundle savedInstanceState) {
  28.         super.onCreate(savedInstanceState);
  29.         setContentView(R.layout.activity_setup);
  30.  
  31.     }
  32.     @Override
  33.     public void onBackPressed() {
  34.  
  35.         /*
  36.          * So here's what's going on: When the user presses the back button,
  37.          * the Instagram app (or any other app that's being blocked) is relaunched
  38.          * from the stack and that triggers the LockScreenActivity to be launched
  39.          * again. Kinda goes into an infinite loop in a sense. What I'm going to do
  40.          * to get around that, is completely kill the Instagram application.
  41.          */
  42.  
  43.  
  44.  
  45.         /*
  46.          * Every running application has a PID (process id). This is how the system
  47.          * keeps track of which apps are running. First, we're gonna need to find the
  48.          * PID of the blocked app (Instagram in this case).
  49.          */
  50.  
  51.         // Grab a list of all running processes and their PIDs.
  52.         ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
  53.         List<ActivityManager.RunningAppProcessInfo> pids = am.getRunningAppProcesses();
  54.  
  55.         // Now loop through the list of PIDs and find Instagram's PID.
  56.  
  57.  
  58.         // Now that we've got the PID, kill the Instagram process.
  59.  
  60.         ActivityManager  am1 = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE);
  61.         newArrayList = ApkAdapter.getArrayList();
  62.         for (Object data : newArrayList) {
  63.             am1.killBackgroundProcesses((String) data);
  64.         }
  65.  
  66.         // Display confirmation here, finish() activity.
  67.         Intent startMain = new Intent(Intent.ACTION_MAIN);
  68.         startMain.addCategory(Intent.CATEGORY_HOME);
  69.         startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  70.         startActivity(startMain);
  71.  
  72.         Intent iHeartBeatService = new Intent(this, HeartBeat.class);
  73.         PendingIntent piHeartBeatService = PendingIntent.getService(this, 0, iHeartBeatService, PendingIntent.FLAG_UPDATE_CURRENT);
  74.         AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  75.         alarmManager.cancel(piHeartBeatService);
  76.         alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 300000, piHeartBeatService);
  77.  
  78.         finish();
  79.         super.onBackPressed();
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment