EditText value in service

I am creating an application where I am using a service to create a background process. but i have a problem. I am using EditText where the user enters a URL, then I will use that URL in the service to check the website connection every X minutes. But if the user closes the app, the background process crashes due to the EditText value becoming Null and logcat is giving me null pointer exceptions. I am passing value from activity to service with intent, with MainActivity code below:

public void onClickReadWebPage(View v)
    {


        if(address.getText().toString().trim().length() == 0)
        {
            Toast.makeText(getApplicationContext(), "URL String empty.", Toast.LENGTH_LONG).show();
        }
        else
        {
            time = String.valueOf(address.getText());
            i=new Intent(MainActivity.this,MyService.class);
            p=PendingIntent.getService(MainActivity.this, 0, i, 0);
            i.putExtra("Address", time);                
            //start the service from here //MyService is your service class name
            startService(i);
        }

      

and this is the service code:

    @Override
    public void onStart(Intent intent, int startId) 
    {   
        Address = intent.getStringExtra("Address");
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(Address);

        if(report == "false")
        {
            //do my stuff
        }
        else
        {
            //do my stuff
        }
    }

      

Any suggestion?

+3


source to share


1 answer


Just store the value ediText

in SharedPreferences

and then put them back in yourservice



0


source







All Articles