我将解释我的应用程序的过程...
首先它显示谷歌地图然后我正在绘制一些折线,然后我选择一些联系人给他发送坐标(所有这些都在主要活动中并且它正在工作)
这是我主要活动的代码:
public class MainActivity extends FragmentActivity {
static boolean active = false;
@Override
public void onStart() {
super.onStart();
active = true;
}
@Override
public void onStop() {
super.onStop();
active = false;
}
public static boolean isActive(){
return active;
}
private static final int PICK_CONTACT = 1;
GoogleMap googleMap;
ArrayList<LatLng> points= new ArrayList<LatLng>() ;
Double glat;
Double glon;
int find_someone=0;
int save=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
googleMap = fm.getMap();
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
// Enabling buildings of Google Map
googleMap.setBuildingsEnabled(true);
googleMap.setOnMapLoadedCallback(new OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
Location lm = googleMap.getMyLocation();
if (lm!=null){
CameraPosition cp = new CameraPosition.Builder()
.target(new LatLng(lm.getLatitude(), lm.getLongitude()))
.zoom(17)
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cp));
}
}
});
// Setting OnClick event listener for the Google Map
googleMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
// Instantiating the class MarkerOptions to plot marker on the map
MarkerOptions markerOptions = new MarkerOptions();
// Setting latitude and longitude of the marker position
markerOptions.position(point);
// Setting title of the infowindow of the marker
markerOptions.title("Position");
// Setting the content of the infowindow of the marker
markerOptions.snippet("Latitude:"+point.latitude+","+"Longitude:"+point.longitude);
// Instantiating the class PolylineOptions to plot polyline in the map
PolylineOptions polylineOptions = new PolylineOptions();
// Setting the color of the polyline
polylineOptions.color(Color.BLUE);
// Setting the width of the polyline
polylineOptions.width(6);
// Adding the taped point to the ArrayList
points.add(point);
// Setting points of polyline
polylineOptions.addAll(points);
// Adding the polyline to the map
googleMap.addPolyline(polylineOptions);
// Adding the marker to the map
googleMap.addMarker(markerOptions);
}
});
googleMap.setOnMapLongClickListener(new OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng point) {
// Clearing the markers and polylines in the google map
googleMap.clear();
// Empty the array list
points.clear();
}
});
if (String.valueOf(points)!=null){
Button pickContact = (Button) findViewById(R.id.button1);
pickContact.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
save=1;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 1);
}
});
}
else{
Toast.makeText(this, "select points", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver().query(uri, new String[]{
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE },
null, null, null);
if (c != null && c.moveToFirst()) {
String number = c.getString(0);
int type = c.getInt(1);
showSelectedNumber(type, number);
System.out.println(number);
System.out.println("val: "+String.valueOf(points));
if (save==1)
{
SmsManager sm = SmsManager.getDefault();
ArrayList<String> parts =sm.divideMessage(String.valueOf(points));
int numParts = parts.size();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
for (int i = 0; i < numParts; i++) {
sentIntents.add(PendingIntent.getBroadcast(getBaseContext(), 0, getIntent(), 0));
deliveryIntents.add(PendingIntent.getBroadcast(getBaseContext(), 0, data, 0));
}
sm.sendMultipartTextMessage(number,null, parts, sentIntents, deliveryIntents);
}
}
}
finally {
if (c != null) {
c.close();
}
}
}
}
}
public void showSelectedNumber(int type, String number) {
Toast.makeText(this, type + ": " + number, Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Inflate the menu; this adds items to the action bar if it is present.
return true;
}
}
但后来我发现这个logcat运行起来......它是由我正在运行的服务引起的
05-13 00:18:54.950: D/Netork(16444): Network
05-13 00:18:54.950: D/Netork(16444): Network
05-13 00:18:54.950: D/Netork(16444): Network
05-13 00:18:54.950: D/Netork(16444): Network
05-13 00:18:54.955: D/Netork(16444): Network
05-13 00:18:54.955: D/Netork(16444): Network
05-13 00:18:54.955: D/Netork(16444): Network
05-13 00:18:54.955: D/Netork(16444): Network
05-13 00:18:54.960: D/Netork(16444): Network
05-13 00:18:54.960: D/Netork(16444): Network
05-13 00:18:54.960: D/Netork(16444): Network
05-13 00:18:54.960: D/Netork(16444): Network
05-13 00:18:54.965: D/Netork(16444): Network
05-13 00:18:54.965: D/Netork(16444): Network
05-13 00:18:54.965: D/Netork(16444): Network
05-13 00:18:54.970: D/Netork(16444): Network
05-13 00:18:54.970: D/Netork(16444): Network
05-13 00:18:54.970: D/Netork(16444): Network
05-13 00:18:54.975: D/Netork(16444): Network
05-13 00:18:54.975: D/Netork(16444): Network
05-13 00:18:54.975: D/Netork(16444): Network
05-13 00:18:54.980: D/Netork(16444): Network
05-13 00:18:54.980: D/Netork(16444): Network
05-13 00:18:54.980: D/Netork(16444): Network
05-13 00:18:54.985: D/Netork(16444): Network
05-13 00:18:54.985: D/Netork(16444): Network
05-13 00:18:54.985: D/Netork(16444): Network
05-13 00:18:54.990: D/Netork(16444): Network
05-13 00:18:54.990: D/Netork(16444): Network
05-13 00:18:54.990: D/Netork(16444): Network
05-13 00:18:54.995: D/Netork(16444): Network
05-13 00:18:54.995: D/Netork(16444): Network
05-13 00:18:54.995: D/Netork(16444): Network
05-13 00:18:55.000: D/Netork(16444): Network
05-13 00:18:55.000: D/Netork(16444): Network
05-13 00:18:55.000: D/Netork(16444): Network
05-13 00:18:55.005: D/Netork(16444): Network
05-13 00:18:55.005: D/Netork(16444): Network
05-13 00:18:55.005: D/Netork(16444): Network
05-13 00:18:55.010: D/Netork(16444): Network
05-13 00:18:55.010: D/Netork(16444): Network
05-13 00:18:55.010: D/Netork(16444): Network
05-13 00:18:55.015: D/Netork(16444): Network
05-13 00:18:55.015: D/Netork(16444): Network
05-13 00:18:55.015: D/Netork(16444): Network
05-13 00:18:55.020: D/Netork(16444): Network
05-13 00:18:55.020: D/Netork(16444): Network
05-13 00:18:55.020: D/Netork(16444): Network
05-13 00:18:55.025: D/Netork(16444): Network
05-13 00:18:55.025: D/Netork(16444): Network
05-13 00:18:55.025: D/Netork(16444): Network
05-13 00:18:55.030: D/Netork(16444): Network
05-13 00:18:55.030: D/Netork(16444): Network
05-13 00:18:55.030: D/Netork(16444): Network
05-13 00:18:55.035: D/Netork(16444): Network
05-13 00:18:55.035: D/Netork(16444): Network
05-13 00:18:55.035: D/Netork(16444): Network
05-13 00:18:55.040: D/Netork(16444): Network
05-13 00:18:55.040: D/Netork(16444): Network
05-13 00:18:55.040: D/Netork(16444): Network
05-13 00:18:55.040: D/Netork(16444): Network
05-13 00:18:55.045: D/Netork(16444): Network
05-13 00:18:55.045: D/Netork(16444): Network
05-13 00:18:55.045: D/Netork(16444): Network
05-13 00:18:55.050: D/Netork(16444): Network
05-13 00:18:55.050: D/Netork(16444): Network
05-13 00:18:55.050: D/Netork(16444): Network
05-13 00:18:55.055: D/Netork(16444): Network
05-13 00:18:55.055: D/Netork(16444): Network
05-13 00:18:55.055: D/Netork(16444): Network
05-13 00:18:55.055: D/Netork(16444): Network
05-13 00:18:55.060: D/Netork(16444): Network
05-13 00:18:55.060: D/Netork(16444): Network
05-13 00:18:55.065: D/Netork(16444): Network
05-13 00:18:55.065: D/Netork(16444): Network
05-13 00:18:55.065: D/Netork(16444): Network
05-13 00:18:55.065: D/Netork(16444): Network
05-13 00:18:55.070: D/Netork(16444): Network
05-13 00:18:55.070: D/Netork(16444): Network
05-13 00:18:55.070: D/Netork(16444): Network
05-13 00:18:55.075: D/Netork(16444): Network
05-13 00:18:55.075: D/Netork(16444): Network
05-13 00:18:55.075: D/Netork(16444): Network
05-13 00:18:55.075: D/Netork(16444): Network
05-13 00:18:55.080: D/Netork(16444): Network
05-13 00:18:55.080: D/Netork(16444): Network
05-13 00:18:55.085: D/Netork(16444): Network
05-13 00:18:55.085: D/Netork(16444): Network
05-13 00:18:55.085: D/Netork(16444): Network
05-13 00:18:55.085: D/Netork(16444): Network
05-13 00:18:55.090: D/Netork(16444): Network
05-13 00:18:55.090: D/Netork(16444): Network
05-13 00:18:55.090: D/Netork(16444): Network
05-13 00:18:55.095: D/Netork(16444): Network
05-13 00:18:55.095: D/Netork(16444): Network
05-13 00:18:55.100: D/Netork(16444): Network
05-13 00:18:55.100: D/Netork(16444): Network
05-13 00:18:55.100: D/Netork(16444): Network
05-13 00:18:55.105: D/Netork(16444): Network
05-13 00:18:55.105: D/Netork(16444): Network
05-13 00:18:55.105: D/Netork(16444): Network
05-13 00:18:55.105: D/Netork(16444): Network
05-13 00:18:55.110: D/Netork(16444): Network
05-13 00:18:55.110: D/Netork(16444): Network
05-13 00:18:55.110: D/Netork(16444): Network
05-13 00:18:55.115: D/Netork(16444): Network
05-13 00:18:55.115: D/Netork(16444): Network
05-13 00:18:55.115: D/Netork(16444): Network
05-13 00:18:55.115: D/Netork(16444): Network
05-13 00:18:55.120: D/Netork(16444): Network
05-13 00:18:55.120: D/Netork(16444): Network
05-13 00:18:55.125: D/Netork(16444): Network
05-13 00:18:55.125: D/Netork(16444): Network
05-13 00:18:55.125: D/Netork(16444): Network
05-13 00:18:55.125: D/Netork(16444): Network
05-13 00:18:55.130: D/Netork(16444): Network
05-13 00:18:55.130: D/Netork(16444): Network
05-13 00:18:55.130: D/Netork(16444): Network
05-13 00:18:55.130: D/Netork(16444): Network
05-13 00:18:55.135: D/Netork(16444): Network
05-13 00:18:55.135: D/Netork(16444): Network
05-13 00:18:55.135: D/Netork(16444): Network
05-13 00:18:55.140: D/Netork(16444): Network
05-13 00:18:55.140: D/Netork(16444): Network
05-13 00:18:55.140: D/Netork(16444): Network
05-13 00:18:55.140: D/Netork(16444): Network
05-13 00:18:55.145: D/Netork(16444): Network
05-13 00:18:55.145: D/Netork(16444): Network
05-13 00:18:55.145: D/Netork(16444): Network
05-13 00:18:55.145: D/Netork(16444): Network
05-13 00:18:55.150: D/Netork(16444): Network
05-13 00:18:55.150: D/Netork(16444): Network
05-13 00:18:55.150: D/Netork(16444): Network
05-13 00:18:55.155: D/Netork(16444): Network
05-13 00:18:55.155: D/Netork(16444): Network
05-13 00:18:55.155: D/Netork(16444): Network
05-13 00:18:55.160: D/Netork(16444): Network
05-13 00:18:55.160: D/Netork(16444): Network
05-13 00:18:55.160: D/Netork(16444): Network
05-13 00:18:55.165: D/Netork(16444): Network
05-13 00:18:55.165: D/Netork(16444): Network
05-13 00:18:55.170: D/Netork(16444): Network
05-13 00:18:55.170: D/Netork(16444): Network
05-13 00:18:55.170: D/Netork(16444): Network
05-13 00:18:55.170: D/Netork(16444): Network
05-13 00:18:55.175: D/Netork(16444): Network
05-13 00:18:55.175: D/Netork(16444): Network
05-13 00:18:55.175: D/Netork(16444): Network
05-13 00:18:55.175: D/Netork(16444): Network
05-13 00:18:55.180: D/Netork(16444): Network
05-13 00:18:55.185: D/Netork(16444): Network
05-13 00:18:55.185: D/Netork(16444): Network
05-13 00:18:55.185: D/Netork(16444): Network
05-13 00:18:55.185: D/Netork(16444): Network
05-13 00:18:55.190: D/Netork(16444): Network
05-13 00:18:55.190: D/Netork(16444): Network
05-13 00:18:55.190: D/Netork(16444): Network
05-13 00:18:55.195: D/Netork(16444): Network
05-13 00:18:55.195: D/Netork(16444): Network
05-13 00:18:55.195: D/Netork(16444): Network
05-13 00:18:55.195: D/Netork(16444): Network
05-13 00:18:55.200: D/Netork(16444): Network
05-13 00:18:55.200: D/Netork(16444): Network
05-13 00:18:55.200: D/Netork(16444): Network
05-13 00:18:55.200: D/Netork(16444): Network
05-13 00:18:55.205: D/Netork(16444): Network
05-13 00:18:55.205: D/Netork(16444): Network
05-13 00:18:55.210: D/Netork(16444): Network
05-13 00:18:55.210: D/Netork(16444): Network
05-13 00:18:55.210: D/Netork(16444): Network
05-13 00:18:55.215: D/Netork(16444): Network
05-13 00:18:55.215: D/Netork(16444): Network
05-13 00:18:55.220: D/Netork(16444): Network
05-13 00:18:55.220: D/Netork(16444): Network
05-13 00:18:55.220: D/Netork(16444): Network
05-13 00:18:55.225: D/Netork(16444): Network
05-13 00:18:55.225: D/Netork(16444): Network
05-13 00:18:55.230: D/Netork(16444): Network
05-13 00:18:55.230: D/Netork(16444): Network
05-13 00:18:55.230: D/Netork(16444): Network
05-13 00:18:55.235: D/Netork(16444): Network
05-13 00:18:55.235: D/Netork(16444): Network
05-13 00:18:55.235: D/Netork(16444): Network
05-13 00:18:55.240: D/Netork(16444): Network
05-13 00:18:55.240: D/Netork(16444): Network
05-13 00:18:55.240: D/Netork(16444): Network
05-13 00:18:55.245: D/Netork(16444): Network
05-13 00:18:55.245: D/Netork(16444): Network
05-13 00:18:55.245: D/Netork(16444): Network
05-13 00:18:55.250: D/Netork(16444): Network
05-13 00:18:55.250: D/Netork(16444): Network
05-13 00:18:55.250: D/Netork(16444): Network
05-13 00:18:55.255: D/Netork(16444): Network
05-13 00:18:55.255: D/Netork(16444): Network
05-13 00:18:55.255: D/Netork(16444): Network
05-13 00:18:55.260: D/Netork(16444): Network
05-13 00:18:55.260: D/Netork(16444): Network
05-13 00:18:55.265: D/Netork(16444): Network
05-13 00:18:55.265: D/Netork(16444): Network
05-13 00:18:55.265: D/Netork(16444): Network
05-13 00:18:55.265: D/Netork(16444): Network
05-13 00:18:55.270: D/Netork(16444): Network
05-13 00:18:55.270: D/Netork(16444): Network
05-13 00:18:55.270: D/Netork(16444): Network
05-13 00:18:55.275: D/Netork(16444): Network
05-13 00:18:55.275: D/Netork(16444): Network
05-13 00:18:55.275: D/Netork(16444): Network
05-13 00:18:55.280: D/Netork(16444): Network
05-13 00:18:55.280: D/Netork(16444): Network
05-13 00:18:55.280: D/Netork(16444): Network
............................................
并且我的手机卡住了,服务一直锁定在网络上......
这是我的服务代码:
public class GPSTRACKER extends Service implements LocationListener {
//private final Context mContext=getBaseContext();
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude;// latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1;
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 60 ;
// Declaring a Location Manager
protected LocationManager locationManager;
//public GPSTRACKER(Context context) {
//this.mContext = context;
//}
//WakeLock wakeLock;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
//PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
//wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotSleep");
}
@Override
public void onStart(Intent intent, int startId) {
Bundle extras = intent.getExtras();
if (extras != null && !extras.isEmpty()) { // has effect of unparcelling Bundle
String message = intent.getStringExtra("message");
String number = intent.getStringExtra("number");
//int pumber= Integer.parseInt(number.toString());
int p=0,j,i=1,t,success=0;
double[] d = new double[1000];
Matcher m = Pattern.compile("(?!=\\d\\.\\d\\.)([\\d.]+)").matcher(message);
while(m.find())
{
double k = Double.parseDouble(m.group(1));
d[p]=k;
p++;
}
//if (message.contains(number)){
// Location h=getLocation();
// latitude=h.getLatitude();
//longitude = h.getLongitude();
// final LocalBroadcastManager localBroadcastManager =
// LocalBroadcastManager.getInstance(getBaseContext());
//intent.putExtra("lat",latitude);
// intent.putExtra("lng",longitude);
//localBroadcastManager.sendBroadcast(new Intent());
// }
double line;
double[] ship = new double[1000] ;
double[] b = new double[1000] ;
ship[0]=SlopeCalc(d[2],d[0],d[3],d[1]);
b[0]=d[0]-ship[0]*d[1];
if (p>3)
{
for(j=2;j<p;j++)
{
if(j+2<p){
ship[i]=SlopeCalc(d[j+2],d[j-2+2],d[j+1+2],d[j-1+2]);
b[i]=d[j]-(ship[i]*d[j+1]);
j++;
i++;
}
else{
break;
}
}
}
while(true)
{
Location h=getLocation();
latitude=h.getLatitude();
longitude = h.getLongitude();
//System.out.println(latitude);
// System.out.println(longitude);
for (t=0;t<i;t++){
line=ship[t]*longitude+b[t]-latitude;
if (line>-0.001 && line<0.001){
success=1;
break;
}
}
if (success==1){
break;
}
}
if ( success==1){
SmsManager.getDefault().sendTextMessage(number, null, "HI WHATS UP DUDE HE IS THER", null, null);
//wakeLock.release();
stopService(intent);
}
}
else
{
Log.i("Log", "Bundle is null");
}
}
public static double SlopeCalc(double y2,double y1, double x2,double x1){
double sou;
sou=(y2-y1)/(x2-x1);
return sou;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates((android.location.LocationListener) 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;
}
/**
* Function to show settings alert dialog
* On pressing Settings button will launch Settings Options
* */
//public void showSettingsAlert(){
//AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
// alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
// alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
// alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog,int which) {
// Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
//mContext.startActivity(intent);
// }
// });
// on pressing cancel button
// alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog, int which) {
// dialog.cancel();
// }
//});
// Showing Alert Message
// alertDialog.show();
//}
@Override
public void onLocationChanged(Location location) {
//getLocation();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public Location getLocation() {
locationManager = (LocationManager)
getSystemService(LOCATION_SERVICE);
//getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Netork", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
// 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 Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
}
return location;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
LOGCAT中的网络来自这个服务代码在一个功能调用的结尾:公共位置getLocation()....我正在从BroadcastReceiver调用此服务。
为什么此服务卡在网络消息上?
答案 0 :(得分:1)
看起来你永远不会突破while(true)
,所以你一遍又一遍地呼叫Location h=getLocation();
。它有点难以阅读您的代码,但如果您未在success==1
中获得onStart()
那么这可以解释您的问题