我创建了一个应用程序,用于记录应用程序的数据使用情况并将其写入手机 SD 卡上的文件中。现在我试图通过使用 adb 来允许这个进程在后台运行。我希望能够向应用程序发送信号/广播以写入当前的数据使用情况。随后能够发送第二个信号,写入数据使用情况和新时间。这样人们就可以查看应用程序使用了多少数据。
到目前为止,我已经尝试使用 adb 并向应用程序发送广播,它似乎工作正常,但我无法通过使用 mediascanner 将文件保存到 SD 卡。
这是当我发送“adb -d shell am Broadcast -n com.axel.datatracking/.IntentReceiver --es --start 'com.linku.android.mobile_emergency.app.activity' ”时运行的函数到应用程序。
public void startLog(Context context, SimplifiedAppInfo selectedApp) {
int i = 0;
String name = "dataFile.csv";
Log.d("update", "sort of works maybe");
// make the file if it already exists increment the name by 1
try {
testFile = new File(context.getExternalFilesDir(null), name);
while(testFile.exists()) {
i++;
name = this.makeFileName(i);
testFile = new File(context.getExternalFilesDir(null), name);
}
Log.d("filename", name);
testFile.createNewFile();
} catch (IOException e) {
Log.d("broke", "Unable to write my dood");
}
// try to write to the file
try {
fOut = new FileOutputStream(testFile);
BufferedWriter writer = new BufferedWriter(new FileWriter(testFile, true));
startingDown = selectedApp.getDownbytes();
startingUp = selectedApp.getUpbyts();
startingTime = System.currentTimeMillis();
writer.write("data,up,down\n");
writer.write("Initial,"+selectedApp.getUpbyts()+","+selectedApp.getDownbytes()+"\n");
writer.close();
// refresh the data file
//MediaScannerConnection.scanFile(context, new String[]{this.testFile.toString()}, null, null);
} catch (IOException e) {
Log.d("broke", "cant write to the file");
}
}
这是写入最终结果的函数,由“adb -d shell am Broadcast -n com.axel.datatracking/.IntentReceiver --es --end 'com.linku.android.mobile_emergency.app”运行。 Activity '"
public void endLog(Context context, SimplifiedAppInfo selectedApp) {
// write end results to file
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(testFile, true));
writer.write("End,"+selectedApp.getUpbyts()+","+selectedApp.getDownbytes()+"\n");
float effectiveDown = selectedApp.getDownbytes() - startingDown;
float effectiveUp = selectedApp.getUpbyts() - startingUp;
writer.write("Effective,"+effectiveUp+","+effectiveDown+"\n");
float timePassed = ((float) ((System.currentTimeMillis() - startingTime)/1000));
float avgUp = effectiveUp/timePassed;
float avgDown = effectiveDown/timePassed;
writer.write("Average bytes/sec,"+avgUp+","+avgDown+"\n");
writer.close();
fOut.close();
//MediaScannerConnection.scanFile(context, new String[]{this.testFile.toString()},null, null);
} catch (IOException e) {
Log.d("broke", "the write dont work");
}
}
广播接收器组件不允许绑定(bind)到服务。
编辑:除了使用广播接收器之外,我对其他解决方案持开放态度,我只需要能够在应用程序外部记录数据并从终端专注于另一个应用程序。
最佳答案
在 list 中注册接收器,然后创建一些接收器类,例如:
public class CustomReceiver extends BroadcastReceiver {
@Override
public final void onReceive(Context context, Intent intent){
//// here you can start your own service or do logic here
}
这里是如何在 list 中注册接收器,只需选择不同的 Intent 过滤器:
<receiver android:name="com.example.example.CustomReceiver ">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
只要触发电话操作,onReceive
就会被调用。
我建议阅读有关 Intents 的文档,因为 Android 有很多限制,例如
关于java - 如何在应用程序未聚焦时记录数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57087888/