Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未从Android Geofence事件接收到意图_Java_Android_Android Intent_Geofencing_Android Geofence - Fatal编程技术网

Java 未从Android Geofence事件接收到意图

Java 未从Android Geofence事件接收到意图,java,android,android-intent,geofencing,android-geofence,Java,Android,Android Intent,Geofencing,Android Geofence,所有内容都正确构建并在模拟器中运行,但我似乎无法让IntentService记录任何内容。我肯定有一些基本的东西我遗漏了或忽略了,但我对Android/Java非常陌生,目前已经没有什么想法了 public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { private

所有内容都正确构建并在模拟器中运行,但我似乎无法让IntentService记录任何内容。我肯定有一些基本的东西我遗漏了或忽略了,但我对Android/Java非常陌生,目前已经没有什么想法了

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private Geofence geofence;
private PendingIntent mGeofencePendingIntent;
private GoogleApiClient mGoogleApiClient;

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

    LatLng latLng = new LatLng(39.7492350, -104.9913250);

    geofence = new Geofence.Builder()
            .setRequestId(GEOFENCE_REQ_ID)
            .setCircularRegion(latLng.latitude, latLng.longitude, GEOFENCE_RADIUS_IN_METERS)
            .setExpirationDuration(GEOFENCE_EXPIRATION)
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();
}

private boolean checkPermission() {
    Log.i(TAG, "MainActivity.checkPermission()");
    return (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
}

private GeofencingRequest getGeofencingRequest() {
    Log.i(TAG, "MainActivity.getGeofencingRequest()");
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
    builder.addGeofence(geofence);
    return builder.build();
}

private PendingIntent getGeofencePendingIntent() {
    Log.i(TAG, "MainActivity.getGeofencePendingIntent()");
    if (null != mGeofencePendingIntent) {
        return mGeofencePendingIntent;
    }

    Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

@Override
public void onConnected(Bundle connectionHint) {
    Log.i(TAG, "MainActivity.onConnected()");
    if (checkPermission()) {
        mGeofencePendingIntent = getGeofencePendingIntent();
        LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, getGeofencingRequest(), mGeofencePendingIntent);
    } else {
        Log.i(TAG, "Permission not granted");
    }
}

@Override
public void onConnectionSuspended(int i) {
    Log.i(TAG, "MainActivity.onConnectionSuspended()");
    if (null != mGeofencePendingIntent) {
        LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, mGeofencePendingIntent);
    }
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.i(TAG, "MainActivity.onConnectionFailed()");
}}
每当我使用emulator或android控制台更新设备lat/lng时,以下服务不会收到任何意图:

public class GeofenceTransitionsIntentService extends IntentService {

public GeofenceTransitionsIntentService() {
    super(GeofenceTransitionsIntentService.class.getSimpleName());
}

@Override
protected void onHandleIntent(Intent intent) {
    Log.i(TAG, "GeofenceTransitionsIntentService.onHandleIntent()");

    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        int errorCode = geofencingEvent.getErrorCode();
        Log.e(TAG, "Location Services error: " + errorCode);
    } else {
        Log.i(TAG, "geofencingEvent was successful");
    }
}}
舱单:

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



和。

位置服务在模拟器中似乎功能不全。一旦我插入一个真正的设备,一切都按预期进行。

我有几件事你没有澄清:1-你在真正的设备上测试过吗?2-您是否在规定的lat/lng内?3-是否安装了Google Play服务?4-您是否尝试过打开谷歌地图,然后更新lat/lng?有时这是让GPS在模拟器上工作的唯一方法。请在真实设备上运行应用程序,然后告诉我。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

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

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

    <service android:name=".GeofenceTransitionsIntentService" />
</application>