[JAVA] Programmierung ab 51 Jahre Notification IntentService Service Code Memorandum / Zusammenfassung

Benachrichtigungscode-Memorandum

https://akira-watson.com/android/service.html

Hinweis:
In Bezug auf startForeground (ID, Benachrichtigung) stürzt die Benachrichtigungsfunktion ab, wenn ID 1 ist, ist die Benachrichtigungsfunktion deaktiviert, ID ist 2 Ich bin auf das Phänomen gestoßen, benachrichtigt zu werden. Beachten Sie, dass es in Ordnung war, wenn es auf der Straße nicht 0 war, in meinem Code jedoch nicht. Daher wird es im Code auf 2 gesetzt

Notification.Builder(context,channelId) || NotificationCompat.Builder(context,channelId)Der Kontext ist erforderlich, um den Benachrichtigungsbereich vom Android-System zu verwenden, und ist nicht länger als die Lebensdauer des generierten Builder-Objekts vorhanden.

** 1. Benachrichtigungscode vor API25 ** ** 2. Benachrichtigungscode nach API26 ** ** 3.manifest zusätzlichen Code für Android9 Pie und höher ** ** 4. Code, der API 25 oder früher und API 26 oder höher bestimmt **

1. Benachrichtigungscode vor API25

  • Inhaltseinstellungen mit Builder
  • Benachrichtigung mit Benachrichtigung anzeigen

MainActivity


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

   NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"channel_id");
   builder.setSmallIcon(R.drawable.notification_icon)
          .setContentTitle("title under 8")
          .setContentText("content under 8")
          .setPriority(NotificationCompat.PRIORITY_DEFAULT);

   NotificationManager manager =(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);
   manager.notify(0,builder.build());
}

2. Benachrichtigungscode API26 oder höher

  • Inhaltseinstellungen mit Builder
  • Legen Sie die Bedeutung für den Kanal fest
  • Kanal generieren
  • Benachrichtigung mit Benachrichtigung anzeigen

MainActivity


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

   NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"channel_id");
   builder.setSmallIcon(R.drawable.notification_icon)
           .setContentTitle("title over 8")
           .setContentText("content over 8");

   int importance = NotificationManager.IMPORTANCE_DEFAULT;
   NotificationChannel channel = new NotificationChannel("channel_id","name",importance);

   NotificationManager manager =(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);
   manager.createNotificationChannel(channel);
   manager.notify(0,builder.build());
}

3. Manifestieren Sie zusätzlichen Code für Android9 Pie oder höher

AandroidManifest


<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

4. Code, der API 25 oder früher und API 26 oder höher bestimmt

if(Build.VERSION.SDK_INT >=26)

MainActivity


String CHANNEL_ID = "channel_id";

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

    createNotificationChannel();

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this,CHANNEL_ID);
    builder.setSmallIcon(R.drawable.notification_icon)
           .setContentTitle("title")
           .setContentText("content")
           .setPriority(NotificationCompat.PRIORITY_DEFAULT);

    NotificationManager manager =(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);
    manager.notify(0,builder.build());
}

private void createNotificationChannel(){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,"channel_name",importance);
        NotificationManager manager =(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);
        manager.createNotificationChannel(channel);
    }
}

IntentService und Benachrichtigung

1. Erstellen Sie eine Benachrichtigung mit API16

--Erstellen Sie ein Projekt mit Android Studio Targeting API16 --Service verwendet Intent Service --Emulator Android4.1 JellyBean API16 Ausgabe

** Intent Service Code ohne Beurteilung der API-Ebene ** ** Intent Service Code mit Beurteilung der API-Ebene **

API16 startService Code (MainActivity)

MainActivity



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

    Intent intent = new Intent(this,MyService.class);
    if(Build.VERSION_SDK_INT >= 26){
       startForegroundService(intent);
    }else{
       startService(intent);
    }
}

IntentService-Code ohne Beurteilung der API-Ebene

IntentService


@Override
public int onStartCommand(Intent intent,int flag,int starId){
    Context context = getApplicationContext();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"id")
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("Title")
            .setPriority(NotificationCompat.PRIORITY_LOW);
    NotificationManagerCompat manager = NotificationManagerCompat.from(context);
    manager.notify(1,builder.build());
    function();
    return START_STICKY;
}

public void function(){
//Was willst du tun
}

IntentService-Code mit API-Ebene

IntentService


public MyIntentService() {
    super("MyIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"channel");
    builder.setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("sample_title")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .build();

    if(Build.VERSION.SDK_INT >=26){
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("channel","name",importance);
        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.createNotificationChannel(channel);
    }

    NotificationManagerCompat compatManager =NotificationManagerCompat.from(this);
    startForeground(1,builder.build());
    compatManager.notify(1,builder.build());
    function();

    return START_STICKY;
}

public void function(){
//Was willst du tun
}

2. Erstellen Sie eine Benachrichtigung mit API29

--Erstellen Sie ein Projekt mit Android Studio für API29 --Service verwendet Intent Service --Emulator Android9.0 Pie API29 Ausgabe
Emulator Auch wenn es auf Android 4.1 ausgegeben wird, wurde es normal benachrichtigt

API29 MainActivity-Code

MainActivity


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

    Intent intent = new Intent(this,MyIntentService.class);
    if(Build.VERSION.SDK_INT >= 26){
        startForegroundService(intent);
    }else{
        startService(intent);
    }
}

API29 IntentService-Code

IntentService


protected void onHandleIntent(Intent intent) {
    Notification notification;
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"id");
    notification = builder.setSmallIcon(R.drawable.notification_icon)
                          .setContentTitle("title")
                          .setContentText("text")
                          .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                          .build();
    startForeground(2,notification);

    if(Build.VERSION.SDK_INT >= 26){
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("id","name",importance);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

    NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
    managerCompat.notify(1,notification);
    function();

    return START_STICKY;
}

public void function(){
//Was willst du tun
}

Service und Benachrichtigung

AndroidManifest.java

AndroidManifest.java


<manifest ...
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

MainActivity.java

  • Für API26 und höher starten Sie den Dienst mit startForegroundService (Absicht).
  • Für API26 oder weniger starten Sie den Dienst mit startService (Absicht).

MainActivity.java


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

    final Intent intent = new Intent(this,MyService.class);
    Button btn_start = findViewById(R.id.btn_start);
    Button btn_stop  = findViewById(R.id.btn_stop);

    btn_start.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            if(Build.VERSION.SDK_INT >=26){
               startForegroundService(intent);
            }else{
               startService(intent);
            }
        }
    });

    btn_stop.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            stopService(intent);
        }
    });
}

MyService.java

MyService.java


@Override 
public int onStartCommand(Intent intent,int flags,int startId){
   //Legen Sie den Benachrichtigungsinhalt fest
   Notification notification;
   Intent intent_pending = new Intent(this,MainActivity.class);
   PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent_pending,0);
   NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"id");
   notification = builder.setSmallIcon(R.drawable.notification_icon)
       .setContentTitle("Title")
       .setContentText("Text")
       .setContentIntent(pendingIntent)
       .build();

   //API26 und höher: startForeground(notificationId,notification)Benachrichtigungsanzeige mit
   //Generieren Sie einen Kanal, indem Sie die Wichtigkeit des Kanals für jede Benachrichtigung festlegen
   if(Build.VERSION.SDL_INT >= 26){
      int importance = NotificationManager.IMPORTANCE_LOW;
      NotificationChannel channel = new NotificationChannel("id","nama",importance);
      NotificationManager manager = getSystemService(NotificationManager.class);
      manager.createNotificationChannel(channel);
      startForeground(1,notification);
   }else{
      //API26 oder weniger: Legen Sie die Wichtigkeit für jede Komponente fest
      // notify(notificationId,notification)Benachrichtigungsanzeige mit
      NotificationManagerCompat managerCompat = NotificationCompat.from(this);
      notification = builder.setPriority(NotificationCompat.PRIORITY_LOW).build();
      managerCompat.notify(1,notification);
   }
   // ToDo
   Log.d("msg","Mesage");

   //
   return START_STICKY;
}

Recommended Posts