组件必须在清单文件里面注册
Activity跳转
新建Activity 必须加到清单文件
设置按钮 跳转
public void startActivity(View view){
startActivity(name Intent(this,MainActivity2));
}
生命周期
认识Sevice
服务在后台默默的运行 , 是不可见的
生命周期
onCreat
onStart
onStartCommand
onDestory
startService 与 生命周期
启动服务
startService(new Intent(this,MyService.class));
会后台继续运行
停止服务
stopService(new Intent(this,MyService.class));
bindService 与 生命周期
直接与 activity 挂钩
生命周期
绑定Activity
BIND_AUTO_CREATE 连接方式
bindService(new Intent(this,MyService.class),connect,Context.BIND_AUTO_CREATE)
解绑Activity
unbindService(connection)
创建桥梁
private ServiceConnection connection = new ServiceConnection(){
@override
public void onServiceConnection(ComponentName name,IBinder service){
}
@override
public void onServiceDisconnected)(ComponentName name){
}
}
认识 Receive
广播:分为系统广播,与用户自定义广播
静态注册接受广播
创建标记 不能错误
<receive android:name = "com.derr.project__">
<intent-filter>
<action android: name "com.derr.project__"/>
</intent-filter>
</receive>
创建广播类
接收者
public class CustomReceive extends BroadcastReceive{
@Override
public void onReceive(Contesxt context , Initent intent){
}
}
activity 发送给接收者
public void sendAction2(View view){
Intent intent = new Intent();
// ActionUtils.ACTION_EQUES_UPDATE_IP 与注册时保持一致
intent.setAction(ActionUtils.ACTION_EQUES_UPDATE_IP);
sendBroadcast(intent);
}
动态注册广播
不需要再清单文件里面 注册
广播注册时 与 发送广播时 的 唯一标识,必须要保持一直(给动态注册使用)
String ACTION_EQUES_UPDATE_IP = "标志"
定义广播接收者
public class CustomReceive extends BroadcastReceive{
@Override
public void onReceive(Contesxt context , Initent intent){
}
}
Java 代码来注册刚刚的接收者即可
在oncreat注册广播(订阅)
动态使用Java代码注册一个广播接收者
UpdataIpSelectCity updataIpSelectCity = new UpdateIpSelectCity();
IntentFilter filter = new IntentFilter();
filter.addAction(ActionUtils.Action_EQUES_UPDATA_IP);
registerReceive(updataIpSelectCity,filter);
filter n. 过滤器;滤波器;筛选程序;分流指示灯
intent n. 意图
发送广播给接收者
public void sendAction1(View view){
Intent intent = new Intent();
intent.secAction(ActionUtils.ACTION_EQUES_UPDATE_IP);
sendBroadcast(intent);
}
broadcast vt. 播送,播放;(无线电或电视)广播;.
原文:https://www.cnblogs.com/AronJudge/p/14652559.html