[英]How to keep internet connection alive for a long time on android?
We are creating internal app which needs to have unkillable or very long living websocket connection.我们正在创建内部应用程序,它需要具有无法终止或非常长寿命的 websocket 连接。 How can I achieve that?
我怎样才能做到这一点? I have it working in foreground service, disabled battery optimization etc. but it doesnt seem to work.
我让它在前台服务、禁用电池优化等方面工作,但它似乎不起作用。 I tried all standard options.
我尝试了所有标准选项。
Command adb shell dumpsys deviceidle whitelist +PACKAGE
doesnt seem to help either.命令
adb shell dumpsys deviceidle whitelist +PACKAGE
似乎也没有帮助。
Would rooting phone and setting app as system app help?将手机生根并将应用程序设置为系统应用程序有帮助吗? Are there enterprise Custom ROMs which give control over battery optimizations and other stuff?
是否有企业自定义 ROM 可以控制电池优化和其他内容?
You should use a Foreground Service with a partial wakelock, in order to prevent the phone from sleeping.您应该使用带有部分唤醒锁的前台服务,以防止手机休眠。 Here below an example of Foreground Service Class that implements a Wakelock:
以下是实施唤醒锁的前台服务 Class 的示例:
public class ICService extends Service {
private static final int ID = 1; // The id of the notification
private NotificationCompat.Builder builder;
private NotificationManager mNotificationManager;
private PowerManager.WakeLock wakeLock; // PARTIAL_WAKELOCK
/**
* Returns the instance of the service
*/
public class LocalBinder extends Binder {
public ICService getServiceInstance(){
return ICService.this;
}
}
private final IBinder mBinder = new LocalBinder(); // IBinder
@Override
public void onCreate() {
super.onCreate();
// PARTIAL_WAKELOCK
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"INSERT_YOUR_APP_NAME:wakelock");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
startForeground(ID, getNotification());
return START_NOT_STICKY;
}
@SuppressLint("WakelockTimeout")
@Override
public IBinder onBind(Intent intent) {
if (wakeLock != null && !wakeLock.isHeld()) {
wakeLock.acquire();
}
return mBinder;
}
@Override
public void onDestroy() {
// PARTIAL_WAKELOCK
if (wakeLock != null && wakeLock.isHeld()) {
wakeLock.release();
}
super.onDestroy();
}
private Notification getNotification() {
final String CHANNEL_ID = "YOUR_SERVICE_CHANNEL";
builder = new NotificationCompat.Builder(this, CHANNEL_ID);
//builder.setSmallIcon(R.drawable.ic_notification_24dp)
builder.setSmallIcon(R.mipmap.YOUR_RESOURCE_ICON)
.setColor(getResources().getColor(R.color.colorPrimaryLight))
.setContentTitle(getString(R.string.app_name))
.setShowWhen(false)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setOngoing(true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentText(composeContentText());
final Intent startIntent = new Intent(getApplicationContext(), ICActivity.class);
startIntent.setAction(Intent.ACTION_MAIN);
startIntent.addCategory(Intent.CATEGORY_LAUNCHER);
startIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 1, startIntent, 0);
builder.setContentIntent(contentIntent);
return builder.build();
}
}
Obviously you still have also to disable battery optimization for your app.显然,您还必须为您的应用禁用电池优化。 You can browse a real working example of this service (but used for GPS) here .
您可以在此处浏览此服务(但用于 GPS)的真实工作示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.