简体   繁体   中英

Android refactor URL onClick button

I want to make an android app where when the user of the app clicks a button, it should go to the IP-address of a RaspBerry PI. This Pi is located on the network itself and has a static IP, but it should be specified by the user once.

I've got a TextField where the user can put the IP-address of the Pi. This should be stored in SharedPreferences. Here's the code for this part:

public void save(View view) {
    editor.putString("homeIP", textHomeIP.getText().toString());
    editor.commit();

    editor.putString("PiIP", textPiIP.getText().toString());
    editor.commit();

    piIP = textPiIP.getText().toString();
    homeIP = textHomeIP.getText().toString();

    makeURL();

    Toast.makeText(getApplication(), "Saved", Toast.LENGTH_SHORT);

    finish();
}

This method runs when clicked on the save button. The class extends the MainActivity and is opened with the settings xml when the settings button is clicked. For some reason in the MainActivity the URL cannot be changed. Here's the method to change the URL

protected void makeURL()
{
    if(mSocket != null)
    {
        mSocket.disconnect();
    }

    try
    {
        url = new URL("http://" + piIP);
        Log.d("URL", url.toString());
    }
    catch (java.net.MalformedURLException e) {}

    try
    {
        mSocket = IO.socket(url.toString(), opts);
    }
    catch (java.net.URISyntaxException e)
    {
        Log.d("ERROR", "Cant initialize socket: " + e.toString());
    }

    mSocket.connect();
    mSocket.on("detection-start", detectionStart);
    mSocket.on("detection-end", detectionEnd);
}

The socket does actually connect but the URL stays the same for some reason. I run this method in the onCreate and initiate piIP before that.

Here's onCreate method:

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ipActivity = new IPActivity();
    ipActivity.execute();

    setContentView(R.layout.content_main);
    notificationSettings();

    //connectionChanger = new ConnectionChangeReceiver();

    sharedPref = this.getPreferences(Context.MODE_PRIVATE);
    //sharedPref = PreferenceManager.getDefaultSharedPreferences(this);

    editor = sharedPref.edit();

    homeIP = sharedPref.getString("homeIP", "localhost");
    piIP = sharedPref.getString("PiIP", "localhost");

    Log.d("Pi IP", piIP);

    makeURL();
}

And at last here's the openBrowser() method

public void openBrowser(View view)
{
    makeURL();
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url.toString()));
    startActivity(browserIntent);
}

Can someone please tell me why the URL is not changed when the settings are saved?

It turns out I was using getPreferences instead of getSharedPreferences. It fixed the problem of creating the URL. I made the subclasses classes on their own and let de ip addresses load via getSharedPreferences.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM