TileService无法正常工作,图块变得无响应

时间:2018-12-08 09:20:19

标签: android service altbeacon foreground-service

我制作了一个tileService,当单击它时会触发前台服务。我已从设置中获取了电池优化许可,并将其设置为“不优化”。但是,三天后,磁贴变得无响应。

public class QSIntentService
    extends TileService {

String TAG = getClass().getSimpleName();
Context context;
boolean isTileActive;


@Override
public void onStartListening() {
    super.onStartListening();
    Log.e(TAG, "-----onStartListening Tile--");
    if (isServiceRunning(getApplicationContext())) {
        isTileActive = true;
        updateTile();
    } else {
        isTileActive = false;
        updateTile();
    }
}

@Override
public void onClick() {
    context = getApplicationContext();
    Log.e(TAG, "-----onClick --- ");

    Tile tile = getQsTile();
    isTileActive = (tile.getState() == Tile.STATE_ACTIVE);

    Intent serviceIntent = new Intent(context, 
ForegroundService.class);
    if (isServiceRunning(context)) {
        isTileActive = false;
        Utils.setBooleanFromPref(context, 
context.getString(R.string.widgetStop), true);
        if (ShareIBeaconInfo.INSTANCE.getForegroundService() != null) {
            ForegroundService foregroundService = 
ShareIBeaconInfo.INSTANCE.getForegroundService();
            if (foregroundService.scanDevice != null) {
                foregroundService.stopForegroundService();
            }
        }

        context.stopService(serviceIntent);
    } else {
        isTileActive = true;
        Utils.setBooleanFromPref(context, 
context.getString(R.string.widgetStop), false);
        Log.e(TAG, "service is not running");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Intent service = new Intent(context, 
ForegroundService.class);
            ContextCompat.startForegroundService(context, service);
        } else {
            Intent service = new Intent(context, ForegroundService.class);
            context.startService(service);
        }
    }
    updateTile();
}


private void updateTile() {
    Tile tile = super.getQsTile();
    int activeState = isTileActive ?
            Tile.STATE_ACTIVE : Tile.STATE_INACTIVE;

    tile.setState(activeState);
    tile.updateTile();
}

private boolean isServiceRunning(Context context) {
    try {
        final ActivityManager manager;
        manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager
                .getRunningServices(Integer.MAX_VALUE)) {
            if ("com.foregroundscan.service.ForegroundService"
                    .equalsIgnoreCase(service.service.getClassName())) {
                return true;
            }
        }
    } catch (NullPointerException ex) {
    } catch (Exception e) {
    }
    return false;
}
}

我的测试设备是在Android 9上运行的OnePlus 6T。如果图块无响应,是否还有办法推送对话?这样我才能重新启动服务?

当我单击一个没有响应的磁贴时,我收到了一条日志

2-08 16:34:56.710 1831-2154/? E/MdmLogger: Cannot get tag from tileTag 
: Tile.CustomTile

0 个答案:

没有答案