如何使GPS功能工作?

时间:2012-04-02 01:13:05

标签: android eclipse gps

我是Eclipse和Android应用程序的新手,所以这里提出了一个非常新的问题。如何使此功能正常工作?我只是复制>将其粘贴到我的public class nowActivity extends Activity {并修复符合的错误。功能如下:

package weather.right;

import weather.right.now.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class nowActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
             Toast.makeText(this, "GPS is Enabled in your devide", Toast.LENGTH_SHORT).show();
        }else{
            showGPSDisabledAlertToUser();
        }
    }

    public void goToSo(View view) {
        goToUrl("http://erik-edgren.nu/weather");
    }

    private void goToUrl(String url) {
        Uri uriUrl = Uri.parse(url);
        Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
        startActivity(launchBrowser);
    }

        private void showGPSDisabledAlertToUser(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
                alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?")
             .setCancelable(false)
             .setPositiveButton("Goto Settings Page To Enable GPS",
                  new DialogInterface.OnClickListener(){
                  public void onClick(DialogInterface dialog, int id){
                          Intent callGPSSettingIntent = new Intent(
                                                android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                startActivity(callGPSSettingIntent);
                  }
             });
             alertDialogBuilder.setNegativeButton("Cancel",
                  new DialogInterface.OnClickListener(){
                  public void onClick(DialogInterface dialog, int id){
                       dialog.cancel();
                  }
             });
        AlertDialog alert = alertDialogBuilder.create();
        alert.show();
        }
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

protected void onCreate1(Bundle savedInstanceState)应为protected void onCreate(Bundle savedInstanceState)

您应该覆盖onCreate()方法。有关详细信息,请参阅this

对于Android,Activity的子类应该实现某些方法,所以要做到这一点,你必须通过完全匹配父类的方法来覆盖某些方法。 onCreate()就是这样一种方法。

对于模拟器,可以按照指南here测试GPS。否则它将显示为已禁用。