2014-02-12 122 views
0

在我的應用程序中,如果GPS關閉並且應用程序啓動,將出現一個警報對話框,其中包含按鈕「GPS設置」和「取消」,並且如果GPS打開並且應用程序將不會啓動發生。在應用程序中,有一個顯示GPS狀態的按鈕,即如果GPS打開,它將顯示「GPS已打開」,如果關閉「GPS關閉,打開它」,當用戶單擊它時,它將打開GPS設置當用戶啓用GPS並返回時,按鈕必須顯示「GPS已打開」。當用戶啓用GPS時,我在按鈕上顯示狀態時遇到麻煩。請幫忙。我是android新手。Android在按鈕上顯示GPS狀態時遇到的問題

 private Button GPSState; 
     GPSState = (Button) findViewById(R.id.bGPSstate); 
     boolean isGPSEnabled = false; 
     LocationManager locationManager; 

       locationManager = (LocationManager) getApplicationContext() 
         .getSystemService(LOCATION_SERVICE); 
     // getting GPS status 
       isGPSEnabled = locationManager 
         .isProviderEnabled(LocationManager.GPS_PROVIDER); 

       if (!isGPSEnabled) { 
        // no GPS provider is enabled 
        // displaying GPS status on the button and and opening GPS Settings 
        GPSState.setText("GPS is OFF.Turn it On"); 
        GPSState.setOnClickListener(new View.OnClickListener() { 

         @Override 
         public void onClick(View arg0) { 
          // TODO Auto-generated method stub 
          startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 

         } 
        }); 
     // creating alertdialog 
        AlertDialog.Builder builder = new AlertDialog.Builder(c); 

        builder.setTitle("Settings"); 
        builder.setMessage("Enable GPS for the Application"); 

        builder.setPositiveButton("GPS Setting", 

        new DialogInterface.OnClickListener() { 

         public void onClick(DialogInterface dialog, int which) { 
          startActivity(new Intent(
            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 

          dialog.dismiss(); 
         } 

        }); 

        builder.setNegativeButton("Cancel", 
          new DialogInterface.OnClickListener() { 

           @Override 
           public void onClick(DialogInterface dialog, int which) { 

            new AlertDialog.Builder(MainActivity.this) 
              .setTitle("How to use Application") 
              .setMessage(
                "You must enable the GPS in order to use this application. Press Activate and then press Power Button twice in order to send the alert message to the selected contacts") 
              .setNeutralButton(
                "OK", 
                new DialogInterface.OnClickListener() { 

                 @Override 
                 public void onClick(
                   DialogInterface dialog, 
                   int which) { 
                  // do something // for 
                  // returning back to // 
                  // application 
                  dialog.cancel(); 

                 } 
                }).show(); 
            dialog.dismiss(); 

           } 
          }); 

        builder.show(); 

       } else { // GPS provider is enabled } 
        GPSState.setText("GPS is On"); 

       } 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      // TODO Auto-generated method stub 
      super.onActivityResult(requestCode, resultCode, data); 

GPSState.setText("GPS is on"); 
} 

回答

2

使用startActivityForResult代替startActivity當你調用設置啓用GPS。在用戶啓用/禁用GPS設置頁面後,他/她將按返回按鈕返回到您的應用程序。在這一刻,方法onActivityResult被調用。你可以在這裏設置你的文字。

如何設置您的文本?粗略地:

onActivityResult() { 
    GPSState = (Button) findViewById(R.id.bGPSstate); 
    LocationManager locationManager; 

    locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE); 
    isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    if (!isGPSEnabled) GPSState.setText("GPS is off"); 
    else GPSState.setText("GPS is on"); 
} 

目前我沒有看到android文檔或eclipse。但我認爲這可以給你一個線索。不過,我知道這種方法不是那樣的。

+0

我完成了startActivityForResult,請告訴我如何設置文本onActivityResult。 –

+0

我已經更新了我的回答 – ardhipoetra

+0

非常感謝。它有幫助。實際上我在onActivityResult中感到困惑。非常感謝 –

0

使用此代碼:

public static boolean isGpS(Context mContext) 
    { 
     final LocationManager manager = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE); 

      if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
      { 
       return false; 
      } 
      else 
      { 
       return true; 
      } 
    } 

在你的onCreate這樣寫:

if(!Utils.isGpS(Youractivity.this) 
{ 
    buildAlertMessageNoGps(); 
} 

這是showAlertDialog方法:

private void buildAlertMessageNoGps() 
{ 
     final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Yout GPS seems to be disabled, do you want to enable it?") 
       .setCancelable(false) 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) { 
         startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
        } 
       }) 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) { 
         dialog.cancel(); 
        } 
       }); 
     final AlertDialog alert = builder.create(); 
     alert.show(); 
    } 

注:不要忘記在AndroidMenifest文件中應用權限。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/> 
相關問題