Trying to process different scenarios in Service

xsonz

I am having a problem and I ran out of ideas how to fix it.

The goal is, when the user clicks the button an URL is loaded depending on what's selected in settings.

Problem is, I am having trouble setting it up in a right way.

Logically(to me), I tried to set it up in a service. Button is clicked > Service starts > URL is loaded from "IF ELSE". Problem is, I get an error in "IF ELSE" - "Method length must be called from the UI Thread, currently inferred thread is is worker.

public static class Service extends IntentService {
    public Service() {
        super("wallpaperchanger-download");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        MainActivity mainActivity;
        mainActivity = new MainActivity();

        if (mainActivity.mEditTextHashtag.length() > 2) {

            WallpaperManager wm = WallpaperManager.getInstance(this);
            int height = wm.getDesiredMinimumHeight();
            int width = wm.getDesiredMinimumWidth();

            String url = "https://source.unsplash.com/all/?" + mainActivity.mEditTextHashtag.getText() + "/" + width + "x" + height + "/";
            try {
                InputStream input = new URL(url).openStream();
                Log.v(TAG, url);
                wm.setStream(input);
                input.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            loading = false;
        }

    }
}

Ok, fair enough. I created new Method getPhoto(); in UI Thread and put the code in there. Then, I called mainActivity.getPhoto(); in Service. Problem is, I get an error - "Attempt to invoke virtual method 'int android.widget.EditText.length()' on a null object reference"

Any ideas on what I should do?

Full code in all its glory:

package com.edip.splashwallpaper;

import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.app.WallpaperManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.Toast;

import java.io.InputStream;
import java.net.URL;

public class MainActivity extends android.app.Activity {

    final static String TAG = "AllInOne";
    final static int CHANGE_INTERVAL = 30 * 1000; //30 sec for testing
    static boolean loading = false;
    WallpaperManager wm;

    //Layout Views
    Switch mSwitchFixedPhoto, mSwitchControls, mSwitchSave, mSwitchPause;
    Spinner mSpinnerCategories, mSpinnerInterval;
    EditText mEditTextHashtag;
    Button mWallpaperButton;


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

        //Layout Views Initialized
        mSwitchFixedPhoto = (Switch) findViewById(R.id.sw_fixedphoto);
        mSwitchControls = (Switch) findViewById(R.id.switch_controls);
        mSwitchSave = (Switch) findViewById(R.id.switch_save);
        mSwitchPause = (Switch) findViewById(R.id.switch_pause);
        mSpinnerCategories = (Spinner) findViewById(R.id.spinner_categories);
        mSpinnerInterval = (Spinner) findViewById(R.id.spinner_interval);
        mEditTextHashtag = (EditText) findViewById(R.id.et_hashtag);
        mWallpaperButton = (Button) findViewById(R.id.btn_set_wallpaper);


        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapterCategory = ArrayAdapter.createFromResource(this,
                R.array.categories_array, R.layout.dialog_spinner_layout);
        // Specify the layout to use when the list of choices appears
        adapterCategory.setDropDownViewResource(R.layout.dialog_spinner_layout);
        // Apply the adapter to the spinner
        mSpinnerCategories.setAdapter(adapterCategory);

        ArrayAdapter<CharSequence> adapterInterval = ArrayAdapter.createFromResource(this,
                R.array.interval_array, R.layout.dialog_spinner_layout);
        adapterInterval.setDropDownViewResource(R.layout.dialog_spinner_layout);
        mSpinnerInterval.setAdapter(adapterInterval);

        mWallpaperButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                PendingIntent pending = PendingIntent.getBroadcast(MainActivity.this,
                        666, new Intent("com.edip.splashwallpaper.CHANGE_WALLPAPTER_TIMER"),
                        PendingIntent.FLAG_CANCEL_CURRENT);

                ((AlarmManager) getSystemService(Context.ALARM_SERVICE))
                        .setRepeating(AlarmManager.RTC, System.currentTimeMillis(),
                                CHANGE_INTERVAL, pending);

            }
        });

    }

    public void getPhoto() {

        if (mEditTextHashtag.length() > 2) {

            wm = WallpaperManager.getInstance(this);
            int height = wm.getDesiredMinimumHeight();
            int width = wm.getDesiredMinimumWidth();

            String url = "https://source.unsplash.com/all/?" + mEditTextHashtag.getText() + "/" + width + "x" + height + "/";
            try {
                InputStream input = new URL(url).openStream();
                Log.v(TAG, url);
                wm.setStream(input);
                input.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            loading = false;

        } else {

            Toast.makeText(this, "Something else", Toast.LENGTH_SHORT).show();

        }

    }

    public static class Service extends IntentService {
        public Service() {
            super("wallpaperchanger-download");
        }

        @Override
        protected void onHandleIntent(Intent intent) {
            MainActivity mainActivity;
            mainActivity = new MainActivity();

            mainActivity.getPhoto();
        }
    }

    public static class AlarmReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(final Context context, Intent intent) {
            if (!loading) {
                loading = true;
                context.startService(new Intent(context, Service.class));
            }
        }
    }
}

Thanks :)

XGouchet

First of all, you should never instantiate an activity by yourself.

Second, as a best practice, your service shouldn't know about your activity, or that it has an edit text. Instead you should send the URL to load inside your intent, when the PendingIntent is created, like this :

Intent intent  = new Intent("com.edip.splashwallpaper.CHANGE_WALLPAPTER_TIMER");
intent.putExtra("USER_URL", "https://source.unsplash.com/all/?" + mEditTextHashtag.getText() + "/" + width + "x" + height + "/");
PendingIntent pending = PendingIntent.getBroadcast(MainActivity.this,
                    666, intent, PendingIntent.FLAG_CANCEL_CURRENT);

Then within your service, read the url like so :

@Override 
protected void onHandleIntent(Intent intent) {
    String url = intent.getStringExtra("USER_URL");
    // ... 
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Linux: process into a service

From Dev

Trying to pass variables to child process

From Dev

What are the Advantages and Disadvantages of running a service in different process?

From Dev

Configuring state of mocked object for different scenarios

From Dev

CUDA atomic operation performance in different scenarios

From Dev

trying to make an Android service

From Dev

Android service in separate process

From Dev

How to handle Service unavailable scenarios with Jersey REST

From Dev

Difference between a process and service?

From Dev

Why WCF service able to process more calls from different processes than from thread

From Dev

Same use case, different actors, slightly different scenarios

From Dev

Laravel is trying to process asset requests

From Dev

Communication between Service and Activity each hosted in different process using PendingIntent

From Dev

How to Communicate between Activity and Service using LocalBroadcastManager in a different Process

From Dev

Sort List of JSONObjects on different scenarios

From Dev

Configuring state of mocked object for different scenarios

From Dev

Trying to hide a process

From Dev

trying to make an Android service

From Dev

Restful - same request but difference response on different scenarios

From Dev

Poodle and Websphere ESB / Process Server trying to call an external TLS service

From Dev

How to kill a service process?

From Dev

How to process selections from different providers via the Eclipse Selection Service while maintaining loose coupling?

From Dev

Where the process of the service?

From Dev

Produce different serialized JSON for a given class in different scenarios

From Dev

Testing Twilio app with multiple requests and different scenarios

From Dev

How to run Music Service in different process

From Dev

How to get the Employee records in different scenarios

From Dev

Apply different test scenarios without duplicating code

From Dev

Scenarios definition for JWT/API Process with Cucumber/Behat

Related Related

  1. 1

    Linux: process into a service

  2. 2

    Trying to pass variables to child process

  3. 3

    What are the Advantages and Disadvantages of running a service in different process?

  4. 4

    Configuring state of mocked object for different scenarios

  5. 5

    CUDA atomic operation performance in different scenarios

  6. 6

    trying to make an Android service

  7. 7

    Android service in separate process

  8. 8

    How to handle Service unavailable scenarios with Jersey REST

  9. 9

    Difference between a process and service?

  10. 10

    Why WCF service able to process more calls from different processes than from thread

  11. 11

    Same use case, different actors, slightly different scenarios

  12. 12

    Laravel is trying to process asset requests

  13. 13

    Communication between Service and Activity each hosted in different process using PendingIntent

  14. 14

    How to Communicate between Activity and Service using LocalBroadcastManager in a different Process

  15. 15

    Sort List of JSONObjects on different scenarios

  16. 16

    Configuring state of mocked object for different scenarios

  17. 17

    Trying to hide a process

  18. 18

    trying to make an Android service

  19. 19

    Restful - same request but difference response on different scenarios

  20. 20

    Poodle and Websphere ESB / Process Server trying to call an external TLS service

  21. 21

    How to kill a service process?

  22. 22

    How to process selections from different providers via the Eclipse Selection Service while maintaining loose coupling?

  23. 23

    Where the process of the service?

  24. 24

    Produce different serialized JSON for a given class in different scenarios

  25. 25

    Testing Twilio app with multiple requests and different scenarios

  26. 26

    How to run Music Service in different process

  27. 27

    How to get the Employee records in different scenarios

  28. 28

    Apply different test scenarios without duplicating code

  29. 29

    Scenarios definition for JWT/API Process with Cucumber/Behat

HotTag

Archive