2020-05-23

遇到难题求高手帮忙解决

遇到难题求高手帮忙解决


背景:

最近在做一个关于android的续航时间测试apk,从开始测试记录时间到低电自动关机时这整个的时间,当前采用监听关机广播的来实际统计续航时间,但遇到的问题是同样的测试apk在android8及以下系统均能正常监听到关机广播,但从android 9开始就监听不到关机广播了。

 

下面是我的监听广播注册方法(采用静态注册关机 广播方式)

AndroidManifest.

 1 <?"1.0" encoding="utf-8"?> 2 <manifest "" 3  package="com.tcldou.dou"> 4  5  <application 6   android:allowBackup="true" 7   android:icon="@mipmap/ic_launcher" 8   android:label="@string/app_name" 9   android:roundIcon="@mipmap/ic_launcher_round"10   android:supportsRtl="true"11   android:theme="@style/AppTheme">12 13   <receiver14    android:name=".ShutDownBroadcastReceiver"15    android:exported="true">16    <!--<intent-filter>-->17     <!--<action android:name="android.intent.action.BOOT_COMPLETED" />-->18     <!--<category android:name="android.intent.category.HOME" />-->19    <!--</intent-filter>-->20 21    <intent-filter>22     <action android:name="android.intent.action.ACTION_SHUTDOWN" />23     <!--<category android:name="android.intent.category.HOME" />-->24    </intent-filter>25   </receiver>26  </application>27 28  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />29  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />30  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />31  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>32  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />33  <uses-permission android:name="android.permission.READ_PHONE_STATE"/>34 35 </manifest>

 

下面是实现接收关机广播的处理实现

 1 import android.content.BroadcastReceiver; 2 import android.content.Context; 3 import android.content.Intent; 4  5 import java.io.File; 6 import java.io.FileWriter; 7 import java.io.IOException; 8 import java.text.SimpleDateFormat; 9 import java.util.Date;10 11 public class ShutDownBroadcastReceiver extends BroadcastReceiver {12  @Override13  public void onReceive(Context context, Intent intent){14   System.out.println("---------Shut down-------------");15   File file = new File ("/sdcard/result.txt");16   try {17    if(!file.exists())18    {19     file.createNewFile();20    }21    FileWriter writer = null;22 23     writer = new FileWriter(file, true);24 25    writer.write("Shut_down [" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()) + "]\n");26    writer.close();27   } catch (IOException e) {28    e.printStackTrace();29   }30  }31 }

 


No comments:

Post a Comment