startService()没有在android中运行

时间:2013-04-30 05:15:06

标签: android debugging service

我正在尝试在我的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>

我尝试添加了intent-filter标签,但仍然没有。我也没有注册日志。这是代码(注释掉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中显式调用方法?我很困惑......

1 个答案:

答案 0 :(得分:3)

尝试在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>

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