Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.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
锁定时防止android服务睡眠_Android_Android Service - Fatal编程技术网

锁定时防止android服务睡眠

锁定时防止android服务睡眠,android,android-service,Android,Android Service,我正在使用cordova开发一个小应用程序,但由于webview的局限性,我需要在本机java中使用socket.io连接 我制作了一个插件,插件启动了一个服务,这个服务现在有一个定时器,使用振动API使手机每秒振动一次进行测试,下面是我的代码: public class SamplePlugin extends CordovaPlugin { private static final String TAG = "SamplePlugin"; Timer timer = new

我正在使用cordova开发一个小应用程序,但由于webview的局限性,我需要在本机java中使用socket.io连接

我制作了一个插件,插件启动了一个服务,这个服务现在有一个定时器,使用振动API使手机每秒振动一次进行测试,下面是我的代码:

public class SamplePlugin extends CordovaPlugin {

    private static final String TAG = "SamplePlugin";
    Timer timer = new Timer();
    public Context context;

    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        // your init code here


        context = cordova.getActivity().getApplicationContext();

        context.startService(new Intent(context, MyService.class));

    }
}
现在,我的服务示例:

public class MyService extends Service {

   private static final String TAG = "MyService";
   private static Timer timer = new Timer(); 
   private Context ctx;
   private PowerManager.WakeLock wl;
   private PowerManager pm;


   @Override
   public IBinder onBind(Intent arg0) {
      return null;
   }

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {

        ctx = this;

            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
         PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
         wl.acquire();

      // Let it continue running until it is stopped.
      Log.i(TAG, "START SERVICE");
      timer.scheduleAtFixedRate(new mainTask(ctx), 0, 1000);
      Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
      return START_NOT_STICKY;
   }

   @Override
   public void onDestroy() {
      super.onDestroy();
      Log.i(TAG, "STOP SERVICE");
      Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
   }

    private class mainTask extends TimerTask { 

    private Context context;

        public mainTask(Context context) {
            this.context=context;
        }

        public void run() {
            Log.i(TAG, "SERVICE TICK");
            Vibrator v = (Vibrator) getSystemService(VIBRATOR_SERVICE);
            v.vibrate(200);
        }
    }    


}
但当我从手机上拔下USB并锁定屏幕时,在大约10秒内振动停止,如果我再次激活屏幕或插入电缆,振动会返回,但如果我在10秒内锁定而没有USB或充电器,振动会停止,就像系统关闭应用程序一样

你可以尝试一些东西,获得一个唤醒锁定(尝试使用完整、部分ans屏幕),但在10秒锁定振动停止

尝试将服务置于粘滞状态,如果您从应用程序列表中关闭应用程序,则服务将运行(包括粘滞状态)!但当锁定屏幕时,大约10秒后停止

我看到的一件事是,我在插件上下文中启动服务,但我认为这不是问题所在(因为在应用程序关闭的情况下进行粘性运行)

是否可能始终在后台运行服务?这是安卓系统的局限性吗?(I基于Android 6.0的Nexus 5)


谢谢你的帮助

使用报警管理器怎么样?服务可能会被OS杀死,如果它需要更多的资源,但我不想发送通知给用户,我想让一个套接字连接到另一个TangeSalm管理器不发送通知给用户,认为他们作为计划任务,但这只允许我每一次X星服务一次,但是,如果我的服务停止,套接字关闭,我可以更改代码以允许在停止和重新启动时连接和重新连接,但是,这就像一个“补丁”,不可能让服务无限运行?如果你想保持服务活动,你应该将其定义为前台服务,但这在资源管理方面有许多缺点。关于保持插座处于活动状态,想象一下收音机将消耗多少功率