question

jackhf avatar image
jackhf asked

Keep app running in the background forever

I am still new to Android development, but I’m creating an app that sends an HTTP request when it receives a broadcast (from a barcode scan). I have tried to implement some other solutions that I read on here but haven't quite figured it out yet. It would be great if someone could help me get going in the right direction.


Essentially, the end goal is for the app to keep running in the background forever so that even if the app is not open and a barcode is scanned and sent to the app via broadcast signal, the HTTP request is still sent.


This is what I have so far:


MainActivity.java

public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener {
    public static final String BARCODE_BROADCAST = "...";
    private final BarcodeReceiver barcodeReceiver = new BarcodeReceiver();
    private TextView mTextView;
    private String scannedBarcode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextView = (TextView) findViewById(R.id.barcode);
    }

    @Override
    public void onResume() {
        super.onResume();
        registerBarcodeScanner();
    }

    @Override
    public void onPause() {
        super.onPause();
        unregisterBarcodeScanner();
        // this code needs to keep the app running
        Intent restartService = new Intent("RestartService");
        this.startService(restartService);
        sendBroadcast(restartService);
    }

    public void onDestroy() {
        super.onDestroy();
        // this code needs to keep the app running
        Intent restartService = new Intent("RestartService");
        this.startService(restartService);
        sendBroadcast(restartService);
    }

    private void registerBarcodeScanner() {
        registerReceiver(barcodeReceiver, new IntentFilter(BARCODE_BROADCAST));
    }

    private void unregisterBarcodeScanner() {
        unregisterReceiver(barcodeReceiver);
    }

    private void displayBarcode() {
        if (scannedBarcode == null) return;

        String text = getString(R.string.barcode_scanned, scannedBarcode);
        mTextView.setText(text);
        
        /* SEND HTTP REQUEST */
    }

    private class BarcodeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(BARCODE_BROADCAST)) {
                String barcode = intent.getStringExtra("Barcode");
                if (barcode != null) {
                    scannedBarcode = barcode;
                    displayBarcode();
                }
            }
        }
    }
}


RestartService.java

public class RestartService extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        context.startService(new Intent(context, MainActivity.class));
    }
}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="..."
    android:versionCode="1">

    <uses-sdk android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        tools:replace="android:label">
        <activity
            android:name="...MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name="...RestartService"
            android:enabled="true"
            android:exported="true"
            android:label="RestartServiceWhenStopped"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="RestartService" />
            </intent-filter>
        </receiver>
    </application>

</manifest>
Clover Android SDKbarcode scannerBroadcasts
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

0 Answers

·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Welcome to the
Clover Developer Community