Android 当显示电池电量过低警告时,广播接收器电池电量过低崩溃
我有电池Android 当显示电池电量过低警告时,广播接收器电池电量过低崩溃,android,broadcastreceiver,batterymanager,Android,Broadcastreceiver,Batterymanager,我有电池BroadcastReceiver通过意向过滤器Battery_LOW和Battery_ok,此接收器工作正常,但当显示电池电量不足警告(电池百分比低于15%)时,应用程序因以下日志而崩溃: 12-18 03:13:49.651 1802-1802/E/AndroidRuntime:致命异常:主 进程:,PID:1802 java.lang.RuntimeException:无法实例化receiver receiver.BatteryLevel receiver:java.lang.Cl
BroadcastReceiver
通过意向过滤器Battery_LOW
和Battery_ok
,此接收器工作正常,但当显示电池电量不足警告(电池百分比低于15%)时,应用程序因以下日志而崩溃:
12-18 03:13:49.651 1802-1802/E/AndroidRuntime:致命异常:主
进程:,PID:1802
java.lang.RuntimeException:无法实例化receiver receiver.BatteryLevel receiver:java.lang.ClassNotFoundException:在路径:DexPathList[[zip文件”//data/app/-1.apk],zip文件“/data/data//code_cache/secondary dexes/-1.apk.classes2.zip”,zip上找不到类“receiver.batteryLevel receiver”
电池电平接收器:
public class BatteryLevelReceiver extends BroadcastReceiver {
private static final String TAG = BatteryLevelReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
try {
int curentPercent = getBatteryPercentage(context);
Log.e(TAG, "getBatteryPercentage: " + curentPercent);
} catch (Exception e){
e.printStackTrace();
}
}
public static int getBatteryPercentage(Context context) {
try {
if (Build.VERSION.SDK_INT >= 21) {
BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
return bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
} else {
IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, iFilter);
int level = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) : -1;
int scale = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1) : -1;
double batteryPct = level / (double) scale;
return (int) (batteryPct * 100);
}
}catch(Exception e) {
e.printStackTrace();
Log.e(TAG, "getBatteryPercentage: ");
}
return 1111;
}
}
<application
android:name=".AppController"
android:allowBackup="true"
android:icon="@mipmap/ic_logo"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:supportsRtl="true"
android:largeHeap="true"
android:hardwareAccelerated="true"
android:theme="@style/AppTheme">
...
<receiver android:name="receivers.BatteryLevelReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BATTERY_OKAY"/>
</intent-filter>
</receiver>
</application>
public class AppController extends MultiDexApplication {
...
@Override
public void onCreate() {
super.onCreate();
MultiDex.install(this);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
android {
compileSdkVersion 28
defaultConfig {
...
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
}
}
vectorDrawables.useSupportLibrary = true
}
dexOptions {
jumboMode true
javaMaxHeapSize "4g"
}
buildTypes {
release {
// Enables code shrinking, obfuscation, and optimization for only
// your project's release build type.
minifyEnabled true
// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources true
// Includes the default ProGuard rules files that are packaged with
// the Android Gradle plugin. To learn more, go to the section about
// R8 configuration files.
proguardFiles getDefaultProguardFile(
'proguard-android-optimize.txt'),
'proguard-rules.pro'
}
debug{
// Enables code shrinking, obfuscation, and optimization for only
// your project's release build type.
minifyEnabled true
// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources true
// Includes the default ProGuard rules files that are packaged with
// the Android Gradle plugin. To learn more, go to the section about
// R8 configuration files.
proguardFiles getDefaultProguardFile(
'proguard-android-optimize.txt'),
'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-annotations:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
...
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
implementation 'com.android.support:multidex:1.0.3'
}
apply plugin: 'com.google.gms.google-services'
AndroidManifest:
public class BatteryLevelReceiver extends BroadcastReceiver {
private static final String TAG = BatteryLevelReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
try {
int curentPercent = getBatteryPercentage(context);
Log.e(TAG, "getBatteryPercentage: " + curentPercent);
} catch (Exception e){
e.printStackTrace();
}
}
public static int getBatteryPercentage(Context context) {
try {
if (Build.VERSION.SDK_INT >= 21) {
BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
return bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
} else {
IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, iFilter);
int level = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) : -1;
int scale = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1) : -1;
double batteryPct = level / (double) scale;
return (int) (batteryPct * 100);
}
}catch(Exception e) {
e.printStackTrace();
Log.e(TAG, "getBatteryPercentage: ");
}
return 1111;
}
}
<application
android:name=".AppController"
android:allowBackup="true"
android:icon="@mipmap/ic_logo"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:supportsRtl="true"
android:largeHeap="true"
android:hardwareAccelerated="true"
android:theme="@style/AppTheme">
...
<receiver android:name="receivers.BatteryLevelReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BATTERY_OKAY"/>
</intent-filter>
</receiver>
</application>
public class AppController extends MultiDexApplication {
...
@Override
public void onCreate() {
super.onCreate();
MultiDex.install(this);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
android {
compileSdkVersion 28
defaultConfig {
...
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
}
}
vectorDrawables.useSupportLibrary = true
}
dexOptions {
jumboMode true
javaMaxHeapSize "4g"
}
buildTypes {
release {
// Enables code shrinking, obfuscation, and optimization for only
// your project's release build type.
minifyEnabled true
// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources true
// Includes the default ProGuard rules files that are packaged with
// the Android Gradle plugin. To learn more, go to the section about
// R8 configuration files.
proguardFiles getDefaultProguardFile(
'proguard-android-optimize.txt'),
'proguard-rules.pro'
}
debug{
// Enables code shrinking, obfuscation, and optimization for only
// your project's release build type.
minifyEnabled true
// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources true
// Includes the default ProGuard rules files that are packaged with
// the Android Gradle plugin. To learn more, go to the section about
// R8 configuration files.
proguardFiles getDefaultProguardFile(
'proguard-android-optimize.txt'),
'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-annotations:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
...
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
implementation 'com.android.support:multidex:1.0.3'
}
apply plugin: 'com.google.gms.google-services'
Gradle:
public class BatteryLevelReceiver extends BroadcastReceiver {
private static final String TAG = BatteryLevelReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
try {
int curentPercent = getBatteryPercentage(context);
Log.e(TAG, "getBatteryPercentage: " + curentPercent);
} catch (Exception e){
e.printStackTrace();
}
}
public static int getBatteryPercentage(Context context) {
try {
if (Build.VERSION.SDK_INT >= 21) {
BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
return bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
} else {
IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, iFilter);
int level = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) : -1;
int scale = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1) : -1;
double batteryPct = level / (double) scale;
return (int) (batteryPct * 100);
}
}catch(Exception e) {
e.printStackTrace();
Log.e(TAG, "getBatteryPercentage: ");
}
return 1111;
}
}
<application
android:name=".AppController"
android:allowBackup="true"
android:icon="@mipmap/ic_logo"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:supportsRtl="true"
android:largeHeap="true"
android:hardwareAccelerated="true"
android:theme="@style/AppTheme">
...
<receiver android:name="receivers.BatteryLevelReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BATTERY_OKAY"/>
</intent-filter>
</receiver>
</application>
public class AppController extends MultiDexApplication {
...
@Override
public void onCreate() {
super.onCreate();
MultiDex.install(this);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
android {
compileSdkVersion 28
defaultConfig {
...
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
}
}
vectorDrawables.useSupportLibrary = true
}
dexOptions {
jumboMode true
javaMaxHeapSize "4g"
}
buildTypes {
release {
// Enables code shrinking, obfuscation, and optimization for only
// your project's release build type.
minifyEnabled true
// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources true
// Includes the default ProGuard rules files that are packaged with
// the Android Gradle plugin. To learn more, go to the section about
// R8 configuration files.
proguardFiles getDefaultProguardFile(
'proguard-android-optimize.txt'),
'proguard-rules.pro'
}
debug{
// Enables code shrinking, obfuscation, and optimization for only
// your project's release build type.
minifyEnabled true
// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources true
// Includes the default ProGuard rules files that are packaged with
// the Android Gradle plugin. To learn more, go to the section about
// R8 configuration files.
proguardFiles getDefaultProguardFile(
'proguard-android-optimize.txt'),
'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-annotations:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
...
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
implementation 'com.android.support:multidex:1.0.3'
}
apply plugin: 'com.google.gms.google-services'
我将意向操作从低更改为已更改,但问题未得到解决。您正在尝试在广播接收器中注册广播接收器 除去
Intent batteryStatus=context.registerReceiver(null,iFilter);
在活动/片段中注册您的服务
如果您想发送广播,您应该调用
context.sendBroadcast
,我做了更改,但问题仍然存在删除“Intent batteryStatus=context.registerReceiver(null,iFilter);”在MainActivity删除生成文件夹中注册广播接收器,清理项目并重新生成