Android启动服务的两种方法

Android启动服务的两种方法

Android 5.0开始,只能使用显式Intent启动服务。其中,启动服务有2种方式,第一种是直接指定想要启动的服务;第二种是使用AIDL的方式。
代码中主活动名是MainActivity,服务名是MyService

第一种方法

这种方法和启动活动的方法比较相似,直接指定了想要启动的服务
MainActivity.java代码

package cn.edu.jssvc.crossprocesscommunication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
    Intent intent = new Intent(MainActivity.this, MyService.class);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);        
        startService(intent);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(intent);
    }
}

MyService.java代码

package cn.edu.jssvc.crossprocesscommunication;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
    public MyService() {
    }
    @Override
    public void onCreate() {
        System.out.println("Myservice created");
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("Myservice startCommand");
        return super.onStartCommand(intent, flags, startId);
    }
    @Override
    public void onDestroy() {
        System.out.println("Myservice destroy");
        super.onDestroy();
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

第二种方法

使用Intent.setComponent(new ComponentName(para1,para2))
para1是服务所在的包名,para2是包名.服务名

MainActivity.java代码

package cn.edu.jssvc.crossprocesscommunication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    Intent intent = new Intent();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intent.setComponent(new ComponentName("cn.edu.jssvc.crossprocesscommunication","cn.edu.jssvc.crossprocesscommunication.MyService"));
        startService(intent);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(intent);
    }
}

MyService.java代码

package cn.edu.jssvc.crossprocesscommunication;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
    public MyService() {
    }
    @Override
    public void onCreate() {
        System.out.println("Myservice created");
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("Myservice startCommand");
        return super.onStartCommand(intent, flags, startId);
    }
    @Override
    public void onDestroy() {
        System.out.println("Myservice destroy");
        super.onDestroy();
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

第二种方法还可以用于跨进程通信

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值