无法启动服务意图{cmp = com.appstech.knowntrack/.MyLocationService} U = 0:找不到 [英] Unable to start service Intent { cmp=com.appstech.knowntrack/.MyLocationService } U=0: not found

查看:121
本文介绍了无法启动服务意图{cmp = com.appstech.knowntrack/.MyLocationService} U = 0:找不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appstech.knowntrack">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@drawable/app"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.DayNight.NoActionBar">


<activity
    android:name=".Login"
    android:screenOrientation="portrait"
    android:label="@string/title_activity_login">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<Service android:name=".MyLocationService"></Service> 
</application>

服务标签:

<Service android:name=".MyLocationService"></Service> 

我在这里尝试使用."没有 "."和完整的软件包名称,例如"com.appstech.knowntrack.MyLocationService",但没有任何效果.

Here i have tried with "." without "." and aslo full package name like "com.appstech.knowntrack.MyLocationService" But nothing is working.

服务等级:

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;

 import com.google.android.gms.maps.model.LatLng;

public class MyLocationService extends Service {
private static final String TAG = "MyLocationService";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 3000;
private static final float LOCATION_DISTANCE = 10f;

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);
    Toast.makeText(this, "MyService Started.", Toast.LENGTH_SHORT).show();
    return START_STICKY;
}

@Override
public void onCreate() {
    Log.e(TAG, "onCreate");
    initializeLocationManager();
    try {
        mLocationManager.requestLocationUpdates(
                LocationManager.NETWORK_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, "network provider does not exist, " + ex.getMessage());
    }
    try {
        mLocationManager.requestLocationUpdates(
                LocationManager.GPS_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, "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 {
                if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                mLocationManager.removeUpdates(mLocationListeners[i]);
            } catch (Exception ex) {
                Log.i(TAG, "fail to remove location listners, ignore", ex);
            }
        }
    }
}

private void initializeLocationManager() {
    Log.e(TAG, "initializeLocationManager");
    if (mLocationManager == null) {
        mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    }
}

// create LocationListener class to get location updates
private class LocationListener implements android.location.LocationListener
{
    Location mLastLocation;
    double latitude;
    double longitude;
    LatLng latLngcurrent;

    public LocationListener(String provider)
    {
        Log.e(TAG, "LocationListener " + provider);
        mLastLocation = new Location(provider);
    }

    @Override
    public void onLocationChanged(Location location)
    {
        Log.e(TAG, "onLocationChanged: " + location);
        mLastLocation.set(location);
        latitude = location.getLatitude();
    longitude = location.getLongitude();
        Store.latu=latitude;
        Store.longu=longitude;
    latLngcurrent = new LatLng(location.getLatitude(), location.getLongitude());

    }

    @Override
    public void onProviderDisabled(String provider)
    {
        Log.e(TAG, "onProviderDisabled: " + provider);
    }

    @Override
    public void onProviderEnabled(String provider)
    {
        Log.e(TAG, "onProviderEnabled: " + provider);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
        Log.e(TAG, "onStatusChanged: " + provider);
    }
   }
 }

意图:我已经在onCreate中调用了意图

Intent: i have called the intent inside onCreate

Intent intent = new Intent(MapsActivity.this, MyLocationService.class);
startService(intent);

Intent serviceIntent  = new Intent(context , MyLocationService.class);
context.startService(serviceIntent);

即使我在意图中给出了程序包名称.我在日志猫中收到错误,例如" W/ActivityManager:无法启动服务意图{cmp = com.appstech.knowntrack/.MyLocationService} U = 0:找不到"摆脱这个.预先感谢.

Even i gave package name inside the intent. I am getting the error in the log cat like " W/ActivityManager: Unable to start service Intent { cmp=com.appstech.knowntrack/.MyLocationService } U=0: not found " Help me out to get out of this. Thanks in advance.

推荐答案

您应该尝试使用<service>而不是<Service>

You should try with <service> instead of <Service>

这篇关于无法启动服务意图{cmp = com.appstech.knowntrack/.MyLocationService} U = 0:找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆