所以我试图在BroadcastReceiver调用的服务中获取当前位置。当我检查服务中是否给出位置权限时,它总是失败。我想要获取当前的GPS位置。
这是BroadcastReceiver
public class AlarmReceiver extends WakefulBroadcastReceiver{
private static final String TAG = AlarmReceiver.class.getSimpleName();
// The app's AlarmManager, which provides access to the system alarm services.
private AlarmManager alarmMgr;
// The pending intent that is triggered when the alarm fires.
private PendingIntent alarmIntent;
@Override
public void onReceive(Context context, Intent intent) {
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
context.startService(new Intent(context, AlarmService.class));
}
/**
* Cancels the alarm.
* @param context
*/
public void cancelAlarm(Context context) {
// If the alarm has been set, cancel it.
if (alarmMgr!= null) {
alarmMgr.cancel(alarmIntent);
}
}
}
这是服务
public class AlarmService extends Service {
private static final String TAG = AlarmService.class.getSimpleName();
public AlarmService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Realm realm = Realm.getDefaultInstance();
RealmResults<Trip> trips = realm.where(Trip.class).findAll();
for (Trip trip : trips) {
if (active(trip)) {
getStamp(trip);
}
}
return START_STICKY;
}
public void getStamp(Trip trip) {
Log.d(TAG, "Getting Stamp");
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
return;
}
Log.d(TAG, "Location found");
//get and store current location using GPS only
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.d(TAG, location.toString());
}
public boolean active(Trip trip){
Date now = new Date();
if(now.compareTo(trip.getStart()) > 0 && now.compareTo(trip.getEnd()) < 0){
return true;
}
return false;
}
}
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapplication.app.myapplication">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<application
android:name=".ApplicationSingleton"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/NoActionBar">
<activity android:name=".StartTravelActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TripsViewActivity" />
<activity android:name=".TripDetailActivity" />
<receiver android:name=".alarms.AlarmReceiver" />
<receiver
android:name=".alarms.BootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".alarms.AlarmReceiver" />
<service
android:name=".alarms.AlarmService"
android:enabled="true"
android:exported="true"></service>
</application>
</manifest>