0

根据我的项目,我对特定设备 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

如果有人能给我建议,我真的很感激。

谢谢

4

1 回答 1

0

最后,我找到了自己的解决方案。

只需将两行代码放入 onReceive MainBroadCastReceiver.java

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

参考:错误 StrictMode$AndroidBlockGuardPolicy.onNetwork

于 2018-11-17T17:22:33.673 回答