I'm building an application in which I use a service to create a background process. but I have a problem. I use an EditText where the user enters a URL, then I will use that url in the service for check the connection to a website every X minutes. But if the user closes the application, the background process crash because the value of the EditText became Null, and logcat give me null pointer exceptions. I pass the value from the activity to the service with an intent, 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?