android 套接字在模拟器上工作,但不在我的真实设备上

时间:2021-08-02 00:26:31

标签: java android sockets

这是我的源代码的一部分,它在模拟器上完美运行,但在真实手机上却无法运行。有一些答案说它与服务器的防火墙设置有关。请问如何设置合适的防火墙让android数据通过?

1 个答案:

答案 0 :(得分:0)

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.IBinder;
import android.os.Looper;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.security.Provider;

public class LocationService extends Service {

    public class sendLocation extends AsyncTask<String,Void,Void>{

//        public static final String SERVER_IP_ADDRESS = "10.50.xx.xx";
        public static final String SERVER_IP_ADDRESS = "10.152.xx.xx";
        public static final int PORT = 8000;
        /**
         * send the data to the server
         * @param strings
         * @return
         */

        @Override
        protected Void doInBackground(String... strings) {
            String location_info = strings[0];
            try {
                Socket socket = new Socket(SERVER_IP_ADDRESS,PORT);
//                    Toast.makeText(LocationService.this, "Connected to the Server", Toast.LENGTH_SHORT).show();
                PrintWriter pw = new PrintWriter(socket.getOutputStream());
                pw.write(location_info);
                pw.flush();
                pw.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    private LocationCallback locationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(@NonNull LocationResult locationResult) {
            super.onLocationResult(locationResult);
            if (locationResult != null && locationResult.getLastLocation() != null){
                double latitude = locationResult.getLastLocation().getLatitude();
                double longitude = locationResult.getLastLocation().getLongitude();
//                Log.d("LOCATION_UPDATE",latitude + " , " + longitude);
                String androidId = Settings.Secure.getString(getContentResolver(),
                        Settings.Secure.ANDROID_ID);
                Toast.makeText(LocationService.this, "Device ID:" + androidId + " \nLOCATION_UPDATE" + latitude + " , " + longitude, Toast.LENGTH_SHORT).show();

                new sendLocation().execute("Device ID:" + androidId + " ; Latitude:" + latitude + " ; Longitude:" + longitude);
            }

        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent){
        throw  new UnsupportedOperationException("Not implement yet!");
    }

    private void startLocationService(){
        String channelId = "location_notification_channel";
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Intent resultIntent = new Intent();
        PendingIntent pendingIntent = PendingIntent.getActivity(
                getApplicationContext(),
                0,
                resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
        );
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                getApplicationContext(),
                channelId
        );
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("Location Service");
        builder.setDefaults(NotificationCompat.DEFAULT_ALL);
        builder.setContentText("Running");
        builder.setContentIntent(pendingIntent);
        builder.setAutoCancel(false);
        builder.setPriority(NotificationCompat.PRIORITY_MAX);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            if (notificationManager != null
                    && notificationManager.getNotificationChannel(channelId) == null){
                NotificationChannel notificationChannel = new NotificationChannel(
                        channelId,
                        "Location Service",
                        NotificationManager.IMPORTANCE_HIGH
                );
                notificationChannel.setDescription("This channel is used by location service");
                notificationManager.createNotificationChannel(notificationChannel);
            }

        }

        LocationRequest locationRequest = new LocationRequest();
        locationRequest.setInterval(4000);
        locationRequest.setFastestInterval(2000);
        locationRequest.setPriority(locationRequest.PRIORITY_HIGH_ACCURACY);

        LocationServices.getFusedLocationProviderClient(this)
                .requestLocationUpdates(locationRequest,locationCallback, Looper.getMainLooper());
        startForeground(Constants.LOCATION_SERVICE_ID,builder.build());
    }

    private void stopLocationService(){
        LocationServices.getFusedLocationProviderClient(this)
                .removeLocationUpdates(locationCallback);
        stopForeground(true);
        stopSelf();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        if (intent != null){
            String action = intent.getAction();
            if (action != null){
                if (action.equals(Constants.ACTION_START_LOCATION_SERVICE)){
                    startLocationService();
                } else if (action.equals(Constants.ACTION_STOP_LOCATION_SERVICE)){
                    stopLocationService();
                }
            }
        }
        return  super.onStartCommand(intent,flags,startId);
    }
}