Android 如何让安卓服务在完成时自行停止?

Android 如何让安卓服务在完成时自行停止?,android,android-service,Android,Android Service,我正在应用程序中实现一个服务。我想在上传任务完成后自行停止服务。我正在尝试检测网络,当wifi或移动网络可用时,开始在服务器上上载数据,完成上载任务后,使用onCreate()方法停止服务本身。当服务启动时,我会记录一条消息,但我不明白为什么即使在调用this.stopSelf()之后,它仍会连续打印停止服务的消息。如何在Android中正确使用服务 这是我的服务代码 public class ServiceTest extends Service { @Override pu

我正在应用程序中实现一个服务。我想在上传任务完成后自行停止服务。我正在尝试检测网络,当wifi或移动网络可用时,开始在服务器上上载数据,完成上载任务后,使用
onCreate()
方法停止服务本身。当服务启动时,我会记录一条消息,但我不明白为什么即使在调用
this.stopSelf()
之后,它仍会连续打印停止服务的消息。如何在Android中正确使用服务

这是我的服务代码

public class ServiceTest extends Service {

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

    @Override
    public void onCreate()
    {
        super.onCreate();
        mTimer = new Timer();
        mTimer.schedule(timerTask, 2000, 15 * 1000);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {

        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.onStartCommand(intent, flags, startId);
    }

    private Timer mTimer;

    TimerTask timerTask = new TimerTask() {

        @Override
        public void run()
        {
            getConnectivityStatusString(getApplicationContext());
        }
    };

    public String getConnectivityStatusString(Context context) {
        int conn = Constant.getConnectivityStatus(context);
        String status = null;
        if (conn == Constant.TYPE_WIFI)
        {
            status = "Wifi enabled";
            Log.e("", " status = " + status);

            ConnectivityManager conMgr =  (ConnectivityManager)context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            {
                NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
                if (netInfo == null)
                {
                    Log.e(" Not "," Accessible !!!");
                }
                else
                {

                    Log.e(" Start ", " To Loading !!!!");
                    //new Welcome_Page().execute();
                    this.stopSelf();
                    Log.e(" Stop ", " The Service !!!!");
                }
            }

        } else if (conn == Constant.TYPE_MOBILE) {
            status = "Mobile data enabled";
            Log.e(""," status = "+status);
            this.stopSelf();
            Log.e(" Stop ", " The Service !!!!");
        } else if (conn == Constant.TYPE_NOT_CONNECTED) {
            status = "Not connected to Internet";
            Log.e(""," status = "+status);
        }
        return status;
    }

    public void onDestroy() {
        try {
            mTimer.cancel();
            timerTask.cancel();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Intent intent = new Intent("com.android.techtrainner");
        intent.putExtra("yourvalue", "torestore");
        sendBroadcast(intent);
    }
}
这是我的BroadcastReceiver类代码

public class ReceiverCall extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        context.startService(new Intent(context, ServiceTest.class));
    }
}
这是我的网络代码

public class Constant
{
    public static int TYPE_WIFI = 1;
    public static int TYPE_MOBILE = 2;
    public static int TYPE_NOT_CONNECTED = 0;


    public static int getConnectivityStatus(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (null != activeNetwork) {
            if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)

                return TYPE_WIFI;

            if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
                return TYPE_MOBILE;
        }
        return TYPE_NOT_CONNECTED;
    }
}

您必须在此onStartCommand方法上返回此START\u NOT\u STICKY

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

    return START_NOT_STICKY;
}
在您可以编写stopSelf()之后随时停止服务的方法。 像这样在menifest文件中添加您的服务

<service
        android:name=".YourService"
        android:enabled="true"
        android:exported="true" >
    </service>

您必须在此onStartCommand方法上返回此START\u NOT\u STICKY

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

    return START_NOT_STICKY;
}
在您可以编写stopSelf()之后随时停止服务的方法。 像这样在menifest文件中添加您的服务

<service
        android:name=".YourService"
        android:enabled="true"
        android:exported="true" >
    </service>

在清单中像这样尝试这些

<service
    android:name=".ServiceTest"
    android:stopWithTask="true" />

在清单中试试这些

<service
    android:name=".ServiceTest"
    android:stopWithTask="true" />

如果您希望服务在执行某些特定任务后自行停止

最好使用IntentService而不是服务类

你可以在这里找到更多信息


如果您希望服务在执行某些特定任务后自行停止,请查找IntentService部分

最好使用IntentService而不是服务类

你可以在这里找到更多信息


查找IntentService部分

仍然服务已启动并打印消息。还有一件事尝试删除onCreate()方法并在onStartCommand方法上调用计时器仍然服务已启动并打印消息。还有一件事尝试删除onCreate()方法并在onStartCommand方法上调用计时器停止服务(新意图(您的_CONTEXT.this,ServiceTest.class))…使用这些并让我知道,以防concern@RavindraKushwaha:我可以把这一行放在ServiceTest.class-stopService(新的意图(您的_CONTEXT.this,ServiceTest.class))中吗无需将其放在服务类中…您只需将这些代码行放在以下位置,即您的操作已完成。即使我的应用程序未处于运行模式或应用程序已退出或未处于打开模式,我也要停止自己。@Ravindra Kushwaha:我正在尝试上载服务中的队列,该服务在internet连接可用时在后台运行。请告诉我是什么时候e我必须把你建议的代码行(stopService(新意图(你的上下文.这个,ServiceTest.类))stopService(新意图(你的上下文.这个,ServiceTest.类))…使用这些代码,并在出现问题时通知我concern@RavindraKushwaha:我可以把这一行放在ServiceTest.class-stopService(新的意图(您的_CONTEXT.this,ServiceTest.class))中吗无需将其放在服务类中…您只需将这些代码行放在以下位置,即您的操作已完成。即使我的应用程序未处于运行模式或应用程序已退出或未处于打开模式,我也要停止自己。@Ravindra Kushwaha:我正在尝试上载服务中的队列,该服务在internet连接可用时在后台运行。请告诉我是什么时候我必须把你建议的代码行(stopService(新的意图(你的上下文this,ServiceTest.class)))