启动和停止Service

本文将介绍如何在Android系统中正确地启动和停止Service,包括详细的步骤和注意事项,帮助开发者更好地管理应用程序的后台服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.mystudy.kibi.networktype;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.List;

/**
 * 程序使用Activity作为Service的访问者,该Activity的界面包含两个按钮,
 * 一个按钮用于启动Service,一个按钮用于关闭Service.
 */
public class MainActivity extends AppCompatActivity {

    String action = "com.mystudy.kibi.service.TEST_SERVICE";
    Intent intent = new Intent();
    Intent eintent;

    Button start_service_btn;
    Button stop_service_btn;

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

        initDate();
        initView();
        initClik();


    }

    private void initDate(){
        intent.setAction(action);
//        eintent = new Intent(createExplicitFromImplicitIntent(this,intent));
        intent.setPackage(this.getPackageName());
    }

    private void initView(){
        start_service_btn = (Button) findViewById(R.id.start_service_btn);
        stop_service_btn = (Button) findViewById(R.id.stop_service_btn);
    }

    private void initClik(){
        start_service_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity.this.startService(intent);
            }
        });
        stop_service_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity.this.stopService(intent);
            }
        });
    }

//    public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
//        PackageManager pm = context.getPackageManager();
//        List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
//        if (resolveInfo == null || resolveInfo.size() != 1) {
//            return null;
//        }
//        ResolveInfo serviceInfo = resolveInfo.get(0);
//        String packageName = serviceInfo.serviceInfo.packageName;
//        String className = serviceInfo.serviceInfo.name;
//        ComponentName component = new ComponentName(packageName, className);
//        Intent explicitIntent = new Intent(implicitIntent);
//        explicitIntent.setComponent(component);
//        return explicitIntent;
//    }

}

   启动、关闭Service十分简单,调用Context里定义的startService()、stopService()方法即可启动、关闭Service。
   注意:每当Service被创建时会回调onCreat()方法,每次Service被调用都会回调onStartCommand()方法。

在测试过程中发现,Android5.0中Service的Intent一定要显性声明,不然会报 "Service Intent must be explicit"错。

_ ( :3 」 )_ 待续。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值