javaandroidandroid-studioandroid-8.1-oreomiui

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. it's simple application, only connect to the server trigger by BOOT_COMPLETED event. 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. 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

<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

I really appreciate if anyone can advice me.

Thanks


Solution

  • Finally, I found my own solution.

    Just put two line code inside 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