问候伙计们,如果我似乎在这里绊倒了,我提前道歉,但我对 android 开发很陌生
场景是我构建了一个简单的 gps 跟踪应用程序。本质上,用户可以单击紧急按钮或检查按钮或短信来激活服务。在 2.2 版中一切正常,这是我在构建中的目标,但是在运行 2.3.4 的 htc 上进行测试时,只要任何函数调用该服务,应用程序就会崩溃。
简而言之,该服务会检查数据库中的用户在数据库中的 pin 这里是 Pinbox_db,然后它会获取 gps 位置,一旦收到对 Web 服务的 http 调用,以发布设备的经纬度
再次抱歉,如果这对您更有经验的开发人员来说似乎微不足道,但我处于非常新的领域并感谢您的反馈
这是我的服务代码
package com.artgraven.misafety;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class Bg_process extends Service {
private static final String TAG = "BOOMBOOMTESTGPS";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1500;
private static final float LOCATION_DISTANCE = 20f;
public static String b_c_long;
public static String b_c_lat;
public static String alert_pin = "", alert_type ="undefined", alert_device ="android", alert_imei="32154",alert_sim_serial="0796768445";
private Pinbox_db entry = new Pinbox_db(Bg_process.this);
public static String recordSet = "PIN";
private class LocationListener implements android.location.LocationListener{
Location mLastLocation;
public LocationListener(String provider)
{
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}
public void onLocationChanged(Location location)
{
Log.e(TAG, "onLocationChanged: " + location);
mLastLocation.set(location);
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());
Toast.makeText(Bg_process.this, message, Toast.LENGTH_LONG).show();
b_c_long = Double.toString(location.getLongitude());
b_c_lat = Double.toString(location.getLatitude());
postData();
}
public void onProviderDisabled(String provider)
{
Log.e(TAG, "onProviderDisabled: " + provider);
}
public void onProviderEnabled(String provider)
{
Log.e(TAG, "onProviderEnabled: " + provider);
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.e(TAG, "onStatusChanged: " + provider);
}
}
LocationListener[] mLocationListeners = new LocationListener[] {
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
Bundle extras = intent.getExtras();
// alert_type = intent.getStringExtra("code");
alert_type = extras.getString("code");
return START_STICKY;
}
@Override
public void onCreate()
{
// get pin
entry.open();
recordSet = entry.getPin();
entry.close();
alert_pin = recordSet;
TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm.getActiveNetworkInfo().getTypeName().equals("MOBILE"))
alert_device = "android: cellnetwork/3G";
else if(cm.getActiveNetworkInfo().getTypeName().equals("WIFI"))
alert_device = "android: wifi";
else
alert_device ="android: na";
alert_sim_serial = tm.getLine1Number();
alert_imei = tm.getDeviceId();
Log.e(TAG, "onCreate");
initializeLocationManager();
try {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[1]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
@Override
public void onDestroy()
{
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listners, ignore", ex);
}
}
}
}
public static void postData() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.website.co.za/api/activity_log.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("alert_long", b_c_long));
nameValuePairs.add(new BasicNameValuePair("alert_lat", b_c_lat));
nameValuePairs.add(new BasicNameValuePair("alert_pin", alert_pin));
nameValuePairs.add(new BasicNameValuePair("alert_type", alert_type));
nameValuePairs.add(new BasicNameValuePair("alert_device", alert_device));
nameValuePairs.add(new BasicNameValuePair("alert_imei", alert_imei));
nameValuePairs.add(new BasicNameValuePair("alert_sim_serial", alert_sim_serial));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
Location location = mLocationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());
Toast.makeText(Bg_process.this, " " + message, Toast.LENGTH_LONG).show();
b_c_long = Double.toString(location.getLongitude());
b_c_lat = Double.toString(location.getLatitude());
postData();
} else {
Toast.makeText(Bg_process.this, "current location unavailable",
Toast.LENGTH_LONG).show();
}
}
}
}
非常感谢您提前提供任何反馈
这是会话 logCat
11-19 21:15:56.030: E/BOOMBOOMTESTGPS(639): LocationListener gps
11-19 21:15:56.070: E/BOOMBOOMTESTGPS(639): LocationListener network
11-19 21:15:56.330: E/BOOMBOOMTESTGPS(639): onCreate
11-19 21:15:56.330: E/BOOMBOOMTESTGPS(639): initializeLocationManager
11-19 21:15:56.430: E/BOOMBOOMTESTGPS(639): onStartCommand
11-19 21:15:56.520: E/BOOMBOOMTESTGPS(639): onLocationChanged: Location[mProvider=gps,mTime=1353268800000,mLatitude=37.422005,mLongitude=-122.084095,mHasAltitude=true,mAltitude=0.0,mHasSpeed=false,mSpeed=0.0,mHasBearing=false,mBearing=0.0,mHasAccuracy=false,mAccuracy=0.0,mExtras=Bundle[mParcelledData.dataSize=4]]
11-19 21:15:56.810: E/AndroidRuntime(639): FATAL EXCEPTION: main
11-19 21:15:56.810: E/AndroidRuntime(639): android.os.NetworkOnMainThreadException
11-19 21:15:56.810: E/AndroidRuntime(639): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
11-19 21:15:56.810: E/AndroidRuntime(639): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
11-19 21:15:56.810: E/AndroidRuntime(639): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
11-19 21:15:56.810: E/AndroidRuntime(639): at java.net.InetAddress.getAllByName(InetAddress.java:220)
11-19 21:15:56.810: E/AndroidRuntime(639): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-19 21:15:56.810: E/AndroidRuntime(639): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-19 21:15:56.810: E/AndroidRuntime(639): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-19 21:15:56.810: E/AndroidRuntime(639): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-19 21:15:56.810: E/AndroidRuntime(639): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-19 21:15:56.810: E/AndroidRuntime(639): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-19 21:15:56.810: E/AndroidRuntime(639): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-19 21:15:56.810: E/AndroidRuntime(639): at com.artgraven.misafety.Bg_process.postData(Bg_process.java:188)
11-19 21:15:56.810: E/AndroidRuntime(639): at com.artgraven.misafety.Bg_process$LocationListener.onLocationChanged(Bg_process.java:62)
11-19 21:15:56.810: E/AndroidRuntime(639): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:234)
11-19 21:15:56.810: E/AndroidRuntime(639): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:167)
11-19 21:15:56.810: E/AndroidRuntime(639): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:183)
11-19 21:15:56.810: E/AndroidRuntime(639): at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 21:15:56.810: E/AndroidRuntime(639): at android.os.Looper.loop(Looper.java:137)
11-19 21:15:56.810: E/AndroidRuntime(639): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-19 21:15:56.810: E/AndroidRuntime(639): at java.lang.reflect.Method.invokeNative(Native Method)
11-19 21:15:56.810: E/AndroidRuntime(639): at java.lang.reflect.Method.invoke(Method.java:511)
11-19 21:15:56.810: E/AndroidRuntime(639): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-19 21:15:56.810: E/AndroidRuntime(639): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-19 21:15:56.810: E/AndroidRuntime(639): at dalvik.system.NativeStart.main(Native Method)
11-19 21:16:05.839: E/BOOMBOOMTESTGPS(653): LocationListener network
11-19 21:16:05.909: E/BOOMBOOMTESTGPS(653): onCreate
11-19 21:16:05.909: E/BOOMBOOMTESTGPS(653): initializeLocationManager
11-19 21:16:06.019: E/AndroidRuntime(653): FATAL EXCEPTION: main
11-19 21:16:06.019: E/AndroidRuntime(653): java.lang.RuntimeException: Unable to create service com.artgraven.misafety.Bg_process: android.os.NetworkOnMainThreadException
11-19 21:16:06.019: E/AndroidRuntime(653): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2263)
11-19 21:16:06.019: E/AndroidRuntime(653): at android.app.ActivityThread.access$1600(ActivityThread.java:123)
11-19 21:16:06.019: E/AndroidRuntime(653): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
11-19 21:16:06.019: E/AndroidRuntime(653): at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 21:16:06.019: E/AndroidRuntime(653): at android.os.Looper.loop(Looper.java:137)
11-19 21:16:06.019: E/AndroidRuntime(653): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-19 21:16:06.019: E/AndroidRuntime(653): at java.lang.reflect.Method.invokeNative(Native Method)
11-19 21:16:06.019: E/AndroidRuntime(653): at java.lang.reflect.Method.invoke(Method.java:511)
11-19 21:16:06.019: E/AndroidRuntime(653): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-19 21:16:06.019: E/AndroidRuntime(653): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-19 21:16:06.019: E/AndroidRuntime(653): at dalvik.system.NativeStart.main(Native Method)
11-19 21:16:06.019: E/AndroidRuntime(653): Caused by: android.os.NetworkOnMainThreadException
11-19 21:16:06.019: E/AndroidRuntime(653): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
11-19 21:16:06.019: E/AndroidRuntime(653): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
11-19 21:16:06.019: E/AndroidRuntime(653): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
11-19 21:16:06.019: E/AndroidRuntime(653): at java.net.InetAddress.getAllByName(InetAddress.java:220)
11-19 21:16:06.019: E/AndroidRuntime(653): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-19 21:16:06.019: E/AndroidRuntime(653): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-19 21:16:06.019: E/AndroidRuntime(653): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-19 21:16:06.019: E/AndroidRuntime(653): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-19 21:16:06.019: E/AndroidRuntime(653): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-19 21:16:06.019: E/AndroidRuntime(653): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-19 21:16:06.019: E/AndroidRuntime(653): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-19 21:16:06.019: E/AndroidRuntime(653): at com.artgraven.misafety.Bg_process.postData(Bg_process.java:188)
11-19 21:16:06.019: E/AndroidRuntime(653): at com.artgraven.misafety.Bg_process.initializeLocationManager(Bg_process.java:211)
11-19 21:16:06.019: E/AndroidRuntime(653): at com.artgraven.misafety.Bg_process.onCreate(Bg_process.java:134)
11-19 21:16:06.019: E/AndroidRuntime(653): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2253)
11-19 21:16:06.019: E/AndroidRuntime(653): ... 10 more
在实施建议的答案后,此更新的 logcat 来自更高的 api 版本 (15),问题已在模拟器上解决,但在设备上测试时出现以下错误
12-08 18:12:47.360: W/dalvikvm(21432): threadid=1: thread exiting with uncaught exception (group=0x401b1760)
12-08 18:12:47.360: E/AndroidRuntime(21432): FATAL EXCEPTION: main
12-08 18:12:47.360: E/AndroidRuntime(21432): java.lang.RuntimeException: Unable to create service com.artgraven.specialopstech.Bg_process: android.os.NetworkOnMainThreadException
12-08 18:12:47.360: E/AndroidRuntime(21432): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2049)
12-08 18:12:47.360: E/AndroidRuntime(21432): at android.app.ActivityThread.access$2500(ActivityThread.java:122)
12-08 18:12:47.360: E/AndroidRuntime(21432): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1059)
12-08 18:12:47.360: E/AndroidRuntime(21432): at android.os.Handler.dispatchMessage(Handler.java:99)
12-08 18:12:47.360: E/AndroidRuntime(21432): at android.os.Looper.loop(Looper.java:132)
12-08 18:12:47.360: E/AndroidRuntime(21432): at android.app.ActivityThread.main(ActivityThread.java:4028)
12-08 18:12:47.360: E/AndroidRuntime(21432): at java.lang.reflect.Method.invokeNative(Native Method)
12-08 18:12:47.360: E/AndroidRuntime(21432): at java.lang.reflect.Method.invoke(Method.java:491)
12-08 18:12:47.360: E/AndroidRuntime(21432): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
12-08 18:12:47.360: E/AndroidRuntime(21432): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
12-08 18:12:47.360: E/AndroidRuntime(21432): at dalvik.system.NativeStart.main(Native Method)
12-08 18:12:47.360: E/AndroidRuntime(21432): Caused by: android.os.NetworkOnMainThreadException
12-08 18:12:47.360: E/AndroidRuntime(21432): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
12-08 18:12:47.360: E/AndroidRuntime(21432): at java.net.InetAddress.lookupHostByName(InetAddress.java:477)
12-08 18:12:47.360: E/AndroidRuntime(21432): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
12-08 18:12:47.360: E/AndroidRuntime(21432): at java.net.InetAddress.getAllByName(InetAddress.java:249)
12-08 18:12:47.360: E/AndroidRuntime(21432): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
12-08 18:12:47.360: E/AndroidRuntime(21432): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-08 18:12:47.360: E/AndroidRuntime(21432): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-08 18:12:47.360: E/AndroidRuntime(21432): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
12-08 18:12:47.360: E/AndroidRuntime(21432): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-08 18:12:47.360: E/AndroidRuntime(21432): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-08 18:12:47.360: E/AndroidRuntime(21432): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-08 18:12:47.360: E/AndroidRuntime(21432): at com.artgraven.specialopstech.Bg_process.postData(Bg_process.java:192)
12-08 18:12:47.360: E/AndroidRuntime(21432): at com.artgraven.specialopstech.Bg_process.initializeLocationManager(Bg_process.java:215)
12-08 18:12:47.360: E/AndroidRuntime(21432): at com.artgraven.specialopstech.Bg_process.onCreate(Bg_process.java:138)
12-08 18:12:47.360: E/AndroidRuntime(21432): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2039)
12-08 18:12:47.360: E/AndroidRuntime(21432): ... 10 more