Android Redmi 6A 问题 - 事件触发连接服务器失败 BOOT_COMPLETED
Android Redmi 6A issues - Failed to connect to the server trigger by Event BOOT_COMPLETED
根据我的项目,我对特定设备 REDMI 6A(使用 OS MIUI Android Oreo 8.1)有问题。这是一个简单的应用程序,仅通过 BOOT_COMPLETED 事件触发连接到服务器。我尝试检查互联网连接是否已连接。 Redmi 6A无法连接真的很奇怪,但是当我用MI A1测试时(OS Android Oreo 8.1 - Android One)设备也可以连接。我真的不明白为什么会这样。如果有人能给我建议,我将不胜感激。
这在代码下面。
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.NETWORK" />
.......
<service
android:name=".services.SocketJobIntentService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE" android:label="@string/socket_job" />
<receiver
android:name=".broadcast.MainBroadCastReceiver"
android:enabled="true"
android:label="@string/receiver_name">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".services.SocketSvc"></service>
SocketJobIntentService.java
public class SocketJobIntentService extends JobIntentService {
static Context context;
static final int JOB_ID = 1000;
public static void enqueueWork(Context _context, Intent work) {
context = _context;
enqueueWork(_context, SocketJobIntentService.class, JOB_ID, work);
}
public boolean isOnline(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
//should check null because in airplane mode it will be null
return (netInfo != null && netInfo.isConnected());
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
if(context==null){context = this;}
boolean isInternetConnected = isOnline(context);
if(isInternetConnected) {
Log.d("DT_JobIntentService","Executed!! and try to Calling Another Service 'SocketSvc' !!!");
Intent service = new Intent(this,SocketSvc.class);
startService(service);
}else{
Log.e("DT_WeGetEcon","isInternetConnected=false !!!!!! LOL!");
}
}}
MainBroadCastReceiver.java
public class MainBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
try {
String action = intent.getAction();
if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
Log.d("DT_MainBroadCastRceiver", action+" Called !!!");
SocketJobIntentService.enqueueWork(context, new Intent());
Log.d("DT_JobIntent","Try to call JobIntent");
}
}
catch (Exception exc){
Log.e("DT_ERR_ACTBOOT_COMPLTED",exc.getMessage());
}
}}
LogCat
LogCat MI A1
LogCat RedMI 6A
如果有人能给我建议,我将不胜感激。
谢谢
最后,我找到了自己的解决方案。
只需要在onReceive里面放两行代码MainBroadCastReceiver.java
public void onReceive(final Context context, Intent intent) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
参考:Error StrictMode$AndroidBlockGuardPolicy.onNetwork
根据我的项目,我对特定设备 REDMI 6A(使用 OS MIUI Android Oreo 8.1)有问题。这是一个简单的应用程序,仅通过 BOOT_COMPLETED 事件触发连接到服务器。我尝试检查互联网连接是否已连接。 Redmi 6A无法连接真的很奇怪,但是当我用MI A1测试时(OS Android Oreo 8.1 - Android One)设备也可以连接。我真的不明白为什么会这样。如果有人能给我建议,我将不胜感激。
这在代码下面。
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.NETWORK" />
.......
<service
android:name=".services.SocketJobIntentService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE" android:label="@string/socket_job" />
<receiver
android:name=".broadcast.MainBroadCastReceiver"
android:enabled="true"
android:label="@string/receiver_name">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".services.SocketSvc"></service>
SocketJobIntentService.java
public class SocketJobIntentService extends JobIntentService {
static Context context;
static final int JOB_ID = 1000;
public static void enqueueWork(Context _context, Intent work) {
context = _context;
enqueueWork(_context, SocketJobIntentService.class, JOB_ID, work);
}
public boolean isOnline(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
//should check null because in airplane mode it will be null
return (netInfo != null && netInfo.isConnected());
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
if(context==null){context = this;}
boolean isInternetConnected = isOnline(context);
if(isInternetConnected) {
Log.d("DT_JobIntentService","Executed!! and try to Calling Another Service 'SocketSvc' !!!");
Intent service = new Intent(this,SocketSvc.class);
startService(service);
}else{
Log.e("DT_WeGetEcon","isInternetConnected=false !!!!!! LOL!");
}
}}
MainBroadCastReceiver.java
public class MainBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
try {
String action = intent.getAction();
if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
Log.d("DT_MainBroadCastRceiver", action+" Called !!!");
SocketJobIntentService.enqueueWork(context, new Intent());
Log.d("DT_JobIntent","Try to call JobIntent");
}
}
catch (Exception exc){
Log.e("DT_ERR_ACTBOOT_COMPLTED",exc.getMessage());
}
}}
LogCat LogCat MI A1
LogCat RedMI 6A
如果有人能给我建议,我将不胜感激。
谢谢
最后,我找到了自己的解决方案。
只需要在onReceive里面放两行代码MainBroadCastReceiver.java
public void onReceive(final Context context, Intent intent) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
参考:Error StrictMode$AndroidBlockGuardPolicy.onNetwork