智能移动设备软件开发复习-第四天

写在前面:仅供参考,一般是自己留着看的,如有错误,敬请指正。
这次的更新接着上次的博客,智能移动设备软件开发复习-第三天

第6章 BroadcastReceiver(广播接收者)
  • 广播接收者的创建
    public class MyReceiver extends BroadcastReceiver{
    }
    @Override
    public void onReceive(Context context, Intent intent) {
    	throw new UnsupportedOperationException("Not yet implemented");
    }
    
  • 广播接收者的注册
    • 静态注册
      <receiver
      	android:name=".MyReceiver"
      	android:enabled="true"
      	android:exported="true" >
      </receiver>
      
    特点:这种静态注册的特点是无论应用程序是否处于运行状态,广播接收者都会对程序进行监听。API26之后不支持静态注册
    • 动态注册
      @Override
      protected void onCreate(Bundle savedInstanceState){
      	super.onCreate(savedInstanceState);
      	//实例化广播接收者
      	MyReceiver receiver = new MyReceiver();
      	String action = "action";  //action要与发送时的action一致
      	IntentFilter intentFilter = new IntentFilter(action);
      	registerReceiver(receiver,intentfilter);
      }
      @Override
      protected void onDestroy(){
      	super.onDestroy();
      	unregisterReceiver(receiver);
      }
      
  • 自定义广播的发送与接收
    • 自定义广播的发送
      public void send(View view){
      	Intent intent = new Intent();
      	Intent.setAction("Help_stitch");
      	//发送广播
      	sendBroadcast(intent);
      }
      
    • 自定义广播的接收
      public class MyBroadcastReceiver extends BroadcastReceiver {
      	@Override
      	public void onReceive(Context context.Intent intent){
      		Log.i("MyBroadcastyReceiver", "自定义的广播接收者,接收到了求救广播事件");
      		Log.i("MyBroadcastReceiver", intent.getAction());
      	}
      }
      
      <action android:name="Help_Stitch"/>
      
  • 有序广播与无序广播
    • 无序广播
      sendBroadcast(intent);
      
      无序广播效率高,但无法被拦截
    • 有序广播
      intent.setAction("Intercept_Stitch");
      sendOrderedBroadcast(intent,null);
      
      有序广播效率低,但此类型是有先后顺序的,可以被拦截
    • 拦截有序广播
      abortBroadcast();//拦截有序广播
      
第7章 Service(服务)
  • 服务的创建
    public class Myservice extends Service{
    	public Myservice(){
    	}
    	@Override
    	public IBinder onBind(Intent intent){
    		Log.i("EXAMPLE","--OnBind()");
    		return new CountBinder();
    	}
    }
    
  • 服务的生命周期
    • onCreate:第一次创建服务时执行的方法
    • onDestroy():服务被销毁时执行的方法;
    • onStartCommand:客户端通过调用startService()方法启动服务时执行该方法;
    • onBind:客户端通过调用BindService()方法启动服务时执行该方法;
    • onUnbind():客户端调用unBindService()断开服务绑定时执行的方法。
      通过startService()方法启动服务时,执行的生命周期方法为onCreate()、onStartCommand(),然后服务处于运行状态,知道自身调用stopself()方法或者其他组件调用stopService()方法时服务停止,最终被系统销毁。
      当使用bindService()方法启动服务时,执行的生命周期方法为onCreate()、onBind(),然后服务处于运行状态,直到调用unBindService()方法时,服务被解绑调用onUnbind()方法最终被销毁。
  • 服务的启动方式
    • startService方式启动
      public void start(View view){
      	Intent intent = new Intent(this,MyService.class);
      	startService(intent);
      }
      public void stop(View view){
      	Intent intent - new Intent(this,MyService.class);
      	stopService(intent);
      }
      
      注:startService开启服务以后,与activity就没有关联,不受影响,独立运行。
    • bindService方式启动
      public class MainActivity extends AppCompatActivity{
      	private MyService.MyBinder myBinder;
      	private Myconn myconn;
      	protected void onCreate(Bundle savedInstanceState){
      		super.onCreate(savedInstanceState);
      		setContentView(R.layout.activity_main);
      	}
      	public void Bind(View view){
      		if(myconn == null){
      			myconn = new Myconn();
      		}
      		Intent intent = new Intent(this,MyService.class);
      		bindService(intent, myconn, BIND_AUTO_CREATE);
      	}
      	public void onUnBind(View view){
      		if(myconn != null){
      			unBindService(myconn);
      			myconn = null;
      		}
      	}
      	public void Call(View view){
      		myBinder.callMethodInService();
      	}
      	private class MyConn implements ServiceConnection{
      		public void onServiceConnected(ComponentName name,IBinder service){
      			myBinder = (MyService.MyBinder) service;
      			Log.i(" MainActivity","服务成功绑定,内存地址为:" + myBinder.toString());
      		}
      		public void onServiceDisconnected(ComponentName name){
      		}
      	}
      	}
      }
      
      注: bindService开启服务以后,与activity存在关联,退出activity时必须调用unbindService方法,否则会报ServiceConnection泄漏的错误。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值