4

我正在尝试在我的 android 应用程序中运行 startService(),但它不起作用。

以下是启动服务的调用代码:

    Intent mPositioningIntent = new Intent(this, MyGeoloqiPositioning.class);
    stopService(mPositioningIntent);
    startService(mPositioningIntent);

这是来自 MyGeoloqiPositioning.java 的代码(注意,这是对 MapAttack 的源代码进行了少量修改)

package com.example.manhunttwopointoh;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.util.Pair;
import android.widget.Toast;

import com.example.manhunttwopointoh.MyFix;
import com.example.manhunttwopointoh.MyGeoloqiFixSocket;
import com.example.manhunttwopointoh.MyUDPClient;

public class MyGeoloqiPositioning extends Service implements LocationListener {
    public static final String TAG = "GeoloqiPositioning";

    private int batteryLevel = 0;

    MyGeoloqiFixSocket fixSocket;

    @Override
    public void onCreate() {
        android.os.Debug.waitForDebugger();
        Toast.makeText(MyGeoloqiPositioning.this, "MyGeoloqiPositiong in onCreate()", Toast.LENGTH_LONG).show();
        if (isConnected()) {
            fixSocket = MyUDPClient.getApplicationClient(this);
        } else {
            // TODO: This is a crude check. Should probably be rolled into UDPClient class directly.
            Log.w(TAG, "Network unavailable! Stopping positioning service.");
            stopSelf();
        }
    }

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

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(MyGeoloqiPositioning.this, "MyGeoloqiPositiong in onStart()", Toast.LENGTH_LONG).show();
        registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        for (String provider : ((LocationManager) getSystemService(LOCATION_SERVICE)).getAllProviders()) {
            if (!provider.equals("passive")) {
                MyADB.log("Registering for updates with " + provider);
                ((LocationManager) getSystemService(LOCATION_SERVICE)).requestLocationUpdates(provider, 0, 0, this);
            }
        }
    }

    public void onStop() {
        unregisterReceiver(batteryReceiver);
        ((LocationManager) getSystemService(LOCATION_SERVICE)).removeUpdates(this);
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(batteryReceiver);
        ((LocationManager) getSystemService(LOCATION_SERVICE)).removeUpdates(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startid) {
        Toast.makeText(MyGeoloqiPositioning.this, "MyGeoloqiPositiong in onStartCommand()", Toast.LENGTH_LONG).show();
        onStart(intent, startid);
        return Service.START_REDELIVER_INTENT;
    }

    @Override
    public void onLocationChanged(Location location) {
        Toast.makeText(MyGeoloqiPositioning.this, "MyGeoloqiPositiong in onLocationChanged()", Toast.LENGTH_LONG).show();
        @SuppressWarnings("unchecked")
        MyFix lqLocation = new MyFix(location, new Pair<String, String>("battery", "" + batteryLevel));

        if (isConnected()) {
            fixSocket.pushFix(lqLocation);
        } else {
            // TODO: This is a crude check. Should probably be rolled into UDPClient class directly.
            Log.w(TAG, "Network unavailable, failed to push location fix!");
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    BroadcastReceiver batteryReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            batteryLevel = intent.getIntExtra("level", 0);
        }
    };

    /** Determine if the network is connected and available. */
    private boolean isConnected() {
        ConnectivityManager manager = (ConnectivityManager) getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = manager.getActiveNetworkInfo();

        return (activeNetwork != null && activeNetwork.isConnected());
    }
}

我尝试在 MyGeoloqiPositioning 中插入断点,但没有任何结果。我还打了各种 Toast() 电话,但仍然没有骰子。我的 GeoloqiPositioning.java 永远不会被调用。我究竟做错了什么?

编辑:

这是清单文件中的新代码:

<service
        android:name="com.manhunttwopointoh.MyGeoloqiPositioning"
        android:enabled="true"
        android:exported="false"
        android:process=":lqRemote" >
        <intent-filter>
            <action android:name="com.manhunttwopointoh.MyGeoloqiPositioning" />
        </intent-filter>
    </service>

我尝试添加意图过滤器标签,但仍然没有。我也没有在日志上注册任何内容。这是代码(注释掉 stopService()):

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_geoloqi);

        Intent mPositioningIntent = new Intent(this, MyGeoloqiPositioning.class);
        //stopService(mPositioningIntent);
        startService(mPositioningIntent);
        Toast.makeText(PreyGeoloqi.this, "testing mPositioningIntent: " + mPositioningIntent.toString(), Toast.LENGTH_LONG).show();
 }

我仍然一无所获。我需要显式调用 MyPreyGeoloqi.java 中的方法吗?我相当困惑...

4

1 回答 1

4

尝试在 AndroidManifest 中为您的服务定义添加一个意图过滤器:

<!-- snippet from Android Manifest file -->
<service android:name="com.manhunttwopointoh.MyGeoloqiPositioning" android:enabled="true" android:process=":lqRemote" >
  <intent-filter>
        <action android:name="com.manhunttwopointoh.MyGeoloqiPositioning" />
    </intent-filter>
</service>

这允许您的服务接收从您的活动发送的意图。

于 2013-04-30T05:56:45.627 回答