以下代码在棉花糖上工作正常,但从7.0开始无效。
我创建了Brocast接收器,每6秒获取一次位置。
MainActivity.java
private PendingIntent getPendingIntent() {
Intent intent = new Intent(this, LocationUpdatesBroadcastReceiver.class);
intent.setAction(LocationUpdatesBroadcastReceiver.ACTION_PROCESS_UPDATES);
return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public void requestLocationUpdates(View view) {
try {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, getPendingIntent());
} catch (SecurityException e) {
Utils.setRequestingLocationUpdates(this, false);
Log.e("requestLocationUpdates"," "+e.toString());
e.printStackTrace();
}
}
LocationUpdatesBroadcastReceiver.class
这是我的BrocadcastReceiver类
public class LocationUpdatesBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "LUBroadcastReceiver";
static final String ACTION_PROCESS_UPDATES =
"com.google.android.gms.location.sample.locationupdatespendingintent.action" +
".PROCESS_UPDATES";
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
final String action = intent.getAction();
Log.d("showthepackgesss","***** "+action +" \n"+ACTION_PROCESS_UPDATES );
if (ACTION_PROCESS_UPDATES.equals(action)) {
LocationResult result = LocationResult.extractResult(intent);
if (result != null) {
List<Location> locations = result.getLocations();
Utils.setLocationUpdatesResult(context, locations);
Utils.sendNotification(context, Utils.getLocationResultTitle(context, locations));
Log.i(TAG, Utils.getLocationUpdatesResult(context));
}
}
}else {
Toast.makeText(context,"No Intent found checkit",Toast.LENGTH_SHORT).show();
}
}
}
我的Manifestfile.xml
这是我的清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cbdc.locationupdates_background">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
<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=".LocationUpdatesIntentService"
android:exported="false"></service>
<receiver android:name=".LocationUpdatesBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.example.cbdc.locationupdates_background.LocationUpdatesBroadcastReceiver.ACTION_PROCESS_UPDATES" />
</intent-filter>
</receiver>
</application>
</manifest>
如果有人知道请帮助我.....提前致谢!!!!!!
答案 0 :(得分:0)
您需要添加对API 23中引入的运行时权限的支持。