无法访问 JobIntentService 中的 class 字段值
Trouble accessing class field values inside a JobIntentService
我正在尝试在 Android 上使用 JobIntentService
,但在访问 onHandleWork
方法中的 class 字段时遇到问题。我的 discoverable
字段在 onHandleWork
方法中似乎总是为空。
这是我的 JobIntentService class:
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import io.hypertrack.smart_scheduler.Job;
import io.hypertrack.smart_scheduler.SmartScheduler;
public class NetworkCheckService extends JobIntentService {
private static final int jobId = 1;
private Settings settings;
private NetworkInformation networkInfo;
private Networks networks;
private Discoverable discoverable;
{
Logger.addLogAdapter(new AndroidLogAdapter());
}
// Needs an empty constructor for some reason.
public NetworkCheckService() {
}
public NetworkCheckService(Context context) {
this.settings = new Settings(context);
this.networkInfo = new NetworkInformation(context);
this.networks = new Networks(context, networkInfo);
this.discoverable = new Discoverable(settings, networks);
}
private Discoverable getDiscoverable() {
return discoverable;
}
static void enqueueWork(Context context, Intent work) {
enqueueWork(context, NetworkCheckService.class, jobId, work);
}
public void onHandleWork(@NonNull Intent intent) {
Logger.d(discoverable); // discoverable is null
Discoverable foo = getDiscoverable(); // foo is null
}
}
我的主要activityclass:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NetworkCheckService networkCheckService = new NetworkCheckService(this);
networkCheckService.enqueueWork(this, new Intent());
}
}
我们不应该在服务中创建任何构造函数class。它由 android 系统完成。这就是您的成员变量变为 null
的原因。最好在 onHandleWork 方法中初始化它们。
我正在尝试在 Android 上使用 JobIntentService
,但在访问 onHandleWork
方法中的 class 字段时遇到问题。我的 discoverable
字段在 onHandleWork
方法中似乎总是为空。
这是我的 JobIntentService class:
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import io.hypertrack.smart_scheduler.Job;
import io.hypertrack.smart_scheduler.SmartScheduler;
public class NetworkCheckService extends JobIntentService {
private static final int jobId = 1;
private Settings settings;
private NetworkInformation networkInfo;
private Networks networks;
private Discoverable discoverable;
{
Logger.addLogAdapter(new AndroidLogAdapter());
}
// Needs an empty constructor for some reason.
public NetworkCheckService() {
}
public NetworkCheckService(Context context) {
this.settings = new Settings(context);
this.networkInfo = new NetworkInformation(context);
this.networks = new Networks(context, networkInfo);
this.discoverable = new Discoverable(settings, networks);
}
private Discoverable getDiscoverable() {
return discoverable;
}
static void enqueueWork(Context context, Intent work) {
enqueueWork(context, NetworkCheckService.class, jobId, work);
}
public void onHandleWork(@NonNull Intent intent) {
Logger.d(discoverable); // discoverable is null
Discoverable foo = getDiscoverable(); // foo is null
}
}
我的主要activityclass:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NetworkCheckService networkCheckService = new NetworkCheckService(this);
networkCheckService.enqueueWork(this, new Intent());
}
}
我们不应该在服务中创建任何构造函数class。它由 android 系统完成。这就是您的成员变量变为 null
的原因。最好在 onHandleWork 方法中初始化它们。