0

我想要一个没有 Binder 的简单服务。只需在启动时启动服务,不会破坏服务。我已经用位置经理建立了一个服务。但是服务没有启动。有什么问题?

public class WayService extends Service implements LocationListener {

blabla
...

    public WayService(Context context) {
        this.mContext = context;
        getLocation();
    }

    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                // First get location from Network Provider
                if (isNetworkEnabled) { //! hinzugefuegt
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                                time = location.getTime();
                                speed = location.getSpeed();
                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }

blabla
...

@Override
    public void onLocationChanged(Location location) {
        double latitude = getLatitude();
        double longitude = getLongitude();
        String mlat = String.valueOf(latitude);
        String mlon = String.valueOf(longitude);
        new PostData().execute(mlat, mlon, mtime);
    }

清单看起来像这样:

 <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".WayService"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
        </service>


        <receiver android:name="WayStartServiceReceiver" >
        </receiver>

    </application>

和:

public class WayStartServiceReceiver extends BroadcastReceiver {

      @Override
      public void onReceive(Context context, Intent intent) {
        Intent service = new Intent(context, WayService.class);
        context.startService(service);
      }
    }

这是我的日志文件:

01-09 13:35:29.317: D/dalvikvm(452): newInstance failed: no <init>()
01-09 13:35:29.337: D/AndroidRuntime(452): Shutting down VM
01-09 13:35:29.337: W/dalvikvm(452): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-09 13:35:29.367: E/AndroidRuntime(452): FATAL EXCEPTION: main
01-09 13:35:29.367: E/AndroidRuntime(452): java.lang.RuntimeException: Unable to instantiate service de.app.trackmyway.WayService: java.lang.InstantiationException: de.app.trackmyway.WayService
01-09 13:35:29.367: E/AndroidRuntime(452):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:1929)
01-09 13:35:29.367: E/AndroidRuntime(452):  at android.app.ActivityThread.access$2500(ActivityThread.java:117)
01-09 13:35:29.367: E/AndroidRuntime(452):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
01-09 13:35:29.367: E/AndroidRuntime(452):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 13:35:29.367: E/AndroidRuntime(452):  at android.os.Looper.loop(Looper.java:123)
01-09 13:35:29.367: E/AndroidRuntime(452):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-09 13:35:29.367: E/AndroidRuntime(452):  at java.lang.reflect.Method.invokeNative(Native Method)
01-09 13:35:29.367: E/AndroidRuntime(452):  at java.lang.reflect.Method.invoke(Method.java:507)
01-09 13:35:29.367: E/AndroidRuntime(452):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-09 13:35:29.367: E/AndroidRuntime(452):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-09 13:35:29.367: E/AndroidRuntime(452):  at dalvik.system.NativeStart.main(Native Method)
01-09 13:35:29.367: E/AndroidRuntime(452): Caused by: java.lang.InstantiationException: de.app.trackmyway.WayService
01-09 13:35:29.367: E/AndroidRuntime(452):  at java.lang.Class.newInstanceImpl(Native Method)
01-09 13:35:29.367: E/AndroidRuntime(452):  at java.lang.Class.newInstance(Class.java:1409)
01-09 13:35:29.367: E/AndroidRuntime(452):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:1926)
01-09 13:35:29.367: E/AndroidRuntime(452):  ... 10 more
4

2 回答 2

0

您需要注册意图过滤器,它更像是告诉接收器它需要收听哪个广播

  <receiver android:name="WayStartServiceReceiver" >

           <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
</receiver>

此外,应用程序应安装在内部存储器上。

编辑:

https://groups.google.com/forum/?fromgroups=#!topic/android-beginners/B_lTmXcfo6A

而不是使用构造函数,你应该做一些这样的事情

@Override
public void onCreate() {
    // Task to be done on first service start
    super.onCreate();
}

@Override
public void onStart(Intent intent, int startId) {
    // Task to be done on each intent received.
    super.onStart(intent, startId);
}
于 2013-01-04T20:24:44.587 回答
0

我希望我们不能在 Android 服务中创建构造函数。就像我们不能在 Activity 中创建构造函数一样。我们将有我们的默认覆盖方法如下。

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

否则尝试在服务类中使用oncreate 。on-create调用方法getLocation(); 如果您尝试使用 oncreate 方法,它肯定会起作用。你不会有任何问题。

于 2013-01-09T13:03:36.400 回答