Android 如何使GPS功能工作?

Android 如何使GPS功能工作?,android,eclipse,gps,Android,Eclipse,Gps,我是Eclipse和Android应用程序的新手,所以这里有一个非常新的问题。如何使此功能正常工作?我刚刚将其复制>粘贴到我的公共类nowActivity扩展了活动{,并修复了一致的错误。函数如下: package weather.right; import weather.right.now.R; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterfac

我是Eclipse和Android应用程序的新手,所以这里有一个非常新的问题。如何使此功能正常工作?我刚刚将其复制>粘贴到我的
公共类nowActivity扩展了活动{
,并修复了一致的错误。函数如下:

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();
        }
}

提前感谢。

创建时受保护的void(Bundle savedInstanceState)
应该是
创建时受保护的void(Bundle savedInstanceState)

您应该重写
onCreate()
方法。有关更多详细信息,请参阅

对于Android来说,
Activity
的子类应该实现某些方法,因此要做到这一点,您必须通过精确匹配父类的方法来覆盖某些方法。
onCreate()
就是这样一种方法


对于仿真器,GPS可以按照指南进行测试。否则它将显示为禁用状态。

嗯。是吗?这是代码中的第二行。你的观点是什么?我是这些“墙”中的新手,记住。@ErikEdgren它应该是
onCreate()
,而不是
onCreate()
(注意后面的
1
)。但是,如果您实际上打算声明一个尾随
1
的方法,则忽略上面的响应。但是,onCreate()将自动调用,因为它是Android中所有活动的重要方法。:)谢谢,但不会再次发生任何事:(我正在通过Eclipse中的Android虚拟设备测试我的应用程序。这可能是问题所在?如果不是,我该如何解决此问题?)@ninetwozero我在尝试使用
onCreate()
时收到了“复制”错误消息。我删除了
onCreate()
并将其内容移动到
onCreate())
。同样的事情发生了-什么也没有发生。将
protected
更改为
public
onCreate()
并查看是否有任何变化?另外,发布整个类。