简体   繁体   English

Android Redmi 6A问题-由事件BOOT_COMPLETED无法连接到服务器触发器

[英]Android Redmi 6A issues - Failed to connect to the server trigger by Event BOOT_COMPLETED

I have problem with specific device REDMI 6A (with OS MIUI Android Oreo 8.1), according to my project. 根据我的项目,我对特定设备REDMI 6A(带有OS MIUI Android Oreo 8.1)有问题。 it's simple application, only connect to the server trigger by BOOT_COMPLETED event. 它是简单的应用程序,仅通过BOOT_COMPLETED事件连接到服务器触发器。 i try to check the internet connection is connected or not. 我尝试检查互联网连接是否连接。 it is really weird the Redmi 6A can't connect,but when i test with MI A1 (OS Android Oreo 8.1 - Android One) the device can connect as well. Redmi 6A无法连接真的很奇怪,但是当我使用MI A1(OS Android Oreo 8.1-Android One)测试时,设备也可以连接。 I really don't understand why this can be happen. 我真的不明白为什么会发生这种情况。 I really appreciate if some one can advice me. 如果有人可以建议我,我非常感谢。

This Below the code. 这下面的代码。

AndroidManifest.xml 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 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 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 LogCat MI A1

LogCat RedMI 6A LogCat RedMI 6A

I really appreciate if anyone can advice me. 如果有人可以给我建议,我真的很感激。

Thanks 谢谢

Finally, I found my own solution. 最后,我找到了自己的解决方案。

Just put two line code inside onReceive MainBroadCastReceiver.java 只需在onReceive MainBroadCastReceiver.java中放入两行代码

public void onReceive(final Context context, Intent intent) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

ref to : Error StrictMode$AndroidBlockGuardPolicy.onNetwork ref: 错误StrictMode $ AndroidBlockGuardPolicy.onNetwork

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM