2017-05-18 73 views
0

这是我的清单java.lang.ClassCastException:android.app.ReceiverRestrictedContext不能转换到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> 

在我关闭GPS的时候,我得到崩溃。与此行=> 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; 
        } 
       } 
      }); 

     } 
    }  
} 

回答

1

问题:你想投广播接收器的情况下在这里活动长话短说:

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

哪项是错误的,因为这是两个不相关的对象。

解决方案:由于该方法确实需要Activity作为参数,为什么不在该处传递活动。 我想你的应用程序并至少有一个活动(如果没有,你可以创建一个),所以只要传递状态,它在演员:

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

然后在其onCreate()方法,你可以unparcel自己的地位和做分辨率:

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

status.startResolutionForResult(this, 1000); 
相关问题