Ask the expert to help solve the problem -- Android P can't turn off the radio when it starts listening

Background:

Recently, I am doing a test apk on android's endurance time. From the start of the test recording time to the time when the low-power automatic shutdown occurs, the current method is to monitor the shutdown broadcast to actually count the endurance time. However, the problem encountered is that the same test apk can normally monitor the shutdown broadcast in android 8 and the following systems, but it can't monitor the shutdown broadcast since android 9.

 

Here is my listening broadcast registration method (using static registration shutdown broadcast method)

AndroidManifest.xml as follows

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 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         <receiver
14             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>

 

The following is the processing implementation of receiving shutdown broadcast

 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 }

Tags: Android Java xml encoding

Posted on Sat, 23 May 2020 11:23:21 -0400 by mike2098