2016-05-31 2 views
0

앱에 일정한 GPS 상태가 켜져 있어야하는 앱이 있습니다. 앱 내부에서 내 GPS를 사용 중지했습니다. 이제 내 앱이 GPS를 다시 표시 할 수 있도록 표시하거나 표시해야합니다. 힘의 대화. 상황이 좋았 으면 좋겠어. 앱을 열 때 gps를 켜거나 끄는 것이 아니다. 코드 아래일정한 GPS 상태 켜기 또는 끄기 Android에서 확인

답변

0

은 ... AndroidManifest.xml에있는

선언 방송 수신기

public class GpsLocationReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 
    if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) { 

     Log.e("GPS", "changed" + intent); 

    } 
}} 

선언이

<receiver android:name=".GpsLocationReceiver"> 
     <intent-filter> 
      <action android:name="android.location.PROVIDERS_CHANGED" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </receiver> 

을 제기 도움이 될 수 있지만 GPS 상태를 moniter 한 일정 varible를 선언합니다. GpsLocationReceiver에서 GPS를 켜거나 끄지 만 값을 설정하거나 해제하지 않으므로 정적 부울 변수를 경고해야합니다. 그러면 코드가 아래 코드로 Open GPS 설정 대화 상자에 표시됩니다.

public void showSettingsAlert(){ 

    AlertDialog myAlertDialog; 

    AlertDialog.Builder builder = new AlertDialog.Builder(mContext); 

    // Setting Dialog Title 
    builder.setTitle("GPS settings"); 
    builder.setCancelable(false); 
    // Setting Dialog Message 
    builder.setMessage("For Use This Application You Must Need to Enable GPS. do you want to go Setting menu?"); 

    // On pressing Settings button 
    builder.setPositiveButton("Settings", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      mContext.startActivity(intent); 
     } 
    }); 

    // on pressing cancel button 
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 

     } 
    }); 

    myAlertDialog = builder.create(); 

     myAlertDialog.show(); 


} 
+1

어디에서 staic 변수를 선언 했습니까? 전체 코드를 게시하시기 바랍니다 –

+0

예, 전체 코드를 게시하십시오 – notTdar

+0

공공 정적 부울 GPS_STATUS 같은 Mainactivty Delcare 정적 변수 및 수신기 –

관련 문제