Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.vkplaya21;
- import static com.example.vkplaya21.CreateNotification.mediaSession;
- import android.annotation.SuppressLint;
- import android.app.ActionBar;
- import android.app.Notification;
- import android.app.NotificationChannel;
- import android.app.NotificationManager;
- import android.app.Service;
- import android.content.BroadcastReceiver;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.content.SharedPreferences;
- import android.content.res.Resources;
- import android.graphics.Color;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- import android.net.Uri;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.IBinder;
- import android.os.PowerManager;
- import android.provider.MediaStore;
- import android.provider.Settings;
- import android.util.Log;
- import android.app.Notification;
- import android.app.PendingIntent;
- import android.app.Service;
- import android.content.BroadcastReceiver;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.media.session.MediaSession;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.os.ResultReceiver;
- import android.support.v4.media.session.MediaSessionCompat;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.widget.EditText;
- import androidx.annotation.Nullable;
- import androidx.core.app.NotificationCompat;
- import androidx.core.app.NotificationManagerCompat;
- import com.example.vkplaya21.Services.BackgroundSoundService;
- import com.example.vkplaya21.Services.NotificationActionService;
- public class CreateNotification extends Service {
- public static final String CHANNEL_ID = "channel1";
- public static MediaSession mediaSession;
- public static final String ACTION_PREVIUOS = "actionprevious";
- public static final String ACTION_PLAY = "actionplay";
- public static final String ACTION_NEXT = "actionnext";
- public static Notification notification;
- public static void createNotification(Context context, Track track, int playbutton, int pos, int size){
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
- NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
- MediaSessionCompat mediaSessionCompat = new MediaSessionCompat( context, "tag");
- Bitmap icon = BitmapFactory.decodeResource(context.getResources(), track.getImage());
- PendingIntent pendingIntentPrevious;
- int drw_previous;
- if (pos == 0){
- pendingIntentPrevious = null;
- drw_previous = 0;
- } else {
- Intent intentPrevious = new Intent(context, NotificationActionService.class)
- .setAction(ACTION_PREVIUOS);
- pendingIntentPrevious = PendingIntent.getBroadcast(context, 0,
- intentPrevious, PendingIntent.FLAG_UPDATE_CURRENT);
- drw_previous = R.drawable.ic_fast_rewind;
- }
- Intent intentPlay = new Intent(context, NotificationActionService.class)
- .setAction(ACTION_PLAY);
- PendingIntent pendingIntentPlay = PendingIntent.getBroadcast(context, 0,
- intentPlay, PendingIntent.FLAG_UPDATE_CURRENT);
- PendingIntent pendingIntentNext;
- int drw_next;
- if (pos == size){
- pendingIntentNext = null;
- drw_next = 0;
- } else {
- Intent intentNext = new Intent(context, NotificationActionService.class)
- .setAction(ACTION_NEXT);
- pendingIntentNext = PendingIntent.getBroadcast(context, 0,
- intentNext, PendingIntent.FLAG_UPDATE_CURRENT);
- drw_next = R.drawable.ic_fast_forward;
- }
- //create notification
- notification = new NotificationCompat.Builder(context, CHANNEL_ID)
- .setSmallIcon(R.drawable.logo)
- .setContentTitle(track.getTitle())
- .setContentText(track.getArtist())
- .setLargeIcon(icon)
- //.setOnlyAlertOnce(true)//show notification for only first time
- .setShowWhen(false)
- .setOngoing(true)
- .addAction(drw_previous, "Previous", pendingIntentPrevious)
- .addAction(playbutton, "Play", pendingIntentPlay)
- .addAction(drw_next, "Next", pendingIntentNext)
- .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
- .setShowActionsInCompactView(0, 1, 2)
- .setMediaSession(mediaSessionCompat.getSessionToken()))
- .setPriority(NotificationCompat.PRIORITY_MAX)
- .build();
- notification.flags = Notification.FLAG_NO_CLEAR;
- notificationManagerCompat.notify(1, notification);
- startForeground(1, notification);
- /**
- ComponentName mediaButtonReceiver =
- new ComponentName(context, BroadcastReceiver.class);
- MediaSessionCompat mediaSession =
- new MediaSessionCompat(context,
- "tag", // Debugging tag, any string
- mediaButtonReceiver,
- null);
- mediaSession.setFlags(
- MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
- MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
- //mediaSession.setCallback(this);
- mediaSession.setCallback(new MediaSessionCompat.Callback() {
- @Override
- public void onCommand(String command, Bundle extras, ResultReceiver cb) {
- super.onCommand(command, extras, cb);
- Log.d("hui", "a");
- }
- @Override
- public void onCustomAction(String action, Bundle extras) {
- super.onCustomAction(action, extras);
- }
- @Override
- public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
- if (Intent.ACTION_MEDIA_BUTTON.equals(mediaButtonEvent.getAction())) {
- final KeyEvent keyEvent = mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
- if (KeyEvent.ACTION_DOWN == keyEvent.getAction()) {
- Log.d("hui", "Media button down!");
- }
- } else {
- Log.d( "hui", "hz intent: " + mediaButtonEvent.getAction());
- }
- return true;
- }
- });
- mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
- mediaSession.setActive(true);
- **/
- }
- }
- @Nullable
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- }
Add Comment
Please, Sign In to add comment