Shorthand of End Code Content for Android

Integrated Development Environment

AS Version
SDK-API
simulator
(omitted from this section)

Layout Design

style="@style/Style Name"

Click Event Binding

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        
        btn_login = findViewById(R.id.button_login);
        btn_login.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.button_login:
                //...
                break;
        }
    }
}

ListView uses

Binding:

Refresh data:

courseAdapter.notifyDataSetChanged();

Adapter:

Fill in data:

Activity

Activate Activity

There are usually two ways to start an Activity:

startActivity( intent )  
startActivityForResult( intent, requestCode)

Both methods require intent to be set first to specify the activity to be started.
However, the latter not only starts the activity, but also requests the other party to return the specified result, so the parameter has one more requestCode parameter than the former.
The Intent object can be set either explicitly (specifying the class name setClass) or implicitly (specifying the action name setAction)
The way to close the current Activity is finish()

Intent intent=new Intent();
intent.setClass(LoginActivity.this,Activity_name.class);//Explicit Intent
startActivity(intent);

Lifecycle

Activity Data Transfer

Sender:

Intent intent= new Intent( MainActivity.this, SecondActivity.class);
intent.putExtra("name", "Andrea");  //"name" is the key and "Andrea" is the value
startActivity(intent);

Receiver:

Intent intent= getIntent();  //Acquisition intent
String name= intent.getStringExtra("name");  //Read the value stored in the name key

Local Data Store

File storage

SharedPreferences

_SQLite Database_

The SQLiteOpenHelper class provided by the Android system for creating and managing databases, typically:
Create/open a database by specifying the database file name and version in the construction method
Add a data table to the onCreat() method, including the table name of each table and the related properties of each field
Implement database version upgrade management process in onUpgrad() method


Add:

Delete:

Check:

Change:

Switch:

db = helper.getReadableDatabase();
db.close();

Broadcast receivers

Create Broadcast Receiver

Send a broadcast message

sendBroadcast() - Sends out-of-order broadcasts without being intercepted;
sendOrderedBroadcast() - Sends an ordered broadcast that a higher priority recipient can intercept.
Both methods encapsulate broadcast messages with Intent objects:
Call the setAction() method to set the action for the Intent object to identify what broadcast this is
Call the putExtra() method to store data to the Intent object, that is, broadcast content.

Service Service

life cycle

startService() : onCreate()-- onStartCommand()
Supporting - stopService(): onDestroy()

bindService() : onCreate()-- onBind()
Supporting - unbindService(): onUnbind()-- OnDestroy()

Service Local Communication

Service Local Communication: The Service component in the same App project package provides data read and write interfaces to other components that are bound. Thus, to achieve Service communication, you need to start the service using the bindService() method.

The key to communication is the Bind object returned in the onBind() method, which is passed to the other party's onServiceConnected() to give the other party an interface for reading and writing Serviceinternal data.

 private ServiceConnection serviceConnection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBinder=(MyService.MyBinder)service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };


Keywords: Android

Added by turbolemon on Mon, 03 Jan 2022 00:57:58 +0200