【问题标题】:run startService in static method在静态方法中运行 startService
【发布时间】:2015-04-25 11:37:11
【问题描述】:

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

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

但是 startService 返回跟随错误:

无法对非静态方法进行静态引用 startService(Intent) 来自 ContextWrapper 类型

有什么方法可以在静态方法中启动服务吗?

【问题讨论】:

标签: java android android-intent service


【解决方案1】:

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

context.startService(intent)

【讨论】:

    猜你喜欢
    • 2018-09-26
    • 2014-01-09
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    相关资源
    最近更新 更多