2012-08-31 64 views
1

我編寫了一個小應用程序,用於在循環中播放單個視頻。我怎麼能意識到,當電源連接並再次關閉活動時,設備醒來並播放此視頻。之後,還應關閉屏幕以避免電池電量耗盡。斷開電源時關閉活動

實際的代碼如下所示:

package org.adem.receiver; 

import android.app.Activity; 
import android.app.ActivityManager; 
import android.app.KeyguardManager; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.PowerManager; 
import android.util.Log; 
import org.adem.activities.AdvertiserActivity; 
import org.adem.global.GlobalVariables; 

public class AppReceiver extends BroadcastReceiver { 

    private static final String TAG = AppReceiver.class.getSimpleName(); 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (Intent.ACTION_POWER_CONNECTED.equals(intent.getAction())) { 
      Log.d(TAG, "##Receiver triggered with " + intent.getAction()); 
      if (!checkForRunningActivity(context)) { 
       initStart(context); 
       Intent advertiserIntent = new Intent(context, AdvertiserActivity.class); 
       advertiserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       Log.d(TAG, "##Activity started" + advertiserIntent.getComponent().getShortClassName()); 
       context.startActivity(advertiserIntent); 
      } else { 
       Log.d(TAG, "##" + intent.getAction() + " received but ignored. Activity still running."); 
      } 
     } else if (Intent.ACTION_POWER_DISCONNECTED.equals(intent.getAction())) { 
      Log.d(TAG, "##Receiver triggered with " + intent.getAction()); 
      stopAllAndTurnOff(context); 

      Log.d(TAG, "##" + intent.getAction() + " received but ignored. Activity is not running."); 
     } 

    } 

    private void initStart(Context context) { 
     getWakeLock(context).acquire(); 
     GlobalVariables.DEVICE_READY = true; 
     getKeyGuardLock(context).disableKeyguard(); 
     Log.d(TAG, "##Wake Lock acquired"); 
    } 

    private void stopAllAndTurnOff(Context context) { 
     GlobalVariables.DEVICE_READY = false; 

     if (getWakeLock(context).isHeld()) { 
      getWakeLock(context).release(); 
      Log.d(TAG, "##Wake Lock released"); 
     } 
    } 

    private KeyguardManager.KeyguardLock getKeyGuardLock(Context context) { 
     if (GlobalVariables.getLock() == null) { 
      KeyguardManager.KeyguardLock lock; 
      KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Activity.KEYGUARD_SERVICE); 
      lock = keyguardManager.newKeyguardLock(Context.KEYGUARD_SERVICE); 
      GlobalVariables.setLock(lock); 
      Log.d(TAG, "##New KeyGuard initialized"); 
     } 
     return GlobalVariables.getLock(); 
    } 

    private PowerManager.WakeLock getWakeLock(Context context) { 
     if (GlobalVariables.getWl() == null) { 
      PowerManager.WakeLock wl; 
      PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
      wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG); 
      GlobalVariables.setWl(wl); 
      Log.d(TAG, "##New Wake Lock initialized"); 
     } 
     return GlobalVariables.getWl(); 
    } 

    private boolean checkForRunningActivity(Context context) { 
     ActivityManager manager = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE); 
     for (ActivityManager.RunningTaskInfo task : manager.getRunningTasks(Integer.MAX_VALUE)) { 
      Log.d(TAG, task.baseActivity.getClassName()); 
      if (task.baseActivity.getClassName().equals("org.adem.activities.AdvertiserActivity") 
        || task.baseActivity.getClassName().equals("org.adem.activities.VideoViewActivity")) { 
       return true; 
      } 
     } 
     return false; 
    } 
} 

當我測試這在真實設備上,它說每一次,該活動仍在運行。但爲什麼?

感謝

阿德姆

回答

3

您可以添加廣播接收器接收更換電池。對於Intent.ACTION_BATTERY_CHANGED

Intent batteryIntent = context.getApplicationContext().registerReceiver(null, 
       new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 

一旦接收器接收檢查連接

獲取響應。

插中/充電:

Android的USB連接

public static boolean isConnected(Context context) { 
    Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); 
    return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB; 
} 

加入許可清單文件:

android.permission.DEVICE_POWER 
0

我覺得首先你把這個清單中。

android.permission.DEVICE_POWER

即開始播放後receiver.then開始您的活動。 希望它能起作用。