android 广播Broadcast 和 Intent 和 播放器seekbar的总结

一.Intent

1)An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an ActivitybroadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a backgroundService.

Intent是一个将被执行的抽象描述。可以使用startActivity启动一个Activity;也可以发广播到任何感兴趣的BroadcastReceiver组件;也可以通过startService(Intent)或者bindService(Intent,ServiceConnection, int)和后台的service进行通信。

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed

一个Intent为在不同的应用程序间的代码的运行晚绑定提供了一个功能。其最显着的用途是启动Activity,是Activity之间的连接关系。它基本上是一个被动的数据结构,保有将要执行的action抽象的描述

2)Intent结构:

action -- The general action to be performed, such as ACTION_VIEWACTION_EDITACTION_MAIN, etc.

data -- The data to operate on, such as a person record in the contacts database, expressed as a Uri.

 

 

启动另一个Activity(acitiviy需要在androidManifest.xml中进行声明

 

 
Intent intent = new Intent();
intent.setClass(activity1.this,activity2.class);
startActivity(intent);

 

启动service

 

Intent intent = new Intent();
intent.setClass(Mp3PlayerActivity.this, Mp3PlayerService.class);
intent.putExtra("MSG", AppConstant.PlayerMsg.STOP_MSG);
intent.putExtra("mp3Infos", (Serializable) mp3Infos);
startService(intent);


service或者activity 接收intent,在Oncreate()方法,或者Onstart()中

 

 

		mp3Info = (Mp3Info)intent.getSerializableExtra("mp3Info");
		mp3Infos = (List<Mp3Info>)intent.getSerializableExtra("mp3Infos");
		currentListItem = intent.getIntExtra("position", 0);
		int MSG = intent.getIntExtra("MSG", 0);
			currentListItem = intent.getIntExtra("position", 0);
		int MSG = intent.getIntExtra("MSG", 0);
	

在Oncreate()方法中需要用Intent intent = getIntent();获取intent

 


做播放器时,如果MP3playerService发送intent到MP3PlayerActivity,更新界面额seekbar,activity无法持续不断的获取intent来更新界面。前面也看了intent的概述,intent主要用于activity之间,activity到service,广播。从service传递信息到activity,觉得只能使用广播的方法来持续更新activity界面。

 

二.BroadcastReceiver

1.broadcastReceiver用于接收程序所发出的Broadcast intent

启动BroadcastReceiver方法:

1)创建需要启动的BroadcastReceiver 的Intent(设置intent的action属性)

2)调用Context的sendBroadcast()或sendOrderedBroadcast()方法启动指定的BroadcastReceiver

BroadcastReceiver本质上属于一个系统级监听器——负责监听各程序所发出的Broadcast。

 

2.注册BroadcastReceiver,实现BroadcastReceiver匹配Intent有两种方法

1)使用代码进行

 

IntentFilter filter  = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");//过滤器
//IntentFilter filter = new IntentFilter();
//filter.addAction(SMS_ACTION)为filter添加action  private static final String SMS_ACTION="android.provider.Telephony.SMS_RECEIVED" 
IncomingSMSReceiver receiver = new IncomingSMSReceiver();//IncomingSMSReceiver继承BroadcastReceiver,实现OnReceive(Context,Intent)
registerReceiver(receiver,filter);//代码方法注册BroadcastReceiver 相应的有unregisterReceiver(receiver)

 

 

 

2)在AndroidManifest.xml文件中进行配置

 

<receiver android:name=".IncomingSMSReceiver">
	<intent-filter> 过滤器	<intent-filter> 过滤器
		<action android:name="android.provider.Telephony.SMS_RECEIVED"/><action android:name="android.provider.Telephony.SMS_RECEIVED"/>
	</intent-filter>
</receiver></intent-filter>
</receiver>


这种方法,即使应用程序关闭了,系统仍然收到广播事件。

 


exam

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值