2016-04-20 21 views
0
BroadcastReceiver updateBooleanBroadCastReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      // TODO Auto-generated method stub 
      if (intent.getBooleanExtra("action", true)) { 
       checkGPS(); 
        } else { 
       checkGPS(); 

       } 
     } 
    }; 

private void checkGPS(){ 
     locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,1.0f, this); 
     boolean isGPS = locationManager.isProviderEnabled (LocationManager.GPS_PROVIDER); 
     if(isGPS){ 

     }else{ 

      Utils.displayPromptForEnablingGPS(true,MainActivity.this); 

     } 
    } 

接收器接收這是我的提醒:如何顯示和解僱alertDilog在全球範圍內時,曾經在安卓

public static void 
    displayPromptForEnablingGPS(boolean a, 
           final Activity activity) { 
     final AlertDialog.Builder builder = 
       new AlertDialog.Builder(activity); 
     final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS; 
     final String message = "Enable either GPS or any other location" 
       + " service to find current location. Click OK to go to" 
       + " location services settings to let you do so."; 

     builder.setMessage(message).setCancelable(false) 
       .setPositiveButton("OK", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface d, int id) { 
           activity.startActivityForResult(new Intent(action),1); 
           d.dismiss(); 
          } 
         }) 
       .setNegativeButton("Cancel", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface d, int id) { 

           activity.finish(); 

          } 
         }); 


      builder.create().show(); 

    } 

我想隱藏Alertdilog時位置在,當我想隱藏或關閉時的位置關閉通知管理員位置我可以在我們關閉時收到接收方法的事件,並且在位置管理者位置時,當我離開位置管理員的位置時,我可以顯示警報但我無法禁用警報當我在位置PLZ幫助我時我將如何實現這一點,請建議。

回答

0

可以在onrecieve方法來顯示你的對話框

private void displayAlert() 
{ 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage("Are you sure you want to exit?").setCancelable(
     false).setPositiveButton("Yes", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel(); 
      } 
     }).setNegativeButton("No", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel(); 
      } 
     }); 
    AlertDialog alert = builder.create(); 
    alert.show(); 
} 

private final BroadcastReceiver mReceivedSMSReceiver = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 

     if (ACTION.equals(action)) 
     { 
      //your SMS processing code 
      displayAlert(); 
     } 
    } 
} 
+0

但這樣做在你的活動廣播reciever其不隱藏當我在位置它顯示警告時,位置關閉但是當我在位置它不隱藏@raj –

+0

你想要顯示時打開或關閉 – raj

+0

是當它應該自動隱藏我不想點擊任何東西 –

0

OK,然後你不需要爲

你可以在簡歷的方法

@Override 
protected void onResume() { 

    super.onResume(); 
    checkGPS(); 
} 

private void checkGPS(){ 
     locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,1.0f, this); 
     boolean isGPS = locationManager.isProviderEnabled (LocationManager.GPS_PROVIDER); 
     if(isGPS){ 

     }else{ 

      Utils.displayPromptForEnablingGPS(true,MainActivity.this); 

     } 
    } 
+0

同樣的問題我無法隱藏AlertDiolg當我在位置 –