Created
February 1, 2013 07:09
-
-
Save jschmid/4689853 to your computer and use it in GitHub Desktop.
Poll the currently running Android Activity then replace it by our own
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission android:name="android.permission.GET_TASKS" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pro.schmid.android.foreground; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.concurrent.ScheduledThreadPoolExecutor; | |
import java.util.concurrent.TimeUnit; | |
import android.app.ActivityManager; | |
import android.app.ActivityManager.RunningTaskInfo; | |
import android.app.Service; | |
import android.content.Intent; | |
import android.os.Handler; | |
import android.os.IBinder; | |
import android.widget.Toast; | |
public class PollingService extends Service { | |
private static final String VICTIM_ACTIVITY = "..."; | |
private ScheduledThreadPoolExecutor mScheduledThreadPoolExecutor; | |
private ActivityManager mActivityManager; | |
private Handler mHandler; | |
@Override | |
public IBinder onBind(Intent i) { | |
return null; // Not used | |
} | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
mHandler = new Handler(); | |
mActivityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); | |
mScheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1); | |
mScheduledThreadPoolExecutor.scheduleWithFixedDelay(new Poll(), 0, 2, TimeUnit.SECONDS); | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
if (mScheduledThreadPoolExecutor != null) { | |
mScheduledThreadPoolExecutor.shutdownNow(); | |
} | |
} | |
private class Poll implements Runnable { | |
@Override | |
public void run() { | |
List<RunningTaskInfo> l = mActivityManager.getRunningTasks(1); | |
Iterator<RunningTaskInfo> i = l.iterator(); | |
while (i.hasNext()) { | |
RunningTaskInfo info = i.next(); | |
final String name1 = info.topActivity.getClassName(); | |
final String name2 = info.baseActivity.getClassName(); | |
if (VICTIM_ACTIVITY.equalsIgnoreCase(name1)) { | |
Intent intent = new Intent(PollingService.this, FakeScreenActivity.class); | |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
startActivity(intent); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment