How to set "context" of an capacitor plugin to use UsageStatsManager


I’m trying to write a capacitor plugin following this explanation. The Plugin should make use of Androids usageStatsManager.

With the code further below, I’m getting the following error in Android Studio:

error: cannot find symbol
UsageStatsManager usm = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
symbol: variable context
location: class echoPlugin

So it seems somehow I’m not using context correctly.

My code in

package com.echo.capacitor;

import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import android.content.Context;
import android.util.Log;

import java.util.Calendar;
import java.util.List;

@CapacitorPlugin(name = "echo")
public class echoPlugin extends Plugin {    

    public void getStats(Context call) {
        Log.i("Method: ", "getStats");           

        UsageStatsManager usm = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
        long now  = Calendar.getInstance().getTimeInMillis();       
        List<UsageStats> stats = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, 0, now);

         JSObject ret = new JSObject();
         ret.put("my Stats",stats);



I have to add that I’m a complete Java beginner. I’m normally doing Apps with Vue+Ionic+Capacitor, but because there is no implementation of the UsageStatsManager in Capacitor I’m trying to find a way to make it work.


Use getContext() instead of context

Answered By – jcesarmobile

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published