4

按照本指南https://developer.android.com/training/monitoring-device-state/battery-monitoring.html

我制作了一个接收器,每次插入或拔出充电器时都应将电池信息记录到文件中。Activity 读取该文件的内容并将其显示在屏幕上。无论应用程序运行如何,这都应该始终有效,但事实并非如此。它仅在应用程序打开/在内存中时注册,当我清除内存或重新启动时它停止工作。

显现:

  <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>

接收者:

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));
    }
}

记录器

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();
    }
}
}

这是应用打开时拔下电源后的样子:

在此处输入图像描述

当它不是时,它只是空的。

4

2 回答 2

2

尝试设置android:enabled="true"到您的接收器

像这样

 <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>

更新

我刚刚以您的方式在我的应用程序中实现了它并且它有效。但我做到了right click->new->other->broadcast receiver

插入意图过滤器后,我得到了这样的代码

    <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方法

于 2017-01-20T15:16:44.940 回答
0

您是否尝试过使用唤醒锁?通常应用程序会在接收到广播时唤醒,但如果没有(在少数设备中发生),则尝试使用唤醒锁,这将强制唤醒设备。

如何使用唤醒锁:

//注册唤醒锁

if (mWakeLock == null) {
        PowerManager pm = (PowerManager)yourcontext.getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, yourcontext);
        mWakeLock.setReferenceCounted(false);

}

//使用获取应用唤醒锁

mWakeLock.acquire();
于 2017-01-20T16:20:29.700 回答