java - android套接字在模拟器上工作,但在我的真实设备上不工作
问题描述
这是我的源代码的一部分,它可以在模拟器上完美运行,但不能在真机上运行。有一些答案说这是关于服务器的防火墙设置。请问如何设置允许android数据通过的适当防火墙?
解决方案
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);
}
}
推荐阅读
- java - 从 if 语句中打印多行到控制台
- c# - RemoveEventHandler 在 IText7 中不起作用
- airflow - Apache 气流的端到端管道测试
- python - 将我从网页中提取的元素按列分开,并使用 python 在 csv 中打印它们
- sql - 寻找类似 Lag 函数但更复杂的东西 (SQL)
- apollo-federation - 当实体是另一个实体的本机时,为什么我会在一个服务中看到 _entities 请求?
- ios - 只能通过wifi连接到CloudKit的公共容器,不能通过蜂窝连接
- reactjs - 嵌套的 React 路由不显示内容
- android - firebase 实时数据库事务和多位置写入在一起?
- javascript - ajax jQuery在浏览器页面刷新时自动重新加载数据