Java 服务中的EditText值
我正在构建一个应用程序,在其中我使用一个服务来创建一个后台进程。但我有个问题。 我使用EditText,用户在其中输入URL,然后我将在服务中使用该URL,每X分钟检查一次与网站的连接。但是如果用户关闭应用程序,后台进程就会崩溃,因为EditText的值变为Null,而logcat会给我Null指针异常。我将值从活动传递到服务,并使用下面的意图MainActivity代码:Java 服务中的EditText值,java,android,android-intent,service,Java,Android,Android Intent,Service,我正在构建一个应用程序,在其中我使用一个服务来创建一个后台进程。但我有个问题。 我使用EditText,用户在其中输入URL,然后我将在服务中使用该URL,每X分钟检查一次与网站的连接。但是如果用户关闭应用程序,后台进程就会崩溃,因为EditText的值变为Null,而logcat会给我Null指针异常。我将值从活动传递到服务,并使用下面的意图MainActivity代码: public void onClickReadWebPage(View v) { if(add
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);
}
这是服务代码:
@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
}
}
有什么建议吗?只需将
ediText
的值保存在SharedReferences
中,然后在服务中检索它们
- .看
- 也
这不是你的问题(目前)-但看起来确实错了:如果(report==“false”)谢谢,SharedReferences可能是存储URL的正确方法。我知道,但我想尝试一下。那我就做。:)…那很好,谢谢