运行 静态方法中的startService

run startService in static method

我有这个函数作为静态方法:

    public static void engine(SQLiteDatabase mydb){
        Intent intent = new Intent(context, GPSLocation.class);
        intent.putExtra("sql_obj", mydb); 
        startService(intent);
    }

但是 startService return 出现错误:

Cannot make a static reference to the non-static method startService(Intent) from the type ContextWrapper

有没有静态方法启动服务的方法?

看起来您已经通过 context 变量获得了对 Context class 的引用(因为您正在使用它来创建意图 Intent intent = new Intent(context, GPSLocation.class);),所以您可以通过以下方式从静态上下文启动它:

context.startService(intent)