【问题标题】:Android AppTileService onClick not get calledAndroid AppTileService onClick 没有被调用
【发布时间】:2017-10-28 07:22:06
【问题描述】:

对于我的 android 应用,我添加了一个 AppTile。大多数情况下,此 AppTile 不可点击。它的图标是灰色的,例如禁用或不可用的图标(https://developer.android.com/reference/android/service/quicksettings/Tile.html#STATE_UNAVAILABLE

在我的日志中,这一行从未到达

Logger.d(getClass(), "onClick()");

但我已经测试过,每次 AppTile 变得可见或隐藏时都会调用方法 onStartListening()onStopListening()

AppTileService.java

package my.package

import android.os.Build;
import android.service.quicksettings.TileService;
import android.support.annotation.RequiresApi;

import my.package.utils.Logger;

@RequiresApi(api = Build.VERSION_CODES.N)
public class AppTileService extends TileService {

    @Override
    public void onClick() {
        Logger.d(getClass(), "onClick()");
        if (isSecure()) {
            Logger.d(getClass(), "isSecure = true");
            NotificationHandler.showDirectReplyNotification(this);
        }
    }
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="my.package"> 
   <application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <service
            android:name=".AppTileService"
            android:icon="@drawable/tile_icon"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
            <intent-filter>
                <action android:name="android.service.quicksettings.action.QS_TILE"/>
            </intent-filter>
        </service>

    </application>
</manifest>

谁能告诉我这可能是什么,或者我是否忘记了要实现的东西?

【问题讨论】:

    标签: android android-service android-7.0-nougat android-7.1-nougat


    【解决方案1】:

    您需要通过以下两种方式之一指示该磁贴处于活动状态。

    首先,您的&lt;service&gt; 可以有&lt;meta-data android:name="android.service.quicksettings.ACTIVE_TILE" android:value="true" /&gt;

    其次,在onStartListening() 中,您可以将磁贴状态更新为活动状态,就像我通过this sample app 中的updateTile() 方法从this book 一样:

      private void updateTile() {
        Tile tile=getQsTile();
    
        if (tile!=null) {
          boolean isEnabled=
            getPrefs()
              .getBoolean(SettingsFragment.PREF_ENABLED, false);
          int state=isEnabled ?
            Tile.STATE_ACTIVE :
            Tile.STATE_INACTIVE;
    
          tile.setIcon(Icon.createWithResource(this,
            R.drawable.ic_new_releases_24dp));
          tile.setLabel(getString(R.string.app_name_short));
          tile.setState(state);
          tile.updateTile();
        }
      }
    

    【讨论】:

    • 谢谢,我已经添加了您的建议,必须删除一次磁贴并重新将其添加到快速设置中。现在它就像一个魅力
    【解决方案2】:

    我想补充一点,虽然标记为解决方案的答案是一个正确的解决方案,但它不适合问题的情况,也不能真正解决所描述的问题。

    相反,解决方案需要:

    1) Tile 的默认状态是 STATE_UNAVAILABLE(值 0x00),与活动和非活动状态不同,它不响应点击事件。相反,在onTileAdded() 和/或onStartListening() 中,您应该将磁贴状态更新为活动或非活动:

     Tile tile = getQsTile();
     tile.setState(Tile.STATE_ACTIVE);
     tile.updateTile();
    

    2) super.onClick(); 应包含在您的 onClick() 实施中。

    我犯了这个错误,对为什么我的 Tile 对点击的响应不一致感到困惑。但是在跟踪调用onStartListening()onStopListening() 的时间后,如果您只是希望Tile 在用户点击后运行任务,则不应使用上述元标记。在不实现 requestListeningState() 的情况下使用它会导致一些奇怪的行为,例如 Tile 无法正确监听用户点击。

    【讨论】:

      猜你喜欢
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      • 2017-01-24
      • 1970-01-01
      • 1970-01-01
      • 2016-08-18
      • 1970-01-01
      相关资源
      最近更新 更多