当应用未运行时,Android Broadcastreceiver不会触发 [英] Android broadcastreceiver not triggering when app is not running
问题描述
遵循本指南
何时
尝试设置 android:enabled = true
发送给您的接收者
赞
<接收者android:name =。Receiver
android:enabled = true>
< intent-filter>
< action android:name = android.intent.action.ACTION_POWER_CONNECTED />
< action android:name = android.intent.action.ACTION_POWER_DISCONNECTED />
< / intent-filter>
< / receiver>
更新
我刚刚在其中实现了它我的应用程序按您的方式进行,并且有效。但是我是在插入后使用右键单击-> new-> other->广播接收器
意图过滤器我得到了这样的代码
< receiver
android:name =。MyReceiver
android:enabled = true
android:exported = true>
< intent-filter>
< action android:name = android.intent.action.ACTION_POWER_CONNECTED />
< action android:name = android.intent.action.ACTION_POWER_DISCONNECTED />
< / intent-filter>
< / receiver>
它实际上调用了 onReceive
方法
Following this guide https://developer.android.com/training/monitoring-device-state/battery-monitoring.html
I made a receiver that should log battery info to a file every time charger is plugged or unplugged. The activity reads the content of this file and displays it on screen. This is supposed to work always regardless of the app running, but it doesn't. It only registers if the app is open/in memory, when I clear the memory or reboot it stops working.
Manifest:
<receiver android:name=".Receiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
Receiver:
public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent chargingIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
final int status = chargingIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
int level = chargingIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
int plugged = chargingIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
Battery battery = new Battery();
battery.setLevel(level);
battery.setDate(new Date());
battery.setPlugged(plugged);
battery.setStatus(status);
Logger.log(JSON.toJSONString(battery));
}
}
Logger
public class Logger {
public static void log(String text)
{
File logFile = new File(Environment.getExternalStorageDirectory().getPath()+"/batterylog.txt");
Log.d("files"," logfile "+logFile+": "+logFile.exists());
if (!logFile.exists())
{
try
{
logFile.createNewFile();
Log.d("files","new logfile created"+logFile);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(text).append("\r\n");
buf.flush();
buf.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
This is what it looks like after unplugging while app is open:
When it's not it's just empty.
Try to set android:enabled="true"
to your receiver
Like this
<receiver android:name=".Receiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
Update
I just implemented it in my app your's way and it worked. But I did it with right click->new->other->broadcast receiver
After inserting intent-filter I got code like this
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
And it actually calling onReceive
method
这篇关于当应用未运行时,Android Broadcastreceiver不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!