Broadcast registration in Android 4.0

Source: Internet
Author: User

You can register a broadcast in either of the following ways:
1. Write a *** broadcastreceiver inherited from the system's broadcastreceiver to implement its

Onreceive method. Then you can register in the mainfest. xml file to accept the broadcast.

2. Define an intentfilter to filter the specified action, and then register it through the registerreceiver of the activity.
However, for the second method of registering broadcast, there is no problem in versions earlier than android4.0. In android4.0, if you have a sub-activity,

And you want to register a broadcast in the sub-acitivity, then you are miserable,ProgramKeep crash and prompt java. Lang. securityexception.

Solution: Because the sub-acit on android4.0 restricts the registration broadcast behavior, but if you need to register the sub-acitvity, you need to use the context of the parent activity,

Use the context of the parent activity to register the broadcast instead of the context of the Child Activity.

Note:

On Android 4.0:
Subactivity cannot register broadcast, create database, and start a new activity.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.