【问题标题】:Android Battery Level Notification & after 5 mins shutdown/Power Off the DeviceAndroid 电池电量通知和 5 分钟后关机/关闭设备
【发布时间】:2012-01-17 04:14:35
【问题描述】:

我有 Android 应用程序,它是 Sales Rep 应用程序。当他使用该应用程序时,首先需要显示通知,例如您的电池电量为 10% ...,然后在 2 分钟后如果他没有插上电源,它会自动显示需要关闭设备。

我是这样创作的

     public class BatteryLevelActivity extends BroadcastReceiver{

     @Override
     public void onReceive(Context context, Intent intent) {
        int level = intent.getIntExtra("level", 0);  
            Toast.makeText(context, "Battery Level is "+level+"%", Toast.LENGTH_LONG) ;

    }
}

还有我的 androidManifest 文件

       <receiver android:name=".service.BatteryLevelActivity">
         <intent-filter>
            <action android:name="android.intent.action.BATTERY_CHANGED" />
        </intent-filter>
    </receiver> 

我喜欢这样打电话的地方

     batteryLevelReceiver = new BatteryLevelReceiver();
    IntentFilter intentFilter1 = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    intentFilter1.addAction(Intent.ACTION_BATTERY_LOW);
    registerReceiver(batteryLevelReceiver, intentFilter1);

转到BatteryLevelReceiver 类。我真的不知道如何查看power is plugged or not &&显示通知后如何自动关机?

请任何人帮助我...

提前谢谢..

【问题讨论】:

    标签: android battery


    【解决方案1】:

    一旦您确定了如何检测电池电量,我建议您不要向工具栏发送通知,只需向设备发送关机命令,这将在内部生成关机对话框。

    更新

    http://developer.android.com/reference/android/content/Intent.html#ACTION_SHUTDOWN

    【讨论】:

      【解决方案2】:

      要检查设备是否已插入,您可以试试这个东西,

      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;
          }
      

      对于关闭设备here 是一个您要检查的线程,并且用于检查已从this 线程发布的方法的插入状态。

      【讨论】:

      • 否。仍然没有显示关机屏幕/对话框。我做了`PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "我的标签"); wl.acquire();`
      • 您可以结帐this one
      【解决方案3】:

      检查 ACTION_BATTERY_CHANGED 、 BATTERY_PLUGGED_USB 或 BATTERY_PLUGGED_AC 希望这会有所帮助:How to detect power connected state?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-29
        相关资源
        最近更新 更多