2017-05-18 9 views
0

캐스트 할 수없는이 내 내가 GPS를 닫을 때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> 

매니페스트, 내가 사고를 얻을 수있다. 이 줄은 => 입니다. 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);