Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 即使从最近的应用程序中清除应用程序或在红色MI设备上关闭应用程序,也要继续服务_Java_Android Service_Redmi Device - Fatal编程技术网

Java 即使从最近的应用程序中清除应用程序或在红色MI设备上关闭应用程序,也要继续服务

Java 即使从最近的应用程序中清除应用程序或在红色MI设备上关闭应用程序,也要继续服务,java,android-service,redmi-device,Java,Android Service,Redmi Device,我正在我的应用程序中运行遥测服务。此服务每秒检查一次WiFi环境中的更改,如果检测到更改,则通知远程数据库。我需要此服务继续运行,即使该应用程序是从最近的应用程序刷卡或关闭 我跟着它,除了小米,它似乎在很多设备上都能工作。我尝试了许多建议此设备的方法: 启用自动启动 禁用该应用程序的电池优化 尝试使用广播接收器启动服务 尝试使用计时器线程和scheduleExecutorservice而不是runnable。没什么不同 这是我的最新示例代码: package com.example.csi1;

我正在我的应用程序中运行遥测服务。此服务每秒检查一次WiFi环境中的更改,如果检测到更改,则通知远程数据库。我需要此服务继续运行,即使该应用程序是从最近的应用程序刷卡或关闭

我跟着它,除了小米,它似乎在很多设备上都能工作。我尝试了许多建议此设备的方法:

  • 启用自动启动
  • 禁用该应用程序的电池优化
  • 尝试使用广播接收器启动服务
  • 尝试使用计时器线程和scheduleExecutorservice而不是runnable。没什么不同
  • 这是我的最新示例代码:

    package com.example.csi1;
    
    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.net.wifi.SupplicantState;
    import android.net.wifi.WifiInfo;
    import android.net.wifi.WifiManager;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.IBinder;
    import android.util.Log;
    import android.view.Gravity;
    import android.widget.Toast;
    
    public class MainService extends Service {
        public Context context = this;
        public Handler handler = null;
        public static Runnable runnable = null;
        public static int count = 0;
        public int restart = 0;
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            super.onStartCommand(intent, flags, startId);
            Toast.makeText(this, "MainService onStartCommand", Toast.LENGTH_LONG).show();
            Bundle extras = intent.getExtras();
            count = 0;
    
            handler = new Handler();
            runnable = new Runnable(){
                public void run() {
                    count = count+1;
    
                    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                    WifiInfo wifiInfo;
    
                    wifiInfo = wifiManager.getConnectionInfo();
                    if (wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
                        String ssid = wifiInfo.getSSID();
                        Log.i("Service", "SSID:"+ssid);
                        Toast toast = Toast.makeText(context, "Service iter " + String.valueOf(count)+ " " + ssid, Toast.LENGTH_LONG );
                        toast.setGravity(Gravity.TOP|Gravity.RIGHT, 0, 0);
                        toast.show();
                    }
    
                    handler.postDelayed(runnable, 1000);
    
                }
            };
    
            handler.postDelayed(runnable, 3000);
            return START_STICKY;
        }
    
        @Override
        public void onTaskRemoved(Intent rootIntent) {
            super.onTaskRemoved(rootIntent);
            Toast.makeText(this, "onTaskRemoved", Toast.LENGTH_LONG).show();
            if (restart == 0) {
                PendingIntent service = PendingIntent.getService(
                        getApplicationContext(),
                        1001,
                        new Intent(getApplicationContext(), MainService.class),
                        PendingIntent.FLAG_ONE_SHOT);
    
                AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, service);
                restart=1;
                Toast.makeText(this, "Service restarted", Toast.LENGTH_LONG).show();
            }
        }
        @Override
        public void onDestroy(){
            Toast.makeText(this, "onDestroy", Toast.LENGTH_SHORT).show();
            if (restart == 0) {
                PendingIntent service = PendingIntent.getService(
                        getApplicationContext(),
                        1001,
                        new Intent(getApplicationContext(), MainService.class),
                        PendingIntent.FLAG_ONE_SHOT);
    
                AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, service);
                restart=1;
                Toast.makeText(this, "Service restarted", Toast.LENGTH_LONG).show();
            }
            super.onDestroy();
        }
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            throw new UnsupportedOperationException("Not yet implemented");
        }
    }
    

    它在三星设备和谷歌nexus emulator上运行良好,但每当我在Red mi上运行它并从最近的应用程序中刷取应用程序时,应用程序就会死掉并使用它提供服务。是否有针对红色mi设备的解决方案,或者这是一些众所周知的限制?

    小米的MIUI和其他一些人在删除后台服务方面非常积极。作为一个用户,您可以在下面禁用它。但作为一名开发人员,我不知道有什么解决方案可以绕过它


    所以你没有做错什么,有些手机只是不喜欢后台服务占用电池寿命。类似的讨论可在此处找到:。他们的解决方案基本上是让用户通过为小米的MIUI和其他几个非常积极地删除后台服务的MIUI调用优化管理器来禁用它。作为一个用户,您可以在下面禁用它。但作为一名开发人员,我不知道有什么解决方案可以绕过它


    所以你没有做错什么,有些手机只是不喜欢后台服务占用电池寿命。类似的讨论可在此处找到:。他们的解决方案基本上是让用户通过为每个

    Thank you Software person调用优化管理器来禁用它。我已经完成了这些步骤,但服务仍然会被终止,所以我知道操作系统不会让我重新启动它?一般来说是的,但也许可以查看以下内容:。看看这是否有帮助。谢谢软件人。我已经完成了这些步骤,但服务仍然会被终止,所以我知道操作系统不会让我重新启动它?一般来说是的,但也许可以查看以下内容:。看看这是否有帮助。