广播接收器未注册 onResume() 而 onPause() 注册
Broadcast receiver not registering with onResume() while onPause() does
我正在尝试显示以下状态:
电池电量 (t/f)
USB 充电 (t/f)
电池交流充电 (t/f)
电池电量(百分比)
在扩展控件中更改充电器连接后。
在 onResume() 中,广播接收器状态
Cannot resolve method
'registerReceiver(android.content.BroadcastReceiver)
它在 onPause() 中注册没有问题。
感谢您的帮助,这似乎是一个很难解决的问题。
主要活动:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
BroadcastReceiver broadcastReceiver;
StringBuilder stringBuilder;
public MyReciever myReciever= new MyReciever();
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
myReciever.onReceive(MainActivity.this, intent);
StringBuilder stringBuilder = new StringBuilder();
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
intent = registerReceiver(null, ifilter);
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;
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float)scale;
stringBuilder.append(String.valueOf(isCharging));
stringBuilder.append(String.valueOf(usbCharge));
stringBuilder.append(String.valueOf(acCharge));
stringBuilder.append(String.valueOf(batteryPct));
String str = stringBuilder.toString();
TextView display = (TextView)findViewById(R.id.results);
display.setText(str);
}
};
}
///Not Registering here
@Override
protected void onResume() {
super.onResume();
registerReceiver(broadcastReceiver);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
}
}
我的收件人:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent newintent = new Intent(context.getApplicationContext(), MainActivity.class);
newintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newintent);
}
}
activity_main.XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/results"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
您的问题是方法 registerReciever
有 2 个参数,而不是 1 个。
这些参数是
- 要注册的广播接收器
- 意图过滤器
由于您只提供了第一个参数,因此未找到该方法,并且没有仅采用一个参数的方法版本(重载方法)。
因此,为了解决这个问题,您必须像这样调用方法:
protected void onResume() {
registerReceiver(broadcastReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
super.onResume();
}
创建 IntentFilter 的文档可在此处获得https://developer.android.com/reference/android/content/IntentFilter.html
试试这个
@Override
protected void onResume() {
registerReceiver(broadcastReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
super.onResume();
}
更多信息请参考https://developer.android.com/training/monitoring-device-state/battery-monitoring.html
我正在尝试显示以下状态:
电池电量 (t/f) USB 充电 (t/f) 电池交流充电 (t/f) 电池电量(百分比)
在扩展控件中更改充电器连接后。
在 onResume() 中,广播接收器状态
Cannot resolve method 'registerReceiver(android.content.BroadcastReceiver)
它在 onPause() 中注册没有问题。 感谢您的帮助,这似乎是一个很难解决的问题。
主要活动:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
BroadcastReceiver broadcastReceiver;
StringBuilder stringBuilder;
public MyReciever myReciever= new MyReciever();
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
myReciever.onReceive(MainActivity.this, intent);
StringBuilder stringBuilder = new StringBuilder();
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
intent = registerReceiver(null, ifilter);
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;
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float)scale;
stringBuilder.append(String.valueOf(isCharging));
stringBuilder.append(String.valueOf(usbCharge));
stringBuilder.append(String.valueOf(acCharge));
stringBuilder.append(String.valueOf(batteryPct));
String str = stringBuilder.toString();
TextView display = (TextView)findViewById(R.id.results);
display.setText(str);
}
};
}
///Not Registering here
@Override
protected void onResume() {
super.onResume();
registerReceiver(broadcastReceiver);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
}
}
我的收件人:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent newintent = new Intent(context.getApplicationContext(), MainActivity.class);
newintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newintent);
}
}
activity_main.XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/results"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
您的问题是方法 registerReciever
有 2 个参数,而不是 1 个。
这些参数是
- 要注册的广播接收器
- 意图过滤器
由于您只提供了第一个参数,因此未找到该方法,并且没有仅采用一个参数的方法版本(重载方法)。
因此,为了解决这个问题,您必须像这样调用方法:
protected void onResume() {
registerReceiver(broadcastReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
super.onResume();
}
创建 IntentFilter 的文档可在此处获得https://developer.android.com/reference/android/content/IntentFilter.html
试试这个
@Override
protected void onResume() {
registerReceiver(broadcastReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
super.onResume();
}
更多信息请参考https://developer.android.com/training/monitoring-device-state/battery-monitoring.html