遇到难题求高手帮忙解决--Android P开始监听不到关机广播

背景:

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

 

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

AndroidManifest.xml如下

 1 "1.0" encoding="utf-8"?>
 2 "http://schemas.android.com/apk/res/android"
 3     package="com.dou.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         <receiver
14             android:name=".ShutDownBroadcastReceiver"
15             android:exported="true">
16             
17                 
18                 
19             
20 
21             
22                 "android.intent.action.ACTION_SHUTDOWN" />
23                 
24             
25         
26     
27 
28     "android.permission.READ_EXTERNAL_STORAGE" />
29     "android.permission.WRITE_EXTERNAL_STORAGE" />
30     "android.permission.RECEIVE_BOOT_COMPLETED" />
31     "android.permission.ACCESS_WIFI_STATE"/>
32     "android.permission.ACCESS_NETWORK_STATE" />
33     "android.permission.READ_PHONE_STATE"/>
34 
35 

 

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

 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     @Override
13     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 }

 

你可能感兴趣的:(遇到难题求高手帮忙解决--Android P开始监听不到关机广播)