0

我浏览了所有其他有此问题的帖子,但无济于事。

我创建了一个应用程序,当所有代码都包含在 MainActivity 中时,它可以完美运行,但是当我创建一个类来检查 GPS 并清理我的 MainActivity 源文件时,我在调试时收到一个错误,提示“源不是成立”。

就此而言,我是 android 和 java 的新手(我通常使用 c 和 c++),所以我不知道这是否与我的 manifest.xml 等有关,但这让我发疯了!

我在下面包含了我的代码以及我的 manifest.xml 文件。

当逐行浏览我的代码时,给我这个错误的行是final LocationManager manager = (LocationManager) getSystemService(LOCATION_SERVICE);

GpsCheck.java

package com.omegaapp.openomega;

import android.os.*;
import android.location.*;
import android.location.GpsStatus.Listener;
import android.app.*;
import android.content.*;
import com.omegaapp.*;



public class GpsCheck extends android.app.Activity{

    public GpsCheck(){
        final LocationManager manager =  (LocationManager) getSystemService(LOCATION_SERVICE);
        Listener gpsStatusListener = new Listener(){
            @Override
            public void onGpsStatusChanged(int arg0) {
                gpsEnable();

            }
        };
        manager.addGpsStatusListener(gpsStatusListener);
    }

    public boolean gpsEnable(){
        //checks weather the GPS is enabled. If not, prompts the user to enable it then checks again. Returns false if the user chooses not to enable the application.
        if(!checkGpsEnabled()){
            promptEnableGps();
        }
        return true;
    }

    private boolean checkGpsEnabled(){
        final LocationManager manager =  (LocationManager) getSystemService(LOCATION_SERVICE);
        if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            return false;
        }
        return true;
    }

    private boolean promptNoGpsQuit(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setMessage("Without enabling GPS this app can not function, Would you like to quit?");
        alertDialogBuilder.setCancelable(false);
        DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int id){
                Intent callGPSSettingIntent = new Intent(
                android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(callGPSSettingIntent);
            }
        };
        alertDialogBuilder.setPositiveButton("Goto Settings Page To Enable GPS", onClick);

        DialogInterface.OnClickListener onClickCancel = new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int id){
                dialog.cancel();
                finish();
            }
        };
        alertDialogBuilder.setNegativeButton("Quit", onClickCancel);
        AlertDialog alert = alertDialogBuilder.create();
        alert.show();
        return true;
    }

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

        DialogInterface.OnClickListener onClickCancel = new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int id){
                dialog.cancel();
                promptNoGpsQuit();
            }
        };
        alertDialogBuilder.setNegativeButton("Cancel", onClickCancel);
        AlertDialog alert = alertDialogBuilder.create();
        alert.show();
        return true;
    }

}

清单.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.omegaapp.openomega"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.omegaapp.openomega.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:enabled="true" android:name=".BootUpReceiver"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

        <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
</application>

谢谢!

4

2 回答 2

2

这里

public class GpsCheck extends android.app.Activity{

    public GpsCheck(){

  }
      your code  .....
}

这不是创建非 Activity 类来获取 Activity 的正确方法。您需要将活动上下文传递给非活动类以访问系统服务或其他活动或应用程序组件

通过将 Context 传递给 GpsCheck 构造函数,将 GpsCheck 更改为使其工作:

public class GpsCheck{
   Context context;
    LocationManager manager;

    public GpsCheck(Context context){
     this.context=context;

     this.manager =  (LocationManager)context.getSystemService(LOCATION_SERVICE);
     //your code here
    }
      your code  .....
}

并从您的活动发送当前活动上下文为:

GpsCheck gpscheckobj=new GpsCheck(MainActivity.this);
于 2012-12-20T20:12:41.577 回答
0

在活动中:

final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

    if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
        buildAlertMessageNoGps();
    }

在片段(非活动类)中:

Boolean isGPSEnabled = false;
        final LocationManager manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE );

        isGPSEnabled = manager.isProviderEnabled( LocationManager.GPS_PROVIDER );
于 2014-11-21T12:06:24.003 回答