终止服务android无法正常工作

时间:2014-10-26 23:22:43

标签: java android android-service

我目前正在尝试实施一项运行服务的应用,直到用户通过应用程序明确结束服务。

即使退出应用程序,服务也会运行(这没关系)

enter image description here

但是当我再次进入应用程序并尝试停止服务时,应用程序就会停止运行。

我做错了什么?

这是服务代码

    package com.mac.service;

    import android.app.Service;
    import android.content.Intent;
    import android.location.Criteria;
    import android.location.Location;
    import android.location.LocationManager;
    import android.os.Handler;
    import android.os.IBinder;
    import android.widget.Toast;

    public class MyService extends Service {

        private Handler handler = new Handler();
        private long timeAndSpace = 5000;
        Thread t;

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

        @Override
        public void onCreate() {
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            handler.removeCallbacksAndMessages(null);
            Toast.makeText(this, "Service Stopped ...", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStart(Intent intent, int startid) {
        }

        public int onStartCommand(Intent intent, int flags, int startId) {

            Toast.makeText(getBaseContext(), "Service Started", Toast.LENGTH_SHORT)
                    .show();

            final Runnable r = new Runnable() {
                public void run() {
                    Location l = getMine();
                    Toast.makeText(
                            getApplicationContext(),
                            String.valueOf(l.getLatitude()) + " "
                                    + String.valueOf(l.getLongitude()),
                            Toast.LENGTH_SHORT).show();
                    handler.postDelayed(this, timeAndSpace);
                }
            };
            handler.postDelayed(r, timeAndSpace);
            return START_STICKY;
        }

        private Location getMine() {
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            Criteria criteria = new Criteria();
            String provider = locationManager.getBestProvider(criteria, true);
            Location myLocation = locationManager.getLastKnownLocation(provider);
            return myLocation;
        }

    }

这就是清单

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

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="19" />

    <uses-permission android:name="com.mac.maps2demo.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <!-- Requires OpenGL ES version 2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

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

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />


        <service
            android:name="com.mac.service.MyService"
            android:enabled="true"
            android:exported="true" />
    </application>

</manifest>

0 个答案:

没有答案