这是我的清单
<receiver android:name="com.devfret.mobile.Fretlink.receivers.GpsLocationReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.LOCALE_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
当我关闭gps时,我会崩溃。用这一行=&gt; status.startResolutionForResult((Activity)ctx,1000);
我称之为此方法的类(CheckAndOpenGps)扩展了AppCompatActivity。
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentSender;
import android.location.LocationManager;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.Toast;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Result;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsStatusCodes;
import com.devfret.mobile.Fretlink.R;
public class GpsLocationReceiver extends BroadcastReceiver {
public GpsLocationReceiver() {
}
private GoogleApiClient googleApiClient;
public Context contex;
ContentResolver contentResolver;
// int mode = Settings.Secure.getInt(
// contentResolver, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
//String locationMode="Gps is not activated";
LocationManager locationManager;
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
// throw new UnsupportedOperationException("Not yet implemented");
this.contex = context;
contentResolver = context.getContentResolver();
locationManager = (LocationManager) contex.
getSystemService(Context.LOCATION_SERVICE);
if (!IsGpsStatusActive(locationManager)) {
Toast.makeText(context, "GPS is not working. Please Activate the GPS", Toast.LENGTH_SHORT).show();//emre changed
CheckAndOpenGps(context, googleApiClient);
}
}
public static Boolean IsGpsStatusActive(LocationManager locationManager)//e
{
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
return false;
} else
return true;
}
public static void buildAlertMessageNoGps(final Context ctx, final GoogleApiClient googleApiClient) // emre yazdi
{
final LayoutInflater mInflater = LayoutInflater.from(ctx);
final AlertDialog.Builder builder = new AlertDialog.Builder(mInflater.getContext());
final AlertDialog.Builder builderexit = new AlertDialog.Builder(mInflater.getContext());
builder.setMessage(R.string.gps_off_turn_on).setCancelable(false).setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused")
final int id) {
CheckAndOpenGps(ctx, googleApiClient);
}
}).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
builderexit.setMessage(R.string.canoot_work_without_gps).setCancelable(false).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused")
final int id) {
System.exit(0);
}
});
final AlertDialog alertexit = builderexit.create();
alertexit.show();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
public static void CheckAndOpenGps(final Context ctx, GoogleApiClient googleApiClient) {
if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(ctx).addApi(LocationServices.API).build();
googleApiClient.connect();
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
//locationRequest.setInterval(30 * 1000);//emre 30
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
com.google.android.gms.common.api.PendingResult result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
//final GoogleApiClient finalGoogleApiClient = googleApiClient;
result.setResultCallback(new ResultCallback() {
@Override
public void onResult(@NonNull Result result) {
final Status status = result.getStatus();
Log.d("statusCode", String.valueOf(status.getStatusCode()) + " message " + status.getStatusMessage());
//
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
status.startResolutionForResult( (Activity)ctx , 1000);
} catch (IntentSender.SendIntentException e) {
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
break;
}
}
});
}
}
}
答案 0 :(得分:1)
问题:长话短说你试图将BroadcastReceiver的上下文投射到Activity:
status.startResolutionForResult( (Activity)ctx , 1000);
哪个错了,因为那是两个不相关的对象。
解决方案:,因为该方法确实需要Activity作为参数,为什么不在那里传递活动。 我想你的应用程序确实至少有一个Activity(如果没有,你可以创建一个),所以只需将其状态传递给它:
ctx.startActivity(new Intent(ctx, YourActivityName.class).putExtra("status-to-resolve", status));
然后在其onCreate()
方法中,您可以取消状态并执行解决方案:
Status status = (Status) getIntent().getParcelableExtra("status-to-resolve");
status.startResolutionForResult(this, 1000);