I have looked through all of the other posts that have this problem, to no avail.
I have created an app, that works perfectly when all of the code is housed inside of MainActivity, but when I create a class to check the GPS, and clean up my MainActivity source file, I get an error when debugging that says “source not found”.
I am new to android and java for that matter (I typically work with c & c++), so I don’t know if this has something to do with my manifest.xml or the like, but it is driving me nuts!
I have included my code below, along with my manifest.xml file.
When going through my code line by line, the line that gives me this error is 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;
}
}
Manifest.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>
Thank you!
Here
this is not right way to create an non Activity class for getting Context of Activity . you will need to pass Activity Context to non Activity class for Accessing system services or other Activity or Application components
Change your GpsCheck as for make it working by passing Context to GpsCheck constructor as:
and from your Activity send Current Activity Context as :