簡體   English   中英

Android電池電量通知,並在5分鍾后關閉/關閉設備電源

[英]Android Battery Level Notification & after 5 mins shutdown/Power Off the Device

我有一個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 &&如何自動關閉電源?

請任何人在這方面幫助我...

提前致謝..

確定了如何檢測電池電量后,我建議您不要向該欄發送通知,而只需向設備發送shutdown命令,這將在內部生成shutdown對話框。

更新資料

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

檢查ACTION_BATTERY_CHANGED,BATTERY_PLUGGED_USB或BATTERY_PLUGGED_AC希望這會有所幫助: 如何檢測電源連接狀態?

要檢查設備是否已插入電源,您可以嘗試以下方法,

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線程發布方法的插入狀態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM