How to implement forced deprecation

Source: Internet
Author: User

How to implement forced deprecation

The Force deprecation function is quite common. Many applications have this function. For example, if your QQ account is logged on elsewhere, it will force you to go offline. In fact, the idea of implementing the force deprecation function is also relatively simple. You only need to pop up a dialog box on the interface so that you cannot perform any other operations. You must click OK in the dialog box, then return to the logon page.

To force deprecation, you must first disable all the activities and then return to the logon interface. First, create an ActivityCollector class to manage all the activities.

public class ActivityCollector {    public static List activities =  new ArrayList<>();    public static void addActivity(Activity activity){        activities.add(activity);    }    public static void removeActivity(Activity activity){        activities.remove(activity);    }    public static void finishAll(){        for (Activity activity:activities){            activity.finish();        }    }}

Create a BaseActivity class as the parent class of all activities.

public class BaseActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        ActivityCollector.addActivity(this);    }    @Override    protected void onDestroy() {        super.onDestroy();        ActivityCollector.removeActivity(this);    }}

Create a login. xml layout for the logon interface.

 
     
          
           
        
    
   
      
          
           
        
    
   
      
  
   
  
 

Create LoginActivity inherited from BaseActivity

import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class LoginActivity extends BaseActivity {    private EditText accountEdit;    private EditText passwordEdit;    private Button login;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        accountEdit = (EditText) findViewById(R.id.account);        passwordEdit = (EditText) findViewById(R.id.password);        login = (Button) findViewById(R.id.login);        login.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                String account = accountEdit.getText().toString();                String password = passwordEdit.getText().toString();                if (account.equals("admin") && password.equals("123456")) {                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);                    startActivity(intent);                    finish();                } else {                    Toast.makeText(LoginActivity.this, "accountorpassword is invalid", Toast.LENGTH_SHORT).show();                }            }        });    }}

After successfully logging on to the MainActivity, you can understand the MainActivity as the main interface of the program after successful logon. Here we do not need to provide any fancy functions on the main interface, you only need to add the force deprecation function.

Modify the code in activity_main.xml

 
  
 

Modify the code in MainActivity

import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button forceOffline = (Button) findViewById(R.id.force_offline);        forceOffline.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                Intent intent = new Intent("com.example.xx.FORCE_OFFLINE");                sendBroadcast(intent);            }        });    }}

We sent a broadcast in the Click Event of the button. The broadcast value is com. example. xx. FORCE_OFFLINE. This broadcast is used to notify the program to force the user to go offline. That is to say, the logic to force a user to go offline is not written in MainActivity, but in the broadcast receiver that receives the broadcast. In this way, the force-deprecation function will not be attached to any interface, no matter where the program is, you only need to send such a broadcast to complete the forced deprecation operation.

New ForceOfflineReceiver inherits from BroadcastReceiver

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.