Совершенно новый для Android Программирование здесь, так что потерпите, если я сделал ладейную ошибку ie. Мне удалось заставить мою службу переднего плана работать хорошо, но я не вижу элементов управления в своем уведомлении при отладке и запуске на моем устройстве мониторинга, за исключением меток Prev, Play и Next. Я хочу видеть эти элементы управления и делать что-нибудь при нажатии на них. У меня также есть некоторые ошибки в моем журнале, как показано ниже:
2020-05-08 12:31:05.567 14495-14495/? E/libc: Access denied finding property "persist.vendor.sys.activitylog"
2020-05-08 12:31:35.063 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: stop called in state 1, mPlayer(0x0)
2020-05-08 12:31:35.063 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:31:35.148 14495-14620/net.groupkse.indupendo E/libc: Access denied finding property "persist.vendor.log.tel_dbg"
2020-05-08 12:31:35.765 14495-14621/net.groupkse.indupendo E/libARC: item map does not been created yet!
2020-05-08 12:31:35.886 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:31:36.261 14495-14621/net.groupkse.indupendo E/ion: ioctl c0044901 failed with code -1: Invalid argument
2020-05-08 12:32:33.046 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: start called in state 4, mPlayer(0x9eebc3e0)
2020-05-08 12:32:33.046 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:32:33.120 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:32:40.226 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: start called in state 0, mPlayer(0x9eebc3e0)
2020-05-08 12:32:40.226 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:32:40.298 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:33:07.020 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: start called in state 0, mPlayer(0x9eebc3e0)
2020-05-08 12:33:07.020 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:33:07.092 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:33:16.921 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: stop called in state 0, mPlayer(0x9eebc3e0)
2020-05-08 12:33:16.922 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:33:17.428 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:33:47.577 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: stop called in state 4, mPlayer(0x8dfec8a0)
2020-05-08 12:33:47.577 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:33:47.971 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:33:48.729 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: start called in state 4, mPlayer(0x8dfecb80)
2020-05-08 12:33:48.729 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:33:48.766 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:33:53.622 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: stop called in state 0, mPlayer(0x8dfecb80)
2020-05-08 12:33:53.622 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:33:54.007 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:33:59.446 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: stop called in state 4, mPlayer(0x8dfecf40)
2020-05-08 12:33:59.446 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:33:59.830 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:33:59.970 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: start called in state 4, mPlayer(0x8b70fb00)
2020-05-08 12:33:59.970 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:34:00.068 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:34:06.971 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: start called in state 0, mPlayer(0x8b70fb00)
2020-05-08 12:34:06.972 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:34:07.014 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:34:07.659 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: start called in state 0, mPlayer(0x8b70fb00)
2020-05-08 12:34:07.659 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:34:07.701 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:34:27.028 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: start called in state 4, mPlayer(0x8acd40e0)
2020-05-08 12:34:27.028 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:34:27.071 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:34:27.658 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: start called in state 0, mPlayer(0x8acd40e0)
2020-05-08 12:34:27.658 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:34:27.704 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:34:30.055 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: stop called in state 0, mPlayer(0x8acd40e0)
2020-05-08 12:34:30.055 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:34:30.382 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:34:32.015 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: start called in state 4, mPlayer(0x8acd4500)
2020-05-08 12:34:32.015 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:34:32.053 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:34:36.824 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: start called in state 0, mPlayer(0x8acd4500)
2020-05-08 12:34:36.824 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:34:36.862 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: stop called in state 0, mPlayer(0x8acd4500)
2020-05-08 12:34:36.862 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:34:37.194 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:34:37.201 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:34:41.317 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: start called in state 4, mPlayer(0x8acd4920)
2020-05-08 12:34:41.317 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:34:41.366 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:34:44.246 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: stop called in state 0, mPlayer(0x8acd4920)
2020-05-08 12:34:44.246 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:34:44.575 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:34:47.350 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: start called in state 4, mPlayer(0x8acd4dc0)
2020-05-08 12:34:47.350 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:34:47.396 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:34:49.349 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: stop called in state 0, mPlayer(0x8acd4dc0)
2020-05-08 12:34:49.349 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:34:49.681 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:34:52.699 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: start called in state 4, mPlayer(0x890eb2a0)
2020-05-08 12:34:52.699 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:34:52.735 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:34:55.546 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: stop called in state 0, mPlayer(0x890eb2a0)
2020-05-08 12:34:55.546 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:34:55.860 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:34:59.521 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: start called in state 4, mPlayer(0x890eb520)
2020-05-08 12:34:59.521 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:34:59.552 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:35:00.624 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: stop called in state 0, mPlayer(0x890eb520)
2020-05-08 12:35:00.624 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:35:00.936 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
2020-05-08 12:35:01.715 14495-15075/net.groupkse.indupendo E/JavaBinder: *** Uncaught remote exception! (Exceptions are not yet supported across processes.)
java.lang.ArrayIndexOutOfBoundsException: length=8192; index=8192
at com.android.okhttp.okio.Buffer.writeByte(Buffer.java:973)
at com.android.okhttp.internal.Platform.concatLengthPrefixed(Platform.java:212)
at com.android.okhttp.internal.Platform.configureTlsExtensions(Platform.java:116)
at com.android.okhttp.internal.io.RealConnection.connectTls(RealConnection.java:187)
at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:149)
at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:299)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:237)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:472)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:418)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:549)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:26)
at android.media.MediaHTTPConnection.seekTo(MediaHTTPConnection.java:212)
at android.media.MediaHTTPConnection.getMIMEType(MediaHTTPConnection.java:387)
at android.media.IMediaHTTPConnection$Stub.onTransact(IMediaHTTPConnection.java:89)
at android.os.Binder.execTransact(Binder.java:731)
2020-05-08 12:35:12.790 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: start called in state 4, mPlayer(0x890eb9e0)
2020-05-08 12:35:12.790 14495-14495/net.groupkse.indupendo E/MediaPlayerNative: error (-38, 0)
2020-05-08 12:35:12.818 14495-14495/net.groupkse.indupendo E/MediaPlayer: Error (-38,0)
Вот мои константы. java class:
public class Constants {
public interface ACTION {
public static String MAIN_ACTION = "net.groupkse.indupendo.action.main";
public static String PREV_ACTION = "net.groupkse.indupendo.action.prev";
public static String PLAY_ACTION = "net.groupkse.indupendo.action.prev";
public static String NEXT_ACTION = "net.groupkse.indupendo.action.next";
public static String STARTFOREGROUND_ACTION = "net.groupkse.indupendo.action.startforeground";
public static String STOPFOREGROUND_ACTION = "net.groupkse.indupendo.action.stopforeground";
}
public interface NOTIFICATION_ID {
public static int FOREGROUND_SERVICE = 101;
}
}
, тогда вот мои методы createNotificationChannel в отдельном классе Я вызываю NotifyApp. java:
public class NotifyApp extends Application {
// Registering notification channel
public static final String CHANNEL_ID = "playerServiceChannel";
@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
}
private void createNotificationChannel() {
// Creating the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
/**
* CharSequence name = getString(R.string.playerServiceChannel);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT; */
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Player Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
}
вот фрагмент кода из моей PlayerService. java class:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getStringExtra("url") != null)
playStream(intent.getStringExtra("url"));
if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
Log.i("info", "Start foreground service!");
showNotification();
} else if (intent.getAction().equals(Constants.ACTION.PREV_ACTION)) {
Log.i("info", "Prev pressed.");
} else if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) {
Log.i("info", "Play pressed.");
togglePlayer();
} else if (intent.getAction().equals(Constants.ACTION.NEXT_ACTION)) {
Log.i("info", "Next pressed.");
} else if (intent.getAction().equals(Constants.ACTION.STOPFOREGROUND_ACTION)) {
Log.i("info", "Stop foreground received.");
stopForeground(true);
stopSelf();
}
return START_REDELIVER_INTENT;
}
@Override
public void onDestroy() {
super.onDestroy();
}
private void showNotification () {
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
//notificationIntent.setFlags((Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Intent previousIntent = new Intent(this, PlayerService.class);
previousIntent.setAction(Constants.ACTION.PREV_ACTION);
PendingIntent ppreviousIntent = PendingIntent.getActivity(this, 0, previousIntent, 0);
Intent playIntent = new Intent(this, PlayerService.class);
playIntent.setAction(Constants.ACTION.PLAY_ACTION);
PendingIntent pplayIntent = PendingIntent.getActivity(this, 0, playIntent, 0);
Intent nextIntent = new Intent(this, PlayerService.class);
nextIntent.setAction(Constants.ACTION.NEXT_ACTION);
PendingIntent pnextIntent = PendingIntent.getActivity(this, 0, nextIntent, 0);
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_kwacha_coin);
int playPauseButtonId = android.R.drawable.ic_media_play;
if (mediaPlayer != null && mediaPlayer.isPlaying())
playPauseButtonId = android.R.drawable.ic_media_pause;
String labelId = "Play";
if (mediaPlayer != null && mediaPlayer.isPlaying())
labelId = "Pause";
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Music Player")
.setTicker("Playing music")
.setContentText("My song")
.setSmallIcon(R.drawable.ic_kwacha_coin)
.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
.setContentIntent(pendingIntent)
.setOngoing(true)
.addAction(android.R.drawable.ic_media_previous, "Previous", ppreviousIntent)
.addAction(playPauseButtonId, labelId, pplayIntent)
.addAction(android.R.drawable.ic_media_next, "Next", pnextIntent)
.build();
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, notification);
}
и, наконец, некоторые из моей MainActivity. java класс:
private void startStreamService (String url) {
Intent i = new Intent(this, PlayerService.class);
i.putExtra("url", url);
i.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);
startService(i);
bindService(i, mServiceConnection, Context.BIND_AUTO_CREATE);
}