Fix possible NullPointerExceptions

This commit is contained in:
edr
2024-02-17 02:44:18 +01:00
committed by GitHub
parent e95a554717
commit 43a7a66caa

View File

@@ -36,8 +36,6 @@ public class ListenService extends Service {
private final IBinder binder = new ListenBinder();
private NotificationManager notificationManager;
private String address;
private int port;
private Thread listenThread;
private final VolumeHistory volumeHistory = new VolumeHistory(16_384);
@@ -57,11 +55,18 @@ public class ListenService extends Service {
Log.i(TAG, "Received start id " + startId + ": " + intent);
// Display a notification about us starting. We put an icon in the status bar.
createNotificationChannel();
Notification n = buildNotification(intent);
Bundle extras = intent.getExtras();
if (extras != null) {
String name = extras.getString("name");
childDeviceName = name;
Notification n = buildNotification(name);
startForeground(ID, n);
doListen();
String address = extras.getString("address");
int port = extras.getInt("port");
doListen(address, port);
}
return START_STICKY;
return START_REDELIVER_INTENT;
}
@Override
@@ -90,18 +95,10 @@ public class ListenService extends Service {
return volumeHistory;
}
private Notification buildNotification(Intent intent) {
private Notification buildNotification(String name) {
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.listening);
final Bundle bundle = intent.getExtras();
if (bundle == null) {
return null;
}
address = bundle.getString("address");
port = bundle.getInt("port");
childDeviceName = bundle.getString("name");
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, ListenActivity.class), PendingIntent.FLAG_IMMUTABLE);
@@ -112,7 +109,7 @@ public class ListenService extends Service {
.setOngoing(true)
.setTicker(text) // the status text
.setContentTitle(text) // the label of the entry
.setContentText(childDeviceName) // the contents of the entry
.setContentText(name) // the contents of the entry
.setContentIntent(contentIntent);
return b.build();
}
@@ -146,7 +143,7 @@ public class ListenService extends Service {
private Runnable mErrorCallback;
private Runnable mUpdateCallback;
private void doListen() {
private void doListen(String address, int port) {
listenThread = new Thread(() -> {
try {
final Socket socket = new Socket(address, port);