0

我正在我的应用程序中创建一项服务。这是我使用的代码。

public class LocationService extends Service{

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

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    Log.e("Location Service", "onStrat of service");
}

@Override
public void onCreate() {
    super.onCreate();
    Log.e("Location Service", "onCreate of service");
    LocationOperations locationOperations = new LocationOperations(getBaseContext());
    locationOperations.retrieveLocation();
}

}

也在清单文件中的应用程序标签内定义

<service android:name=".LocationService"
         android:process=":LocationService">
    </service>

在 Activity 的 onCreate 方法中调用此 Service

Context context = this;
    Intent service = new Intent(context, LocationService.class);
    service.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startService(service);

但是这个服务没有启动。我在 LogCat 窗口中看不到任何日志消息请帮助

4

2 回答 2

0
        ServiceConnection serviceConnection = new ServiceConnection() {

                @Override
                public void onServiceDisconnected(ComponentName name) {
                                //do stuff when service disconnected
                }

                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                                //do stuff when service connected
                }
            };


context.bindService(new Intent(context, LocationService.class), serviceConnection ,
                    Context.BIND_AUTO_CREATE);

希望这有帮助

于 2013-02-13T07:54:42.453 回答
0

onStart() 已弃用,而是重写OnStartCommand(),这应该是调用 startActivity() 时调用的函数。

另外,我不确定您为什么将Intent.FLAG_ACTIVITY_NEW_TASK标志添加到您的意图中。我认为这里不需要它。

于 2013-02-13T09:47:55.347 回答