I am new to android and was running into an issue I was unclear about. I have been stuck at it for a while and no research seems to help so wanted to try posting. I have an android java application and In that application I have an intentService that has a AsyncTask that is executed. The issue I am having is once I run the intent once, it works fine, but the second time the code runs through doInBackground() method, the payment connector and order connector return null. I know I am doing something wrong but can't find where as if I move the ASYNC task logic into the intentService class, the payment listener stops working.
My payment connector service class:
package com.drivensoftware.cloverServerAssistant;
import android.accounts.Account;
import android.app.IntentService;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;
import com.clover.connector.sdk.v3.PaymentConnector;
import com.clover.sdk.Lockscreen;
import com.clover.sdk.util.CloverAccount;
import com.clover.sdk.v1.BindingException;
import com.clover.sdk.v1.ClientException;
import com.clover.sdk.v1.ServiceException;
import com.clover.sdk.v3.connector.ExternalIdUtils;
import com.clover.sdk.v3.connector.IDeviceConnectorListener;
import com.clover.sdk.v3.connector.IPaymentConnectorListener;
import com.clover.sdk.v3.order.Order;
import com.clover.sdk.v3.order.OrderConnector;
import com.clover.sdk.v3.remotepay.SaleRequest;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
public class PaymentConnectorService extends IntentService {
private Account account;
private PaymentConnector paymentConnector;
private OrderConnector orderConnector;
private AsyncTask waitingTask;
private ArrayList<IDeviceConnectorListener> listeners = new ArrayList<>();
public PaymentConnectorService() {
super("PaymentConnectorService");
}
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
if (account == null) {
account = CloverAccount.getAccount(this);
if (account == null) {
Toast.makeText(this, "there is no account", Toast.LENGTH_SHORT).show();
}
}
return super.onStartCommand(intent, flags, startId);
}
private void connect(IPaymentConnectorListener cloverPaymentListener){
disconnect();
if (account != null) {
Log.i("connect()", "Account found: connecting services");
orderConnector = new OrderConnector(this, account, null);
orderConnector.connect();
initializePaymentConnector(cloverPaymentListener);
}
Log.i("connect()", "Done connecting!");
}
private void disconnect(){
Log.i("disconnect()", "Disconnecting services!");
if (orderConnector != null) {
orderConnector.disconnect();
orderConnector = null;
}
if (paymentConnector != null) {
paymentConnector.dispose();
paymentConnector = null;
}
Log.i("disconnect()", "Done disconnecting!");
}
public void addCloverConnectorListener(IDeviceConnectorListener listener) {
if (!listeners.contains(listener)) {
listeners.add(listener);
}
}
private void initializePaymentConnector(IPaymentConnectorListener ccListener) {
if (account == null) {
account = CloverAccount.getAccount(this);
if (account == null) {
Toast.makeText(this, "there is no account", Toast.LENGTH_SHORT).show();
}
}
if (paymentConnector == null) {
// Set your RAID as the remoteApplicationId
String remoteApplicationId = "SWDEFOTWBD7XT.6W3D67YDX8GN3";
paymentConnector = new PaymentConnector(this, account, ccListener, remoteApplicationId);
paymentConnector.initializeConnection();
}
}
@Override
protected void onHandleIntent(Intent intent) {
Log.i("printOrderService", "onHandleIntent: " + intent.getStringExtra("orderId"));
Toast.makeText(getApplicationContext(), "New Order Received!", Toast.LENGTH_LONG).show();
new SpecificOrderAsyncTask().execute(intent.getStringExtra("orderId"));
}
public class SpecificOrderAsyncTask extends AsyncTask<String, Void, Order> {
final IPaymentConnectorListener ccListener = new PaymentConnectorListener(PaymentConnectorService.this);
@Override
protected final Order doInBackground(String... currentOrderID) {
Order newOrder;
try {
connect(ccListener);
String orderId = currentOrderID[0];
newOrder = orderConnector.getOrder(orderId);
Log.i("SpecificOrderAsyncTask", "Order found: returning");
if (newOrder !=null){
Log.i("printOrderService", "onHandleIntent: Order found");
int retry = 0;
while (retry < 5) {
SaleRequest saleRequest = new SaleRequest();
saleRequest.setOrderId(newOrder.getId());
saleRequest.setExternalId(ExternalIdUtils.generateNewID()); //required, but can be any string
saleRequest.setAmount(newOrder.getTotal());
Lockscreen lockscreen = new Lockscreen(PaymentConnectorService.this);
lockscreen.unlock();
paymentConnector.sale(saleRequest);
retry = 5;
}
//new StaticOrderPrintJob.Builder().order(newOrder).build().print(getApplicationContext(), account);
} else{
Log.i("PaymentConnectorService", "onHandleIntent: Order not found, print not triggered.");
}
return newOrder;
} catch (RemoteException | ClientException | ServiceException | BindingException e) {
e.printStackTrace();
} finally{
Log.i("PaymentConnectorService", "onHandleIntent: Attempted to retrieve order");
}
return null;
}
@Override
protected void onPostExecute(Order order){
System.out.println("ORDER " +order);
}
@Override
protected void onPreExecute(){
}
}
public class LocalBinder extends Binder {
public PaymentConnectorService getPaymentConnectorService(){
return PaymentConnectorService.this;
}
}
@Override
public void onDestroy()
{
super.onDestroy();
disconnect();
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IBinder mBinder = new LocalBinder();
}
How I am starting the intent:
Intent intent = new Intent(context, KioskActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);