java - 服务中的 EditText 值

标签 java android android-intent service

我正在构建一个应用程序,我在其中使用服务来创建后台进程。但我有一个问题。 我在用户输入 URL 的位置使用 EditText,然后我将在服务中使用该 URL 每隔 X 分钟检查一次网站连接。但是如果用户关闭应用程序,后台进程崩溃,因为 EditText 的值变为 Null,logcat 给我空指针异常。我将值从 Activity 传递到服务,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
        }
    }

有什么建议吗?

最佳答案

只需将 ediText 的值保存在 SharedPreferences 中,然后在您的 service 中检索它们

关于java - 服务中的 EditText 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25134503/

相关文章:

java - Spring Boot + REST 应用程序出现 "No message available"错误

java - Android Google map Api 在放大时不会取消聚类

android - 如何仅从 DCIM 目录获取图像选择器

android - 应该怎么做? (安卓)

java - 根据Java中的天数获取日期

Java邮件 : Searching for emails

java - 如何在 SE 应用程序中引导 weld-osgi 版本 2

android - 在 RecyclerView 中的一行内单击处理按钮

android - 在自定义 ListView 上调用 notifyDatasetChanged() 后继续关注 TextView?

java - 如何在android中使用intent传递数组列表