0

Currently I am using IntentService to loop urls in the Browser.apk. I am running it until the battery drains. Here is my dirty code. =)

@Override
protected void onHandleIntent(Intent intent) {
    int size = intent.getStringArrayExtra("addresses").length;
    int counter = sp.getInt("counter", 0);
    String address = intent.getStringArrayExtra("addresses")[counter];
    Intent i = new Intent(Intent.ACTION_VIEW);

    i.setData(Uri.parse(address));
    i.setFlags(Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);

    counter++;
    if(counter == size) {
        counter = 0;
    }
    spe.putInt("counter", counter);
    spe.commit();
}

I tried to use wakelock but not all devices stays awake. It is working with Motorola Xoom but not in Thinkpad tablet slate.

Do you know other option other than using the wakelock. Or How should I properly implement the wakelock?

Can I tell the Browser that I should keep the Screen On while loading the urls? By using intent or other means.

quiel
  • 427
  • 1
  • 3
  • 19

2 Answers2

1

For open the device screen, if the device is in sleep mode, use the code below:

       //acquireLock(context);
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
         boolean isScreenOn = pm.isScreenOn();
         Log.e("screen on.................................", ""+isScreenOn);
         if(isScreenOn==false)
         {
              WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock");
              wl.acquire(10000);
              WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock");
              wl_cpu.acquire(10000);
         }
Ant4res
  • 1,217
  • 1
  • 18
  • 36
Pradeep Sodhi
  • 2,135
  • 1
  • 19
  • 19
  • Thanks Pradeep! Here is what I did. mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE, TAG); mWakeLock.acquire(); – quiel Mar 26 '12 at 04:51
  • I just have one concern, I always acquire() the wakelock and not releasing it. What will be its impact other than it will drain my battery? – quiel Mar 26 '12 at 04:56
0

Best solution here: https://stackoverflow.com/a/2134602/1316372

@Override
protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
Community
  • 1
  • 1
HenryW
  • 3,551
  • 1
  • 23
  • 23