0

對於我的Android應用程序,我添加了一個AppTile。大多數時候,這個AppTile 是不可點擊的。它的圖標是灰色的,如禁用或不可圖標(https://developer.android.com/reference/android/service/quicksettings/Tile.html#STATE_UNAVAILABLEAndroid AppTileService onClick不會被調用

在我的日誌此行從未達到

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

但我測試過該方法onStartListening()onStopListening( )每當AppTile變爲可見或隱藏時調用

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> 

有人能告訴我這可能是什麼,或者是否已經忘記了什麼來實現?

回答

1

您需要通過兩種方式之一來指示該貼圖處於活動狀態。

首先,您的<service>可能有<meta-data android:name="android.service.quicksettings.ACTIVE_TILE" android:value="true" />

其次,在onStartListening(),您可以更新平鋪狀態是積極的,因爲我從this book做在this sample appupdateTile()方法的手段:

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(); 
    } 
    } 
+0

謝謝,我已經加入你的建議,並必須刪除平鋪一次並重新添加到快速設置。現在它像一個魅力 – Larcado