后台服务正在崩溃"启动服务"

时间:2015-12-16 05:33:49

标签: android

我必须找到用户的位置,即使我关闭应用程序,它仍然在后台继续。代码工作正常,它显示正确的位置,但当我点击"启动服务"它崩溃了。我找不到错误: GPS跟踪课程

package com.malay.gpsservice;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
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.util.Log;
import android.widget.Toast;

public class GPSTracService extends Service implements LocationListener {

// DECLARE ALL THE VARIABLES
Location location = null; // location
LocationManager locationManager;
boolean isGPSEnabled = false;
boolean isNetworkEnabled=false;

String msg = "CAUTION! If kept open, can consume lots of battery";
// FOR FOREGROUND_ID
int FORE_ID = 1335;

double latitude; // latitude
double longitude; // longitude

// The minimum distance to change Updates in meters
final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@SuppressLint("NewApi")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    Intent noty_intent = new Intent(this,
            com.malay.gpsservice.MainActivity.class);
    noty_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, noty_intent,
            0);

    Notification n = new Notification.Builder(this)
            .setContentTitle("GPS Serice is running...")
            .setContentText(msg).setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent).setAutoCancel(true).setOngoing(true)
            .build();

    startForeground(FORE_ID, n);

    try {
        locationManager = (LocationManager) this
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        isNetworkEnabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

         // First get location from Network Provider
        if(isNetworkEnabled){
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
            Log.d("Network","Network");
            if(locationManager!=null){
                location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                if(location!=null){
                    latitude=location.getLatitude();
                    longitude=location.getLongitude();
                }
            }
        }

        if (!isGPSEnabled) {
        } else {
            // 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);

                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        Toast.makeText(this,
                                "Location Listener on GPS started...",
                                Toast.LENGTH_SHORT).show();
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
        Toast.makeText(this,
                "Some Error occur while starting Location Listener",
                Toast.LENGTH_SHORT).show();
        ex.printStackTrace();
    }
    return (START_STICKY);
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    if (locationManager != null) {
        locationManager.removeUpdates((LocationListener) this);
        locationManager = null;
    }
    stopForeground(true);
    Toast.makeText(this, "Location Listener on GPS Stopped...",
            Toast.LENGTH_SHORT).show();
    super.onDestroy();
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}
}

这是主要活动:

package com.malay.gpsservice;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

 public class MainActivity extends Activity implements LocationListener{

Context mcontext=this;

boolean isGpsEnabled=false;
boolean isNetworkEnabled=false;
boolean canGetLocation=false;

protected LocationManager locManager;
Location loc=null;
double latitude;
double longitude;
double accuracy;

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATE=20;
private static final long MIN_TIME_BETWEEN_UPDATE=1000*60;

double latitudet;
double longitudet;
double accuracyt;
TextView tview;
TextView tlat;
TextView tlon;
TextView tacc;
String lat;
String lon;
String acc;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    loc=getLocation();
    tview = (TextView) findViewById(R.id.textView1);

    try {
        getGPSLoc();
    } catch (Exception e) {
        e.printStackTrace();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public boolean onMenuItemSelected(int featureId, MenuItem item) {
    //Refresh Button
    if (item.getItemId() == R.id.ref) {
        getLocation();
        getGPSLoc();
    }
    return super.onMenuItemSelected(featureId, item);
}

public void getGPSLoc() {
    if (latitude != 0.0 && longitude != 0.0) {
        tview.setText("Location Fixed");
    } else {
        tview.setText("Waiting for Location...");
    }
if (canGetLocation) {
    latitudet=(double)Math.round(latitude*1000000)/1000000;
    tlat = (TextView) findViewById(R.id.lat);
    lat = Double.toString(latitudet);
    tlat.setText(lat);

    longitudet=(double)Math.round(longitude*1000000)/1000000;
    tlon = (TextView) findViewById(R.id.lon);
    lon = Double.toString(longitudet);
    tlon.setText(lon);

    accuracyt=(double)Math.round(accuracy*100)/100;
    tacc = (TextView) findViewById(R.id.acc);
    acc = Double.toString(accuracyt);
    tacc.setText(acc);
}
}

public Location getLocation(){
    try{
        locManager=(LocationManager)mcontext.getSystemService(LOCATION_SERVICE);

        isGpsEnabled=locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        isNetworkEnabled=locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if(isGpsEnabled || isNetworkEnabled){
            this.canGetLocation=true;

             // First get location from Network Provider
            if(isNetworkEnabled){
                locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BETWEEN_UPDATE, MIN_DISTANCE_CHANGE_FOR_UPDATE, (LocationListener) this);
                Log.d("Network","Network");
                if(locManager!=null){
                    loc=locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if(loc!=null){
                        latitude=loc.getLatitude();
                        longitude=loc.getLongitude();
                        accuracy=loc.getAccuracy();
                    }
                }
            }

            // if GPS Enabled get lat/long using GPS Services
            if(isGpsEnabled){
                locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BETWEEN_UPDATE, MIN_DISTANCE_CHANGE_FOR_UPDATE, (LocationListener) this);
                Log.d("GPS Enabled","GPS Enabled");
                if(locManager!=null){
                    loc=locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if(loc!=null){
                        latitude=loc.getLatitude();
                        longitude=loc.getLongitude();
                        accuracy=loc.getAccuracy();
                    }
                }
            }
        }
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return loc;
}

public void startServ(View vw){
    Intent gps=new Intent(this,GPSTracService.class);
    this.startService(gps);
}

public void stopServ(View vw){
    Intent gps=new Intent(this,GPSTracService.class);
    this.stopService(gps);
}


@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}


@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}


@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}


@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}
}

清单文件

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.malay.gpsservice.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>
     <service android:name=".GPSTracService" />
  </application>

   </manifest>

Logcat

1 个答案:

答案 0 :(得分:1)

在服务中使用getApplicationContext()代替this

更改为以下行。

Intent noty_intent = new Intent(getApplicationContext(), com.malay.gpsservice.MainActivity.class);
.
.

Notification n = new Notification.Builder(getApplicationContext())

而不是

Intent noty_intent = new Intent(this, com.malay.gpsservice.MainActivity.class);

注意:必须在menifest.xml中声明服务,如果它丢失了。

更新:

使用以下代码进行通知

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
                .setStyle(new NotificationCompat.BigTextStyle().bigText("App Name"))
                .setContentText("App Name");
startForeground(1, mBuilder.build());

使用 android.support.v7 库并设置目标版本 21