Android exit Program (lower)-Broadcast Mechanism

Source: Internet
Author: User

Android exit Program (lower)-Broadcast Mechanism
Overview:

Based on the previous blog 《Android exit Program (on)-singleton ModeWe learned how to exit our application by using singleton and loop traversal. This blog will solve the problem from another perspective-broadcast. That is, when receiving a broadcast that closes the Activity, it closes the current Activity.


Class chart display and description:

Authorization/y + zbrDwcuhozwvcD4KPHA + PGJyPgo8L3A + CjxoMT60 + sLryr7A/aO6PC9oMT4KPHA + authorization/authorization + MS6908rVysK8/authorization vadi + authorization "brush: java; "> public class BaseActivity extends Activity {public static final String TAG = BaseActivity. class. getName (); public static final String BROAD_CA ST_MESSAGE = TAG + ". BROAD_CAST_MESSAGE "; private ExitappReceiver mReceiver = null; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); registerReceiver ();} private void registerReceiver () {IntentFilter filter = new IntentFilter (); try {if (mReceiver! = Null) {unregisterReceiver (mReceiver) ;}} catch (Exception e) {e. printStackTrace ();} mReceiver = new ExitappReceiver (); filter. addAction (BROAD_CAST_MESSAGE); registerReceiver (mReceiver, filter) ;}@ Override protected void onDestroy () {if (mReceiver! = Null) {unregisterReceiver (mReceiver);} super. onDestroy ();} class ExitappReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {if (intent. getAction (). equals (BROAD_CAST_MESSAGE) {finish ();}}}}

2. trigger event:

public class TwoActivity extends BaseActivity {    private int getLayoutResID() {        return R.layout.activity_two;    }        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);                setContentView(getLayoutResID());                Button nextButton = (Button) findViewById(R.id.activity_two_button);        nextButton.setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View v) {                sendBroadcast();            }        });    }        private void sendBroadcast() {        Intent intent = new Intent();        intent.setAction(BaseActivity.BROAD_CAST_MESSAGE);        sendBroadcast(intent);    }}


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.