在那之前我们先写好我们的service
MyService
public class MyService extends Service {
private static final String TAG = "ssd";
public MyService() {
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate: ");
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy: ");
}
}
进入正题
通过添加action标签启动
直接在AndroidManifest.xml中找到需要启动的service->找到<intent-filter>->添加action标签然后调用就可以了
但这个方法从android5.0以后已经没法用了只能使用显示intent启动服务,在这里就不多说了
反正打完也是错的偷个懒
通过AIDL启动
启动服务
1.我们新建一个Module
2.我们先给两个app的mainActiaity写一样的代码
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开启"
android:onClick="btn_1"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭"
android:onClick="btn_2"/>
</LinearLayout>
MainActivity
package com.hrk.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btn_1(View view) {
startService(new Intent(this,MyService.class));
}
public void btn_2(View view) {
stopService(new Intent(this,MyService.class));
}
}
3.运行一下
My Application肯定是可以看到
但是My Application2肯定是没有反应的
那我们怎么让My Application2启动My Application中的MyService呢?
4.最简单的在OnCeate加上这两行代码
intent = new Intent();
intent.setComponent(new ComponentName("com.hrk.myapplication","com.hrk.myapplication.MyService"));
intent茫茫多的方法中有一个setComponen方法他专门是用来调用本app以外的应用。
而setComponen需要传递一个ComponentName对象,这个对象的第一个参数是被打开APP的包名,第二个参数是ShowImageActivity这个页面所在的全路径
全路径获取方式如下
接下来我们试一下绑定服务
对生命周期和绑定不熟可以先看我上一篇
我们知道如果我们是无法从其他应用程序中访问另一个应用程序中类的定义那我们怎么访问呢?
往下看
1.我们先重写一遍我们的service生命周期的所有方法方便观察(不写也行。。。)
2.android给我们提供了一种方法AIDL那我们就使用一下新建一个AIDL文件
可以看到系统为我们自动生成了一个类
3.在onbind中返回新生成的类
如果像下面一样可以rebuild一下
结果如下
4.然后我们改一下My Application2
MainActivity
package com.hrk.myapplication2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Intent intent;
private ServiceConnection serviceConnection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent();
intent.setComponent(new ComponentName("com.hrk.myapplication","com.hrk.myapplication.MyService"));
serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Toast.makeText(MainActivity.this,String.valueOf(service),Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
}
public void btn_1(View view) {
startService(intent);
}
public void btn_2(View view) {
stopService(intent);
}
public void btn_3(View view) {
bindService(intent,serviceConnection,BIND_AUTO_CREATE);
}
public void btn_4(View view) {
unbindService(serviceConnection);
}
}
xml就不放上来了
我们运行一下看看结果
手机上夜能正常弹出toast(这里就是试一下能不能弹出)
实现通信
1.我们先在Service中加一个num
通过num的值观察结果判断是否能够通信
private int num = 0;
2.AIDL_MyService.aidl
我们再添加一个方法用来修改num
// AIDL_MyService.aidl
package com.hrk.myapplication;
// Declare any non-default types here with import statements
interface AIDL_MyService {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
void setnum(int newnum);
}
没有报错的rebuild一下
3.setnum我们把这个抽象方法写一下输出修改前后的num看看是否改变
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind: ");
return new AIDL_MyService.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public void setnum(int newnum) throws RemoteException {
Log.i(TAG, "num: "+MyService.this.num);
MyService.this.num = newnum;
Log.i(TAG, "newnum: "+MyService.this.num);
}
};
}
4.我们要让My Application实现通信
怎么实现呢,通信需要相同的『语言』也就是相同的接口那么我们把My Application2中的AIDL_MyService拿过来
注意要原封不动的拿过来要有相同的包名
这是My Application中的样子那么我们要保持在My Application2中也是这样
创建aidl并且保持包名也要一样
5.获取传过来的IBinder service
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Toast.makeText(MainActivity.this,String.valueOf(service),Toast.LENGTH_SHORT).show();
aidl_myService = (AIDL_MyService) service;
}
6.写一个新按钮调用aidl_myService的setnum方法
public void btn_5(View view) throws RemoteException {
aidl_myService.setnum(1);
}
7.试一下结果
为什么呢看了半天我发现是我们的强制类型转换错了。。。。。
虽然两个都是转AIDL_MyService但是实际上因为传过来的值他传过来的是之前那个My Application中的类而转换要转成我们这个My Application中的类型虽然看起来一样但是占的内存是不同的所以会出现错误,那咋办呢?
8.类型转换
android有给出方法我们这么写
aidl_myService = AIDL_MyService.Stub.asInterface(service);
ok结果出来了
代码如下
MyService
package com.hrk.myapplication;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class MyService extends Service {
private static final String TAG = "ssd";
private int num = 0;
public MyService() {
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate: ");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand: ");
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind: ");
return new AIDL_MyService.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public void setnum(int newnum) throws RemoteException{
Log.i(TAG, "num: "+MyService.this.num);
MyService.this.num = newnum;
Log.i(TAG, "newnum: "+MyService.this.num);
}
};
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "onUnbind: ");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy: ");
}
}
My Application2中的
MainActivity
package com.hrk.myapplication2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.Toast;
import com.hrk.myapplication.AIDL_MyService;
public class MainActivity extends AppCompatActivity {
private AIDL_MyService aidl_myService;
private Intent intent;
private ServiceConnection serviceConnection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent();
intent.setComponent(new ComponentName("com.hrk.myapplication","com.hrk.myapplication.MyService"));
serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Toast.makeText(MainActivity.this,String.valueOf(service),Toast.LENGTH_SHORT).show();
aidl_myService = AIDL_MyService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
}
public void btn_1(View view) {
startService(intent);
}
public void btn_2(View view) {
stopService(intent);
}
public void btn_3(View view) {
bindService(intent,serviceConnection,BIND_AUTO_CREATE);
}
public void btn_4(View view) {
unbindService(serviceConnection);
}
public void btn_5(View view) throws RemoteException {
aidl_myService.setnum(1);
}
}