Fix possible NullPointerExceptions
This commit is contained in:
@@ -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();
|
||||||
|
if (extras != null) {
|
||||||
|
String name = extras.getString("name");
|
||||||
|
childDeviceName = name;
|
||||||
|
Notification n = buildNotification(name);
|
||||||
startForeground(ID, n);
|
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
|
@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);
|
||||||
@@ -112,7 +109,7 @@ public class ListenService extends Service {
|
|||||||
.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);
|
||||||
|
|||||||
Reference in New Issue
Block a user