Java 在强制关闭并使用Android重新启动时重新启动服务

Java 在强制关闭并使用Android重新启动时重新启动服务,java,android,service,broadcastreceiver,manifest,Java,Android,Service,Broadcastreceiver,Manifest,我实际上正在开发一个Android应用程序,允许家长跟踪他的孩子。我面临的问题是,GPS定位需要每N秒执行一次,但每当我强制关闭应用程序或重新启动移动设备时,服务都不会重新启动 这是我的位置服务: public class LocationService extends Service { private LocationListener listener; private LocationManager manager; @Nullable @Override

我实际上正在开发一个Android应用程序,允许家长跟踪他的孩子。我面临的问题是,GPS定位需要每N秒执行一次,但每当我强制关闭应用程序或重新启动移动设备时,服务都不会重新启动

这是我的位置服务:

public class LocationService extends Service
{
    private LocationListener listener;
    private LocationManager manager;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        Toast.makeText(this, "Service.onCreate()", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        Toast.makeText(this, "Service.destroy()", Toast.LENGTH_LONG).show();
        if(this.manager != null)
            //noinspection MissingPermission
            this.manager.removeUpdates(this.listener);
    }

    @Override
    public int onStartCommand(Intent intent, int flag, int startId)
    {
        super.onStartCommand(intent, flag, startId);

        //Here end the code to execute
        Toast.makeText(this, "Service.onStartCommand()",Toast.LENGTH_LONG).show();

        this.listener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Log.e("started", "service running");
                play();
                Toast.makeText(LocationService.this, location.getLongitude() + " " + location.getLatitude(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        };

        this.manager = (LocationManager)getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

        //noinspection MissingPermission
        this.manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000,0, this.listener);

        return START_NOT_STICKY;
    }

    @Override
    public boolean onUnbind(Intent intent)
    {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Service.onUnbind()", Toast.LENGTH_LONG).show();
        return super.onUnbind(intent);
    }
}
我的广播接收器:

public class BootIntentReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Intent serviceIntent = new Intent(context, LocationService.class);
        serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startService(serviceIntent);
    }
}
My Manifest.xml:

<receiver android:name=".activity.service.BootIntentReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>
<service
    android:name=".activity.service.LocationService"
    android:exported="false" />

启动的权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

无论何时启动设备,都不会发生任何事情:/


谢谢你们的帮助,伙计们

您是否尝试在清单中为您的服务使用完全限定的包名

e、 g.如果服务在包
com.package.activity.service
中,请使用
而不仅仅是


然后在你的
BootIntenternetReceiver
中加入日志或祝酒词,以确保它确实有效!思想被吹走了!我花了好几个小时试图寻找一个错误,但那只是..哈哈。无论如何非常感谢你!还有,你知道为什么一段时间后它会停止吗?