gpt4 book ai didi

Android异常android.app.IntentReceiverLeaked

转载 作者:行者123 更新时间:2023-12-05 00:22:04 25 4
gpt4 key购买 nike

我正在使用 BroadcastReceiver 来监控电池电量。我提到了this我的代码是

public class PowerConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;

int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
}

}

并将其添加到我的 list 中
  <receiver android:name=".PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>

它可以工作,但在 onReceive 中,intent 没有任何值(value)。当我尝试在 onReceive 中启动 Activity 时,应用程序停止并返回以下错误。
android.app.IntentReceiverLeaked: Activity com.example.batterycharging.Activity.MainActivity has leaked IntentReceiver com.example.batterycharging.Activity.MainActivity$1@b7548490 that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:756)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:551)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:800)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:787)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:781)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318)
at com.example.suresh.batterycharging.Activity.MainActivity.onCreate(MainActivity.java:40)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

我不知道该怎么做。谁能帮我检测电池状态。提前致谢。

这是 MainActivity.java 类
package com.example.batterycharging.Activity;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.example.suresh.batterycharging.R;


public class MainActivity extends ActionBarActivity {

/*private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
@Override
//When Event is published, onReceive method is called
public void onReceive(Context c, Intent i) {
//Get Battery %
int level = i.getIntExtra("level", 0);
//Find the progressbar creating in main.xml
ProgressBar pb = (ProgressBar) findViewById(R.id.progressbar);
//Set progress level with battery % value
pb.setProgress(level);
//Find textview control created in main.xml
TextView tv = (TextView) findViewById(R.id.textfield);
//Set TextView with text
tv.setText("Battery Level: " + Integer.toString(level) + "%");
}

};*/


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// registerReceiver(mBatInfoReceiver, new IntentFilter(
// Intent.ACTION_BATTERY_CHANGED));
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

最佳答案

您正在注册 BroadcastReceiverMainActivity 第 40 行.

您需要 unregisterReceiver(...)onPause() .

您需要注册 BroadcastReceiver在您的 AndroidManifest.xml文件或 registerReceiver(...) .你不应该两者都做。

关于Android异常android.app.IntentReceiverLeaked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29846935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com