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();
}

}

applicatoin タグ内のマニフェスト ファイルにも定義します。

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

アクティビティの onCreate メソッドでこのサービスを呼び出す

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

onStart()は非推奨であり、代わりにOnStartCommand()をオーバーライドします。これは、startActivity()を呼び出すときに呼び出される関数である必要があります。

Intent.FLAG_ACTIVITY_NEW_TASKまた、意図にフラグを追加した理由がわかりません。ここでは必要ないと思います。

于 2013-02-13T09:47:55.347 に答える
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 に答える