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创建?它可以免费吗?