Java GeofenceClient.addGeofences()不';不要启动IntentService
我正在尝试使用谷歌的地理围栏,我遵循 问题是,当我调用Java GeofenceClient.addGeofences()不';不要启动IntentService,java,android,android-geofence,Java,Android,Android Geofence,我正在尝试使用谷歌的地理围栏,我遵循 问题是,当我调用addgeofines()时,它应该启动IntentService,但它只是停止了。 addgeofines()结果始终显示成功和我的日志: 然后什么也没发生, 我的IntentService的onHandleIntent()的日志没有显示任何内容;我想它甚至没有被激活 我使用nexus5api25模拟器和targetandroid7.1.1(googleapi)进行测试。 我搜索了很多,但仍然不知道为什么;也许我错过了什么 这是我的密码:
addgeofines()
时,它应该启动IntentService
,但它只是停止了。
addgeofines()
结果始终显示成功和我的日志:
然后什么也没发生,
我的IntentService的onHandleIntent()的日志没有显示任何内容;我想它甚至没有被激活
我使用nexus5api25模拟器和targetandroid7.1.1(googleapi)进行测试。
我搜索了很多,但仍然不知道为什么;也许我错过了什么
这是我的密码:
private void startGeofenceMonitor()
{
Log.i("info", "Start geofence monitoring");
try
{
int permission = ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION);
if (permission != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions( MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION} , PERMISSIONS_REQUEST_CODE);
Log.i("info", "Permission not available! ");
}
else
{
mGeofencingClient = LocationServices.getGeofencingClient(this);
geofenceList.add(new Geofence.Builder().setRequestId("point0")
.setCircularRegion(55, 55, 15)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setNotificationResponsiveness(1000)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.build());
mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
.addOnSuccessListener(this, new OnSuccessListener<Void>()
{
@Override
public void onSuccess(Void aVoid)
{
Log.i("info", "geofence added!: ");
}
})
.addOnFailureListener(new OnFailureListener()
{
@Override
public void onFailure(@NonNull Exception e)
{
Log.i("info", "geofence failed! " + e.toString());
}
});
}
}
catch (Exception e)
{
Log.i("er", e.getMessage());
}
}
private GeofencingRequest getGeofencingRequest()
{
return new GeofencingRequest.Builder()
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_DWELL)
.addGeofences(geofenceList)
.build();
}
private PendingIntent getGeofencePendingIntent()
{
if (mGeofencePendingIntent != null)
{
return mGeofencePendingIntent;
}
Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
mGeofencePendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return mGeofencePendingIntent;
}
private void startGeofenceMonitor()
{
Log.i(“信息”,“启动地理围栏监控”);
尝试
{
int permission=ActivityCompat.checkSelfPermission(MainActivity.this、android.Manifest.permission.ACCESS\u FINE\u位置);
if(权限!=PackageManager.permission\u已授予)
{
ActivityCompat.requestPermissions(MainActivity.this,新字符串[]{Manifest.permission.ACCESS\u FINE\u LOCATION},PERMISSIONS\u REQUEST\u CODE);
Log.i(“信息”,“权限不可用!”);
}
其他的
{
mGeofencingClient=LocationServices.getGeofencingClient(this);
geofineslist.add(new geofiness.Builder().setRequestId(“point0”)
.setCircularRegion(55,55,15)
.setExpirationDuration(地理围栏永不过期)
.SetNotificationResponsibility(1000)
.setTransitionTypes(geofines.geofines_TRANSITION_ENTER | geofines.geofines_TRANSITION_EXIT)
.build());
mGeofencingClient.addgeofenses(getGeofencingRequest(),getgeofencinpendingent())
.addOnSuccessListener(此,新的OnSuccessListener()
{
@凌驾
成功时公开作废(作废避免)
{
Log.i(“信息”,“添加了geofence!:”;
}
})
.addOnFailureListener(新的OnFailureListener()
{
@凌驾
public void onFailure(@NonNull异常e)
{
Log.i(“信息”,“地理围栏失败!”+e.toString());
}
});
}
}
捕获(例外e)
{
Log.i(“er”,即getMessage());
}
}
私人GeofencingRequest getGeofencingRequest()
{
返回新的GeofencingRequest.Builder()
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_驻留)
.addGeofences(geofenceList)
.build();
}
私有PendingEvent GetGeoFencePendingEvent()
{
if(mgeofencependingent!=null)
{
返回mgeofencependingent;
}
意向意向=新意向(这是GeofenceTransitionsIntentService.class);
mgeofencependingent=pendingent.getService(this,0,intent,pendingent.FLAG_UPDATE_CURRENT);
返回mgeofencependingent;
}
还有我的意向服务
public GeofenceTransitionsIntentService()
{
super("GeofenceTransitionsIntentService");
Log.i("info", "IntentService is activated!");
}
@Override
protected void onHandleIntent(Intent intent)
{
String test = intent.getStringExtra("pls");
Log.i("info", "onHandleIntent is activated!");
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
Log.i("info", "geofencingEvent size: " + intent.toString());
if (geofencingEvent.hasError())
{
Log.e("er", "geofencingEvent has error!");
return;
}
else
{
}
int geofenceTransition = geofencingEvent.getGeofenceTransition();
Log.e("er", "geofenceTransition" + geofenceTransition);
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER)
{
List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
Geofence geofence = triggeringGeofences.get(0);
String requestID = geofence.getRequestId();
Log.i("geo", "Entering geofence : " + requestID);
}
else if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT)
{
Log.i("geo", "Exiting geofence : ");
}
else
{
Log.i("geo", "geo not in range ");
}
}
publicGeoFenceTransitionsIntentService()
{
超级(“GeofenceTransitionsIntentService”);
Log.i(“信息”,“IntentService已激活!”);
}
@凌驾
受保护的手部内容无效(意图)
{
字符串测试=intent.getStringExtra(“pls”);
Log.i(“信息”,“OnHandleContent已激活!”);
GeofencingEvent GeofencingEvent=GeofencingEvent.fromIntent(intent);
Log.i(“info”,“geofencingEvent size:+intent.toString());
if(geofencingEvent.hasError())
{
Log.e(“呃”,“geofencingEvent有错误!”);
返回;
}
其他的
{
}
int geofenceTransition=geofencingEvent.getGeofenceTransition();
Log.e(“er”、“geofenceTransition”+geofenceTransition);
如果(geofenceTransition==Geofence.Geofence\u TRANSITION\u ENTER)
{
List triggeringGeofences=geofencingEvent.getTriggeringGeofences();
Geofence Geofence=triggeringGeofences.get(0);
String requestID=geofines.getRequestId();
Log.i(“地理”,“进入地理围栏:”+requestID);
}
else if(geofenceTransition==Geofence.Geofence\u TRANSITION\u EXIT)
{
Log.i(“geo”,“退出geofence:”);
}
其他的
{
Log.i(“geo”、“geo不在范围内”);
}
}
显示
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hs.geofencestest">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<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">
<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" android:exported="true"/>
</application>