简体   繁体   中英

How to call a private class from a public class

I have the following private class :

private class sendReportePeriodico extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... params) {
        socket = null;
        SocketAddress address = new InetSocketAddress(server_address, server_port);
        socket = new Socket();
        try {
            socket.connect(address, 3000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            socket.setSoTimeout(3000);
        } catch (SocketException e) {
            e.printStackTrace();
            return "Can't Connect";
        }
        OutputStream out = null;
        try {
            out = socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        PrintWriter output = new PrintWriter(out);

        output.print(texto);
        output.flush();

        return null;
    }
}

I need to call this class inside this a public class :

 public void onLocationChanged(Location location) {
    Log.d("LogXXX","Visualizando gps");
    texto = "GPS: " + location.getLatitude() + ", " + location.getLongitude();
    tvMensaje.setText(texto);
    mapa(location.getLatitude(), location.getLongitude());
    new sendReportePeriodico();

    HERE I NEED TO CALL THE PRIVATE CLASS;
 }

How can I do it?

It does not really matter that the class you´re calling from is public. If the class "sendReportePeriodico" is not in another class then there is no way to call it.

Private classes are useless unless they´re located inside another class (In this case it is referred to as "inner class").

Even reflection would not work since private classes take an instance of the outter class as an argument using reflection)

The solution would be to make the class "sendReportePeriodico" public or protected or move the class inside the class you want to call it from.

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