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