【问题标题】:Tizen app vibration backgroundTizen 应用振动背景
【发布时间】:2018-04-30 02:48:08
【问题描述】:

我有一台三星 Gear S,我正忙于编写一个网络应用程序。当我从服务器收到消息时,我想振动我的应用程序。我只是使用navigator.vibrate(1000),这很好用。但是当我的应用程序进入后台时,振动不再起作用。我发现这个线程(https://developer.tizen.org/ko/forums/web-application-development/vibration-background?langswitch=ko)他们面临同样的问题,但没有解决方案。有人还建议使用警报使您的应用程序处于活动状态,但我不知道该怎么做。我想是这样的:

var alarm = new tizen.AlarmRelative(1);
var service = navigator.vibrate(1000);
tizen.alarm.add(alarm, "org.tizen.browser", service);

这会失败,因为服务不正确。我怎样才能让它工作?

【问题讨论】:

    标签: tizen tizen-wearable-sdk tizen-web-app samsung-gear


    【解决方案1】:

    第一种方法:

    您可以先打开屏幕灯然后振动。

    tizen.power.turnScreenOn();
    navigator.vibrate(1000);
    

    这很棘手,但很有效。

    第二种方法:

    1. 创建一个从服务器接收数据的服务应用程序

    2. 创建需要启动的 UI 应用程序。

    3. 在指向服务应用的 UI 应用中进行引用

    4. 当 Service 接收到来自服务器的数据时,它将振动并午餐 UI 应用程序。

    在服务应用中,

    var alarm = new tizen.AlarmRelative(10);
    var service = new tizen.ApplicationService("http://tizen.org/appsvc/operation/view","http://www.tizen.org");
    tizen.alarm.add(alarm, "org.your.app.id", service);
    console.log("Alarm added with id: " + alarm.id);
    
    1. 如果您需要将数据从 Service 应用发送到 UI 应用,请使用消息端口。

    【讨论】:

    • 但我不想制作第二个应用程序。服务器会不时发送一条消息,我只想显示一个弹出窗口。不幸的是,第一种方法不起作用。有没有其他办法?
    • 你能详细解释一下你的应用吗?您提到的服务器,它是单独的应用程序吗?如果是,那么应用程序如何在后台接收消息?我想你已经知道应用程序在前台时振动手机的方式。在后台的情况下,您需要服务应用程序。
    • 我有一个球衣网络服务器,我从我的 Gear S 网络应用程序发送传感器数据。这在后台工作。当发现异常时,服务器将发回一条消息,该消息也可以在后台运行。当我收到这条消息时,我可以在应用程序处于后台时点亮屏幕,但我不能让它振动。如何做到这一点?
    【解决方案2】:

    以下方法:

    您可以先打开屏幕灯然后振动。

    tizen.power.turnScreenOn();
    navigator.vibrate(1000);
    

    还没有完全起作用。我不得不为振动添加延迟:

    tizen.power.turnScreenOn();
    setTimeout(function (){
        navigator.vibrate(1000);
    }, 500);
    

    【讨论】:

      【解决方案3】:

      我认为背景振动在网络应用程序中是“非官方”支持的,但您可以激活它。 只需将以下键添加到您的 config.xml:

      <tizen:setting background-support="enable" background-vibration="enable" />
      

      即使屏幕关闭,您也可以振动。 (另请参阅tizen-2.3-wrt-core-spec.pdf 中的第 12 页)

      到目前为止,这应该可行,但我认为您无法将具有此设置的应用上传到 Tizen 商店...

      [编辑]

      今天我玩了一点振动,只是想添加我的发现。 如果您不使用 'background-vibration="enable" ' 键,您似乎有两个选择,但两者都更像是一种解决方法:

      1. 只需使用通知 (Notification) 这将推送一个 通知到手表的通知区域,包括 振动选项
      2. 报警的另一种方式是另一种方式,但它需要一个 不仅仅是警报。

      您的应用需要一些条件才能从后台振动:

      • 屏幕需要打开
      • 您的应用需要在前台

      因此,对于第二种方式,您需要做更多的事情。我使用向导(TAU 基本)创建了一个新项目,并按如下方式对 app.js 进行了修改:

      var myAlarm = null;
      var page = document.querySelector('#main');
      var remoteMsgPort = null;
      
      ( function () {
          window.addEventListener( 'tizenhwkey', function( ev ) {
      		if( ev.keyName === "back" ) {
      			var page = document.getElementsByClassName( 'ui-page-active' )[0],
      				pageid = page ? page.id : "";
      			if( pageid === "main" ) {
      				try {
      					// cleanup 
      					showAfterWakeup(false);
      					tizen.power.unsetScreenStateChangeListener();
      					tizen.application.getCurrentApplication().exit();
      				} catch (ignore) {
      				}
      			} else {
      				window.history.back();
      			}
      		}
      	} );
      
      	/**
      	 * Method to set the app as topmost app. This causes the app to be shown
      	 * after display turns on, instead of the clock face.
      	 * 
      	 * @param {boolena}enable
      	 *            True to enable, false to disable
      	 */
      	function showAfterWakeup(enable) {
      		var param = [{
      			key: "state",
      			value: enable ? "show" : "hide"
      		}];
      		
      		if(remoteMsgPort !== null) {
      			remoteMsgPort.sendMessage(param);
      			console.debug("remoteMsgPort.sendMessage(" + JSON.stringify(param) + ")");
      		}
      	}
      	
      	function checkLaunchRequest() {
      		var appControl,
      			appOperation,
      			ret = false;
      		
      		try {
      			appControl = tizen.application.getCurrentApplication().getRequestedAppControl().appControl;
      			appOperation = appControl.operation;
      			console.debug("checkLaunchRequest operation: " + appOperation);
      			
      			// check if operation view was used, as we set this for the alarm
      			if (appOperation.indexOf('http://tizen.org/appcontrol/operation/view') !== -1) {
      				console.debug("URI: " + JSON.stringify(appControl.uri));
      				// set as topmost app to be in foreground when screen turns on
      				showAfterWakeup(true);
      				// turn the screen on
      				tizen.power.turnScreenOn();
      				ret = true;
      			}
      		} catch (err) {
      			console.error("checkLaunchRequest Invalid launch request: " + err);
      		}
      	}
      	
      	function onScreenStateChanged(previousState, changedState) {
      		console.log("Screen state changed from " + previousState + " to " + changedState);
      		
      		if(previousState === "SCREEN_OFF" && changedState !== "SCREEN_OFF" ){
      			console.log("screen changed to ON");
      			navigator.vibrate([250,100,350]);
      			showAfterWakeup(false);
      		}else if(previousState !== "SCREEN_OFF" && changedState === "SCREEN_OFF" ){
      			// Triggers an alarm on a given date/time --> 10sec from now
      			var date = new Date();
      			date.setSeconds(date.getSeconds() + 10);
      			
      			// check if already a alarm was set and remove it to avoid multiple alarms
      			if(myAlarm !== null){
      				tizen.alarm.remove(myAlarm);
      				myAlarm = null;
      			}
      			
      			myAlarm = new tizen.AlarmAbsolute(date);
      			var appControl = new tizen.ApplicationControl("http://tizen.org/appcontrol/operation/view");
      			tizen.alarm.add(myAlarm, tizen.application.getCurrentApplication().appInfo.id, appControl);
      			console.log("Alarm added set for: " + date.toLocaleTimeString() + " with id: " + myAlarm.id);
      			
      		}
      	}
      	
      	/**
      	 * Callback for pageshow event
      	 */
      	function onPageShow(){
      		console.log("pageshow");
      
      		/*
      		 * check if alarm called app
      		 * if false we interpret as the first app start, not clean but will do it for demo ;)
      		 */
      		if(checkLaunchRequest() !== true){
      			// just for testing vibration
      			var text = document.querySelector('.ui-listview');
      			text.addEventListener('click', function(ev) {
      				console.log("clicked: " + ev.target.nodeName);
      				navigator.vibrate(500);
      			});
      		}
      	}
      
      	/**
      	 * init view
      	 */
      	function init(){
      		try {
      			remoteMsgPort = tizen.messageport.requestRemoteMessagePort('starter', 'Home.Reserved.Display');
      		} catch (err) {
      			console.error("Exception for requestRemoteMessagePort: " + err);
      		}
      		showAfterWakeup(false);
      		page.addEventListener('pageshow', onPageShow);
      
      		// Sets the screen state change listener.
      		tizen.power.setScreenStateChangeListener(onScreenStateChanged);
      	}
      	
      	window.onload = init();
      } () );

      那么这里做了什么:

      1. 在 onload 事件中我们注册了一个远程消息端口 (MessagePort) 并为“页面显示”添加一些事件监听器和 屏幕状态的变化。
      2. 应用程序启动后,我们等到应用程序进入后台和屏幕 关闭
      3. 在 onScreenStateChanged 我们得到屏幕关闭 我们添加了一个新的警报,它将在 10 秒内触发。
      4. 警报会触发应用程序和 pageShow 事件,以便 将调用 checkLaunchRequest。
      5. 应用程序控制操作是 .../view 因此应用程序设置为最顶层 应用程序和屏幕已打开
      6. 现在我们再次处于 onScreenStateChanged 中,但这次看到的是 屏幕已打开并振动

      我知道这似乎只是振动过载,但我认为这是在屏幕关闭后让振动 100% 起作用的唯一方法。 在https://www.w3.org/TR/2014/WD-vibration-20140211/ 他们描述如果应用程序不可见,它不应该振动,所以我认为这就是为什么屏幕需要打开并且应用程序需要在前台。 不知何故,在我看来,如果应用程序在后台无法振动,这似乎是合乎逻辑的......

      我希望这会有所帮助;)

      【讨论】:

        【解决方案4】:

        您可以使用闹钟。

        var appId = tizen.application.getCurrentApplication().appInfo.id; 
        var alarm = new tizen.AlarmRelative(delay);
        console.log('Setting relative alarm to ' + delay + ' seconds.');
        tizen.alarm.add(alarm, appId);
        

        当您的应用从服务器接收信息时添加此代码片段。

        【讨论】:

          【解决方案5】:

          这对我有用。

          在您的 config.xml 中找到 application.lauch 参数 ID。

          请参阅下面的示例以及所需的权限和设置。

          config.xml:

          <tizen:application id="SHrC13kzHD.Alarm" package="SHrC13kzHD" required_version="2.3.2"/>
          <tizen:privilege name="http://tizen.org/privilege/power"/>
          <tizen:privilege name="http://tizen.org/privilege/internet"/>
          <tizen:privilege name="http://tizen.org/privilege/notification"/>
          <tizen:privilege name="http://tizen.org/privilege/application.launch"/>
          <tizen:setting background-support="enable" encryption="disable" hwkey-event="enable"/>
          
          function multiVibration() {
            tizen.power.turnScreenOn();   
            tizen.application.launch("SHrC13kzHD.Alarm", onsuccess);
            function onsuccess() {
              console.log("Application launched successfully");
            }
            /* Vibrate SOS */
            setTimeout(function (){  navigator.vibrate([100,30,100,30,100,200,200,30,200,30,200,200,100,30,100,30,100]);
              }, 500);
          }
          
          //test
          multiVibration();
          

          【讨论】:

            猜你喜欢
            • 2017-04-11
            • 2020-10-01
            • 2020-08-02
            • 1970-01-01
            • 1970-01-01
            • 2018-02-07
            • 2019-10-22
            • 2019-10-30
            • 1970-01-01
            相关资源
            最近更新 更多