0%

页面&服务

Intent、Service。

意图-Intent

Intent的中文名是意图,意思是我想让你干什么,简单地说就是传递消息。

Intent是各个组件之间信息沟通的桥梁,既能在Activity之间沟通,又能在Activity与Service之间沟通,也能在Activity与Broadcast之间沟通。总而言之,Intent用于处理Android各组件之间的通信。

跳转

Activity间的跳转

1
2
Intent intent = new Intent(当前类.this,目标类名.class);
startActivity(intent);

传递消息

Intent用于处理Android各组件之间的通信,需要完成以下3个步骤:

  1. Intent需标明本次通信请求从哪来、到哪去、要怎么走。
  2. 发起方携带本次通信需要的数据内容,接收方对收到的Intent数据进行解包。
  3. 如果发起方要求接收方的处理结果,Intent就要负责让接收方传回应答的数据内容。

显式Intent

显式Intent,直接指定来源类与目标类名,属于精确匹配。

在声明一个Intent对象时,需要指定两个参数,第一个参数表示跳转的来源页面,第二个参数表示接下来要跳转到的页面类。

隐式Intent

隐式Intent,没有明确指定要跳转的类名,只给出一个动作让系统匹配拥有相同定义的目标,属于模糊匹配。

不希望直接暴露源码的类名,只给出一个事先定义好的名称,所以隐式Intent起到了过滤作用。这个定义好的动作名称是一个字符串,可以是自己定义的动作,也可以是已有的系统动作。

创建Intent实例

创建Intent实例三种方式

  1. 在构造函数中指定

    1
    Intent intent = new Intent(当前类.this,目标类名.class);//创建一个目标确定的意图
  2. 调用setClass方法指定

    1
    2
    Intent intent = new Intent();//创建一个新意图
    intent.setClass(当前类.this,目标类名.class);//设置意图要跳转的活动类
  3. 调用setComponent方法指定

    1
    2
    3
    Intent intent = new Intent();//创建一个新意图
    ComponentName component = new ComponentName(当前类.this,目标类名.class);
    intent.setComponent(component);//设置意图 携带的组件信息

代码示例

不携带数据

携带数据

携带数据且有返回

第2、3方式:

Service

service服务,在后台运行。看不见。

继承结构图:

服务启动

生命周期

总结:

  • startService(),启动服务
    • 所以要执行的操作放在重写Service子类的onStartCommand方法中。(Intent对象传递数据)
  • bindService(),绑定服务
    • onBind方法中

代码演示

准备

新建一个Service类。

重写下边的几个方法,观察service生命周期。

1
2
onBind、onCreate、onStartCommand、onUnbind、onDestroy
绑定、创建、启动、解绑、销毁

日志

观察service生命周期,可以在重写的各个方法中添加日志调试语句:

Log.i("TAG","MyBindService onBind");

绑定服务

重写Service子类。创建类MyBindService并继承Service。要执行的操作放在重写Service子类的onBind方法中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.example.myapplication;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

//绑定服务
public class MyBindService extends Service {
public MyBindService() {
}

@Override
public IBinder onBind(Intent intent) {
System.out.println("服务,被绑定!!!");
Log.i("TAG","MyBindService onBind");
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
return null;
}
@Override
public void onCreate() {
System.out.println("服务,被创建!!!");
Log.i("TAG","MyBindService onCreate");
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("服务,被启动!!!");
Log.i("TAG","MyBindService onStartCommand");

return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
System.out.println("服务,被销毁!!!");
Log.i("TAG","MyBindService onDestroy");
super.onDestroy();
}

@Override
public boolean onUnbind(Intent intent) {
System.out.println("服务,被解绑!!!");
Log.i("TAG","MyBindService onUnbind");
return super.onUnbind(intent);
}

}

启动服务

重写Service子类。创建类MyStartService并继承Service。要执行的操作放在重写Service子类的onStartCommand方法中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.example.myapplication;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

//启动服务
public class MyStartService extends Service {
public MyStartService() {
}

@Override
public IBinder onBind(Intent intent) {
System.out.println("服务,被绑定!!!");
Log.i("TAG","MyStartService onBind");
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
return null;
}

@Override
public void onCreate() {
System.out.println("服务,被创建!!!");
Log.i("TAG","MyStartService onCreate");
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("服务,被启动!!!");
Log.i("TAG","MyStartService onStartCommand");
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
System.out.println("服务,被销毁!!!");
Log.i("TAG","MyStartService onDestroy");
super.onDestroy();
}

@Override
public boolean onUnbind(Intent intent) {
System.out.println("服务,被解绑!!!");
Log.i("TAG","MyStartService onUnbind");
return super.onUnbind(intent);
}
}

使用

绑定服务

  • 创建连接对象ServiceConnection

  • 创建意图Intent对象

    • Intent intent = new Intent(this,MyBindService.class);
    • MyBindService为前边重写的Service子类
  • 绑定服务

    • bindService(intent,connection,BIND_AUTO_CREATE);//绑定服务
    • connection为连接对象
  • 解绑服务

    • unbindService(connection);//解绑

创建连接对象:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 private MyServiceConnection connection;

//初始化 连接对象
if(connection == null){
connection = new MyServiceConnection();
}

//写一个内部类,用于连接(启动)服务(测试生命周期)
class MyServiceConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
//建立连接
System.out.println("建立连接对象");
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
//断开连接、
System.out.println("断开连接(对象)");
}
}

创建意图Intent对象、绑定服务、解绑服务:

1
2
3
4
Intent intent = new Intent(this,MyBindService.class);
bindService(intent,connection,BIND_AUTO_CREATE);//绑定服务

unbindService(connection);//解绑

启动服务

启动:

  • 创建意图Intent对象
    • Intent intent1 = new Intent(this,MyStartService.class);
  • 启动服务
    • startService(intent1);//启动服务

停止:

  • 创建意图Intent对象
    • Intent intent2 = new Intent(this,MyStartService.class);
    • MyStartService为前边重写的Service子类
  • 停止服务
    • stopService(intent2);//停止服务

完整源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.example.myapplication;

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;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private MyServiceConnection connection;

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

findViewById(R.id.bind).setOnClickListener(this);
findViewById(R.id.unbind).setOnClickListener(this);
findViewById(R.id.start).setOnClickListener(this);
findViewById(R.id.stop).setOnClickListener(this);
findViewById(R.id.finish).setOnClickListener(this);
//初始化 连接对象
if(connection == null){
connection = new MyServiceConnection();
}

}

@Override
public void onClick(View view) {
switch (view.getId()){
//绑定服务
case R.id.bind:
Intent intent = new Intent(this,MyBindService.class);
bindService(intent,connection,BIND_AUTO_CREATE);//绑定服务
break;
case R.id.unbind:
unbindService(connection);//解绑
break;
case R.id.finish:
this.finish();//退出程序
break;
//启动服务
case R.id.start:
Intent intent1 = new Intent(this,MyStartService.class);
startService(intent1);//启动服务
break;
case R.id.stop:
Intent intent2 = new Intent(this,MyStartService.class);
stopService(intent2);//停止服务
break;
}

}

//写一个内部类,用于连接(启动)服务(测试生命周期)

class MyServiceConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
//建立连接
System.out.println("建立连接对象");
}

@Override
public void onServiceDisconnected(ComponentName componentName) {
//断开连接、
System.out.println("断开连接(对象)");
}
}
}

运行结果

由运行结果,可得到下图的生命周期流程图:

若图片不能正常显示,请在浏览器中打开

欢迎关注我的其它发布渠道