2016-05-31 7 views
0

私は一定のGPSステータスをONにする必要があるアプリを持っています。アプリ内で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する1定数varibleを宣言しています。 GpsLocationReceiverではGPSのチェンジをオンまたはオフにしますが、オンまたはオフの値を取得しないので、静的なブール変数をモニタしてオフにしなければならない場合は、下のコードでDialog to 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

あなたはその定常変数をどこで宣言しましたか?完全なコードを投稿してください –

+0

はい、全体のコードを投稿してください – notTdar

+0

公共の静的なブールのGPS_STATUSのようなMainactivtyのDelcare静的変数Receiverの値を割り当てます –

関連する問題