我正在开发一个项目,其中应用程序发送每个电池百分比变化的通知。我将仅在应用程序处于前台时收到通知。当我把它放在后台时,我不会收到任何通知。这是码。我需要做哪些修改,以便即使我的应用程序在后台也会收到通知。
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView textview;
IntentFilter intentfilter;
int deviceStatus;
String currentBatteryStatus="Battery Info";
int batteryLevel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview = (TextView)findViewById(R.id.textViewBatteryStatus);
intentfilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
MainActivity.this.registerReceiver(broadcastreceiver,intentfilter);
}
private BroadcastReceiver broadcastreceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
deviceStatus = intent.getIntExtra(BatteryManager.EXTRA_STATUS,-1);
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int batteryLevel=(int)(((float)level / (float)scale) * 100.0f);
if(deviceStatus == BatteryManager.BATTERY_STATUS_CHARGING){
textview.setText(currentBatteryStatus+" = Charging at "+batteryLevel+" %");
//Notifying Operation..
}
if(deviceStatus == BatteryManager.BATTERY_STATUS_DISCHARGING){
textview.setText(currentBatteryStatus+" = Discharging at "+batteryLevel+" %");
//Notifying Operation..
}
if (deviceStatus == BatteryManager.BATTERY_STATUS_FULL){
textview.setText(currentBatteryStatus+"= Battery Full at "+batteryLevel+" %");
//Notifying Operation..
}
if(deviceStatus == BatteryManager.BATTERY_STATUS_UNKNOWN){
textview.setText(currentBatteryStatus+" = Unknown at "+batteryLevel+" %");
//Notifying Operation..
}
if (deviceStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING){
textview.setText(currentBatteryStatus+" = Not Charging at "+batteryLevel+" %");
//Notifying Operation..
}
}
};
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vinayts.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我已将Manifest编辑如下,但后来也显示错误,我在内部类使用$ symbol声明清单
<?xml version="1.0" encoding="utf-8"?><!--
Copyright 2013 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bledemo.android.bluetoothlegatt"
android:versionCode="1"
android:versionName="1.0">
<!-- Min/target SDK versions (<uses-sdk>) managed by build.gradle -->
<!--
Declare this required feature if you want to make the app available to BLE-capable
devices only. If you want to make your app available to devices that don't support BLE,
you should omit this in the manifest. Instead, determine BLE capability by using
PackageManager.hasSystemFeature(FEATURE_BLUETOOTH_LE)
-->
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light">
<activity
android:name="com.bledemo.android.bluetoothlegatt.DeviceScanActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.bledemo.android.bluetoothlegatt.DeviceControlActivity"
android:screenOrientation="portrait"/>
<service
android:name="com.bledemo.android.bluetoothlegatt.BluetoothLeService"
android:enabled="true" />
<activity
android:name="com.bledemo.android.bluetoothlegatt.SettingActivity"
android:label="@string/title_activity_setting"
android:screenOrientation="portrait"></activity>
<receiver android:name="com.bledemo.android.bluetoothlegatt.MainActivity$broadcastreceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
</application>
</manifest>
答案 0 :(得分:0)
要在应用未运行时接收电池电量变化,您应该在清单中注册广播接收器,通过在意图过滤器中定义ACTION_POWER_CONNECTED和ACTION_POWER_DISCONNECTED来监听这两个事件:
<receiver android:name=".BroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
您需要公开访问BroadcastReceiver,然后在清单文件中更新接收器android:name中的位置。
在此处查找更多详情: https://developer.android.com/training/monitoring-device-state/battery-monitoring.html