GeofenceTransitionsIntentService'没有默认构造函数

问题描述 投票:0回答:1

我正在使用地理围栏。我编写了服务类GeofenceTransitionsIntentService,它显示红色标记并且说没有默认构造函数。请帮我。

它是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
     <!--optional (needed if default theme has no action bar) -->
    <activity android:name=".loginpage"></activity>
    <activity android:name=".Userlogin" />
    <activity android:name=".Register" />
    <activity android:name=".MainActivity">

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyB9DNBzfrYiTcmUThheWGNdAKY3lRU3pi8" />


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

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

    </activity>

    <service android:name=".GeofenceTransitionsIntentService"/> // here's the error. it turned up red.


</application>

这是我的java类。它有一个构造函数但不是默认构造函数,如果我手动放置一个构造函数,它会显示错误。

package com.example.foodtag;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingEvent;

import java.util.ArrayList;
import java.util.List;

public class GeofenceTransitionsIntentService extends IntentService {
    GeofencingEvent geofencingEvent;
/**
 * Creates an IntentService.  Invoked by your subclass's constructor.
 *
 * @param name Used to name the worker thread, important only for debugging.
 */
public GeofenceTransitionsIntentService(String name) { //here is the constructor but it is not default constructor.
    super(name);
}

GeofenceTransitionsIntentService(){ // and if i create constructor without parameters, it is showing an error "There is no default constructor available in 'android.app.IntentService'"

}


@Override
protected void onHandleIntent( Intent intent) {  

    geofencingEvent = GeofencingEvent.fromIntent(intent);
    if(geofencingEvent.hasError()){
        Toast.makeText(this, "Event has error", Toast.LENGTH_SHORT).show();
    return;
    }

    int geoFenceTransition = geofencingEvent.getGeofenceTransition();

    if (geoFenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER){

        List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
      // String geofenceTransitionDetails = getGeofenceTransitionDetails(this, geoFenceTransition,triggeringGeofences);

      //  sendNotofication();

    }else {
        Toast.makeText(this, "geofence transition invalid", Toast.LENGTH_SHORT).show();        }
}

private  String getGeofenceTransitionDetails(
        Context context,
        int geofenceTransition,
        List<Geofence> triggeringGeofences){
   // String geofenceTransitionString = getTransitionString(geofenceTransition);//getTransitionString(geoFenceTransition)

    ArrayList triggeringgeofencesIdsList = new ArrayList();
    for (Geofence geofence : triggeringGeofences){
        triggeringgeofencesIdsList.add(geofence.getRequestId());
    }

    String triggeringgeofencesIdsString = TextUtils.join(",",triggeringgeofencesIdsList);

    return  triggeringgeofencesIdsString;
}

}

geofencing
1个回答
0
投票

创建一个默认构造函数(不带参数)并将super放在类名中。

 public GeofenceTransitionsIntentService(){
    super("GeofenceTransitionsIntentService"); //add this to avoid the error.
}
© www.soinside.com 2019 - 2024. All rights reserved.