BackgroundTasks in Android: 103

IntentSerivce and JobIntentService

Intent Service

In Comparison to AsyncTask

private WeatherFetcher weatherFetcher = WeatherFetcher.getInstance();
private ResultReceiver myResultReceiver;
ArrayList<String> quote = new ArrayList<>();

public WeatherIntentService() {
super("weather_thread");
}

@Override
protected void onHandleIntent(@Nullable Intent intent) {
try {
if(intent!=null){
myResultReceiver = intent.getParcelableExtra("result");
}

quote = weatherFetcher.makeRequest();
Log.i(TAG, "onHandleIntent: " + quote);
Bundle bundle = new Bundle();
bundle.putStringArrayList("compValue", quote);
myResultReceiver.send(100,bundle);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
resultValue.getResult(resultCode,resultData);
}

public void setReceiver(GetResultInterface result){
resultValue = result;
}

public interface GetResultInterface {
void getResult(int resultCode, Bundle resultData);
}
public class IntentServiceActivity extends AppCompatActivity implements MyResultReceiver.GetResultInterface {

private MyResultReceiver myResultReceiver;
private TextView cityTemp, cityRealFeel, cityTempMin, cityTempMax, cityHumidity;
private ArrayList<String> list = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent_service);
cityTemp = findViewById(R.id.textTemp);
cityRealFeel = findViewById(R.id.textfeels);
cityTempMin = findViewById(R.id.texttempmin);
cityTempMax = findViewById(R.id.texttempmax);
cityHumidity = findViewById(R.id.texthumadity);
myResultReceiver = new MyResultReceiver(new Handler());
myResultReceiver.setReceiver(this);
}

@Override
public void getResult(int resultCode, Bundle resultData) {
if(resultData!=null) {
switch (resultCode) {
case 100:
list = resultData.getStringArrayList("compValue");
cityTemp.setText("Temp C: " + list.get(0));
cityRealFeel.setText("Real Feel Temp C: " + list.get(1));
cityTempMin.setText("Min Temp C: " + list.get(2));
cityTempMax.setText("Max Temp C: " + list.get(3));
cityHumidity.setText("Humidity: " + list.get(4));
break;
}
}
}

public void getNumber(View view) {
Intent intent = new Intent(IntentServiceActivity.this,WeatherIntentService.class);
intent.putExtra("result",myResultReceiver);
startService(intent);
}
}

A Software Engineer and gamer at heart.