2017-05-18 1 views
0

Ceci est mon manifestejava.lang.ClassCastException: android.app.ReceiverRestrictedContext ne peut pas être jeté à android.app.Activity

<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> 

Au moment où je ferme le GPS, je reçois crash. avec cette ligne => status.startResolutionForResult ((Activity) ctx, 1000);

mes classes que j'appelle cette méthode (CheckAndOpenGps) étend 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; 
        } 
       } 
      }); 

     } 
    }  
} 

Répondre

1

Le problème: longue histoire courte que vous essayez de jeter le contexte BroadcastReceiver à l'activité ici:

status.startResolutionForResult((Activity)ctx , 1000); 

Ce qui est faux, puisque ce sont deux objets indépendants.

Une solution: étant donné que le procédé nécessite l'activité comme paramètre, pourquoi ne pas y passer l'activité. Je suppose que votre application ne au moins une activité (sinon, vous pouvez en créer un), donc il suffit de passer le statut en extras:

ctx.startActivity(new Intent(ctx, YourActivityName.class).putExtra("status-to-resolve", status)); 

Et puis dans sa méthode onCreate() vous pouvez unparcel votre statut et faites la résolution:

Status status = (Status) getIntent().getParcelableExtra("status-to-resolve"); 

status.startResolutionForResult(this, 1000);