服务中的EditText值

时间:2014-08-05 08:40:20

标签: java android android-intent service

我正在构建一个应用程序,我在其中使用服务来创建后台进程。但我有一个问题。 我使用EditText,用户输入URL,然后我将在服务中使用该URL每隔X分钟检查一次网站的连接。但是如果用户关闭应用程序,后台进程崩溃,因为EditText的值变为Null,而logcat给我空指针异常。我将活动中的值传递给服务,其意图为MainActivity代码:

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
        }
    }

有什么建议吗?

1 个答案:

答案 0 :(得分:0)