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");
}
}
第二种方法还可以用于跨进程通信