ロケーションマネージャーAPIを使用してユーザーの現在の場所が必要なAndroidアプリケーションを作成しています...最初にインターネット接続を確保し、インターネットが接続されていない場合はアプリにダイログボックスが表示されます。これを実行すると、致命的な主な例外などのさまざまなタイプのエラーが表示されます..私は一生懸命疲れましたが、最終年のプロジェクトに関連しているため、解決策を見つけることができませんでした..
package com.example.directory;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity {
ConnectionDetector cd;
AlertDialogManager alert = new AlertDialogManager();
Boolean isInternetPresent = false;
GPSTracker gps;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//cd = new ConnectionDetector(getApplicationContext());
//isInternetPresent = cd.isConnectingToInternet();
/*if (!isInternetPresent) {
// Internet Connection is not present
alert.showAlertDialog(MainActivity.this, "Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
else{alert.showAlertDialog(MainActivity.this, "Internet Connection Error",
" working Internet connection", false);}*/
gps = new GPSTracker(this);
if (gps.canGetLocation()) {
Log.d("Your Location", "latitude:" + gps.getLatitude() + ", longitude: " + gps.getLongitude());
} else {
// Can't get user's current location
alert.showAlertDialog(MainActivity.this, "GPS Status",
"Couldn't get location information. Please enable GPS",
false);
// stop executing code by return
return;
}
// lv = (ListView) findViewById(R.id.list);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
/////////////////////
package com.example.directory;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
/**
* Checking for all possible internet providers
* **/
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
}
//////////////////
package com.example.directory;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location = null; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener Calling this function will stop using GPS in your
* app
* */
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
*
* @return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}}
/////////////////////////
package com.example.directory;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
public class AlertDialogManager {
/**
* Function to display simple Alert Dialog
* @param context - application context
* @param title - alert dialog title
* @param message - alert message
* @param status - success/failure (used to set icon)
* - pass null if you don't want icon
* */
public void showAlertDialog(Context context, String title, String message,
Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
if(status != null)
// Setting alert dialog icon
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
// Showing Alert Message
alertDialog.show();
}
}
log cat
////////////////////////////
01-26 20:11:59.212: W/dalvikvm(2541): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-26 20:11:59.232: E/AndroidRuntime(2541): FATAL EXCEPTION: main
01-26 20:11:59.232: E/AndroidRuntime(2541): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.directory/com.example.directory.MainActivity}: java.lang.SecurityException: ConnectivityService: Neither user 10042 nor current process has android.permission.ACCESS_NETWORK_STATE.
01-26 20:11:59.232: E/AndroidRuntime(2541): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-26 20:11:59.232: E/AndroidRuntime(2541): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-26 20:11:59.232: E/AndroidRuntime(2541): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-26 20:11:59.232: E/AndroidRuntime(2541): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-26 20:11:59.232: E/AndroidRuntime(2541): at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 20:11:59.232: E/AndroidRuntime(2541): at android.os.Looper.loop(Looper.java:130)
01-26 20:11:59.232: E/AndroidRuntime(2541): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-26 20:11:59.232: E/AndroidRuntime(2541): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 20:11:59.232: E/AndroidRuntime(2541): at java.lang.reflect.Method.invoke(Method.java:507)
01-26 20:11:59.232: E/AndroidRuntime(2541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-26 20:11:59.232: E/AndroidRuntime(2541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-26 20:11:59.232: E/AndroidRuntime(2541): at dalvik.system.NativeStart.main(Native Method)
01-26 20:11:59.232: E/AndroidRuntime(2541): Caused by: java.lang.SecurityException: ConnectivityService: Neither user 10042 nor current process has android.permission.ACCESS_NETWORK_STATE.
01-26 20:11:59.232: E/AndroidRuntime(2541): at android.os.Parcel.readException(Parcel.java:1322)
01-26 20:11:59.232: E/AndroidRuntime(2541): at android.os.Parcel.readException(Parcel.java:1276)
01-26 20:11:59.232: E/AndroidRuntime(2541): at android.net.IConnectivityManager$Stub$Proxy.getAllNetworkInfo(IConnectivityManager.java:390)
01-26 20:11:59.232: E/AndroidRuntime(2541): at android.net.ConnectivityManager.getAllNetworkInfo(ConnectivityManager.java:267)
01-26 20:11:59.232: E/AndroidRuntime(2541): at com.example.directory.ConnectionDetector.isConnectingToInternet(ConnectionDetector.java:22)
01-26 20:11:59.232: E/AndroidRuntime(2541): at com.example.directory.MainActivity.onCreate(MainActivity.java:23)
01-26 20:11:59.232: E/AndroidRuntime(2541): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-26 20:11:59.232: E/AndroidRuntime(2541): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-26 20:11:59.232: E/AndroidRuntime(2541): ... 11 more