快速设置在Android N中切换

Ran*_*ndy 3 android android-7.0-nougat

我正在尝试在Android N中添加一个快速设置切换到我的应用程序.快速图块显示,但点击时它不会执行任何操作.触摸时我可以看到可见的反馈,所以我知道它正在识别点击,但点击时它没有做任何事情.

这是我的服务代码:

public class QuickSettingTileService extends TileService {

    public QuickSettingTileService()
    {
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startID)
    {
        //some setup code is here

        return START_STICKY;
    }

    @Override
    public void onClick()
    {
        Context context = getApplicationContext();

        Toast.makeText(context, "Quick Setting Clicked", Toast.LENGTH_SHORT).show();
        //Toggle code is here
    }
}
Run Code Online (Sandbox Code Playgroud)

我的清单几乎直接从文档中复制了代码.只做了一些修改:

<service
    android:name=".QuickSettingTileService"
    android:label="@string/app_name"
    android:icon="@drawable/quick_toggle_off"
    android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
    <intent-filter>
        <action android:name="android.service.quicksettings.action.QS_TILE" />
    </intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)

该服务在打开应用程序时启动:

Intent serviceIntent = new Intent(this, QuickSettingTileService.class);
startService(serviceIntent);
Run Code Online (Sandbox Code Playgroud)

mur*_*rki 5

只需从QuickSettingTileService类中删除这些行

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

@Override
public int onStartCommand(Intent intent, int flags, int startID)
{
    //some setup code is here
    return START_STICKY;
}
Run Code Online (Sandbox Code Playgroud)

无需在TileService上覆盖onBind()或onStartCommand().

此外,您不需要显式启动此服务.清单中的权限和意图过滤器将确保在您的磁贴添加到通知栏时Android OS将启动您的服务.