开发者

Creating Intents with putExtra

I have been creating Intents using putExtra() for quite a while now and just read in the Android documentation that I should be prefixing the 'name' with the package name. So, instead of 'putExtra( "ButtonText", "Ok")' it should be more like 'putExtra( "com.mycompany.myapplication.ButtonText", "Ok" ).

Is this really necessary? (seems to be OK without it).

If it is necessary what is the advantage?

Also, is the package name the callers or the one being called? If it is the callers the 'called Activity' has to know about the callers name which wouldn't be very generic.

T开发者_如何学运维hanks


Is this really necessary? (seems to be OK without it).

No, it isn't necessary for a completely stand-alone app but may be considered to be good practice anyway.

It is more important in apps which are publicly available so they can interact but maintain some way of uniquely identifying themselves and the data they're exchanging. As for which package name is used will depend on the context.

To give an abstract example...

Company A produces an app which can provide some sort of data processing which apps produced by Company B and Company C can use. The 'action' for the intent will be named relevant to Company A but the data passed into it by the two 'client' apps will be named relevant to the client apps companies. Example...

AppA's docs...

To request data processing use:
com.companyA.intent.action.PROCESS_DATA

Pass data with the above intent as an extra named:
<your package name>.SOME_DATA

Now when the relevant component of AppA is called with the above, it will check that there's an 'extra' with a name which ends with .SOME_DATA but it will also be able to maintain that data separately from other data provided by other apps due to the unique prefix. So...

Company B code

Intent i = new Intent(com.companyA.intent.action.PROCESS_DATA);
i.putExtra(com.companyB.SomeApp.SOME_DATA, data);

Company C code

Intent i = new Intent(com.companyA.intent.action.PROCESS_DATA);
i.putExtra(com.companyC.SomeOtherApp.SOME_DATA, data);

OK, possibly not one of my better examples but what it comes down to is it's important to see how the Android environment is very much about different application components being able to use each other, pass data and for the source of that data to be uniquely identifiable.


It sounds more like a "best practices" thing than an actual requirement. The benefit you get here is if you stuff a lot of things in generic intents that are handled by several activities spanning several applications, then you're being more specific about the data in your Intent. If you're using these intents only internally in your application and only your own activities are handling them, you're fine the way you are.


I think this is a GREAT idea IF you are wrapping your extended data into a serializable class say com.mycompany.myapp.MyAppData and sending it as:

intent.putExtra("com.mycompany.myapp.MyAppData",myAppData); //==> out

and retrieving it as:

MyAppData myAppData= (com.mycompany.myapp.MyAppData)intent.getSerializableExtra("com.mycompany.myapp.MyAppData",myAppData; // <== in

JAL

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜