android广播是干什么的,android – setAction()为intent做什么(广播)

setAction()在intent(Service)中做了什么

我真的没有得到setAction()的功能,我主要在“服务到活动数据传递”的例子中找到它.字符串可以自由设置吗?它究竟做了什么?

When a broadcast intent is created, it must include an ACTION STRING

in addition to optional data and a category string. As with

standard intents, data is added to

a broadcast intent using key-value pairs in conjunction with the putExtra() method

of the intent object. The optional category string may be assigned to a broadcast intent via a call to the addCategory() method.

The action string, which identifies the broadcast event, must be

unique and typically uses the application’s Java package name syntax.

For example, the following code fragment creates and sends a broadcast

intent including a unique action string and data:

Intent intent = new Intent();

intent.setAction("com.example.Broadcast");

intent.putExtra("HighScore", 1000); sendBroadcast(intent);

我见过的另一个变种是:

Intent broadcastIntent = new Intent();

broadcastIntent.setAction("com.truiton.broadcast.string");

broadcastIntent.putExtra("Data", "Broadcast Data");

sendBroadcast(broadcastIntent);

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

broadcastIntent.setAction("com.truiton.broadcast.integer");

broadcastIntent.putExtra("Data", 10);

sendBroadcast(broadcastIntent);

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

broadcastIntent .setAction("com.truiton.broadcast.arraylist");

broadcastIntent.putExtra("Data", mList);

sendBroadcast(broadcastIntent);

这看起来更像是识别传入的数据类型.

这是否意味着识别事件,传入数据类型,操作或每个Intent创建?它可以免费吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值