Android development—Notification control, use Builder constructor to create Notification object, detailed explanation of common properties of control Toolbar

一.Notification

1. Notification 与 NotificationManager 

1.1 Create a NotificationManager 

 The NotificationManager class is a notification manager class. This object is a service maintained by the system and is obtained in a singleton mode, so this object is generally not instantiated directly. In the Activity, you can use the Activity.getSystemService (String) method to obtain
 the NotificationManager object, and the Activity.getSystemService (String) method can return the corresponding object through the handle of the Android system-level service. The NotificationManager needs to be returned here, so just pass Context . NOTIFICATION _ SERVICE directly.

1.2 Use the Builder constructor to create Notification objects

Using the Builder constructor of the NotificationCompat class to create a Notification object can ensure that the program can work normally on all versions. Android 8.0 has added the concept of notification channels, if not set, the notification cannot be displayed on Android 8.0 machines

2. NotificationChannel 

Notification Channels: Android 8.0 introduces notification channels, which allow you to create user-customizable channels for each type of notification to display.

2.1 Notification importance setting, in the NotificationManager class

 IMPORTANCE _ NONE  close notification
 IMPORTANCE _ MIN When the notification is turned on, it will not pop up, but there is no prompt sound, and there is no display in the status bar
  IMPORTANCE _ LOW  When the notification is turned on, it will not pop up or make a sound, and it will display in the status bar
 IMPORTANCE _ DEFAULT  Turn on the notification, it will not pop up, the notification will sound, and the status bar will display
 IMPORTANCE _ HIGH  When the notification is turned on, a pop-up will sound, and the status bar will display
    private NotificationManager manager;

    private Notification notification;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel ("leo", "测试通知",
                        NotificationManager.IMPORTANCE_HIGH);
                manager.createNotificationChannel(channel);
            }


        notification  = new NotificationCompat.Builder(this, "leo")
                .setContentText("官方通知")
                .setContentText("世界那么大想去走走")
                .setSmallIcon(R.drawable.baseline_person_24)
                .build();

    }

Among them, there are three methods in NotificationChannel: id, name, importance

id is actually leo in channelld

3. Description of common methods

1. setContentTitle ( String string ) set title
2. setContentText ( String string ) set text content
3. setSmalllcon ( int icon ) set small icon
4. setLargelcon ( Bitmap icon ) Set large icons for notifications
5. setColor ( int argb ) Set the color of the small icon
6. setContentintent ( Pendingintent intent ) Set the jump intent after clicking the notification
7. setAutoCancel ( boolean boolean ) Set to automatically clear the notification after clicking the notification
8. setWhen ( long when ) Set the time when notifications are created

 Set the first three to display the notification requirements

4. Notes

Starting from the Android 5.0 system, the design of the notification bar icon has been modified.
Now Google requires that the notification bar icons of all applications should only be drawn using the alpha layer, and should not include the RGB layer.

 

<Button
        android:text="发出通知"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="sendNotification" />

    <Button
        android:text="取消通知"
        android:onClick="cacelNotification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    public void sendNotification(View view) {
        manager.notify(1,notification);
    }

    public void  cacelNotification(View view){
    }

At this time, the function is realized as shown in the figure:



 

.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ceshi))

set notification large icon

 

4.1 Rebuild a NotificationActivity.java class to realize the jump intention

package com.example.wzyproject;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

public class NotificationActivity  extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.e("leo","oncreat:进入NotificationActivity");
    }
}
 Intent intent = new Intent(this, NotificationActivity.class);
 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);



notification  = new NotificationCompat.Builder(this, "leo")
                .setContentText("官方通知")
                .setContentText("世界那么大想去走走")
                .setSmallIcon(R.drawable.baseline_person_24)
                                    
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ceshi))
                .setColor(Color.parseColor("#ff0000"))
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .build();

2. Toolbar control

1. Detailed explanation of common attributes

android : layout _ width =" match _ parent " 

android : layout _ height ="? attr / actionBarSize " 

android : background ="#ffff00"
app : navigationlcon ="@ drawable / ic _ baseline _ arrow _ back _24" 

app : title = "main title"
app: title TextColor = "#ff0000" 

app : titleMarginStart ="90dp" 

app : subtitle = "subtitle"
app: subtitle TextColor = "#00ffff"
app: logo = "@ mipmap / ic_launcher"

2. Function realization effect

2.1app : navigationlcon

Demonstration of other functions

Code implementation in MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Toolbar toolbar = findViewById(R.id.tb);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("leo", "onClick: toolbar被点击了");
            }
        });
    }
}

In the activity_main.xml file

<androidx.appcompat.widget.Toolbar
        android:id="@+id/tb"
        android:background="#ff0000"
        app:navigationIcon="@drawable/baseline_sports_baseball_24"
        app:title="标题"
        app:titleTextColor="#ff00ff00"
        app:titleMarginStart="90dp"
        app:subtitle="子标题"
        app:logo="@mipmap/ic_launcher"
        app:subtitleTextColor="#00ffff"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"/>

The running effect can be seen at the running place:

 2.2 You can also set properties in java:

Toolbar toolbar2 = findViewById(R.id.tb2);

        toolbar2.setNavigationIcon(R.drawable.baseline_sports_baseball_24);
        toolbar2.setTitle("标题");
        toolbar2.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("leo", "onClick: toolbar被点击了");
            }
        });
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/tb2"
        android:layout_marginTop="10dp"
        android:background="#ff0000"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />

The effect shown at this time is shown in the figure

 2.3 Use TextView to center the font

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/tb2"
        android:layout_marginTop="10dp"
        app:navigationIcon="@drawable/baseline_sports_baseball_24"
        android:background="#ff0000"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" >

        <TextView
            android:text="标题"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </androidx.appcompat.widget.Toolbar>

Display of results:

 

Guess you like

Origin blog.csdn.net/WZY22502701/article/details/129972143