-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Persistent Notification #26
Changes from 1 commit
38dd1e6
0009c3b
053cf34
51dc915
e19f1cc
64db907
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,13 +22,16 @@ | |
import java.net.UnknownHostException; | ||
|
||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.media.AudioFormat; | ||
import android.media.AudioManager; | ||
import android.media.AudioTrack; | ||
import android.media.MediaPlayer; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import android.widget.TextView; | ||
import android.support.v4.app.NotificationCompat; | ||
import android.support.v4.app.NotificationManagerCompat; | ||
|
||
public class ListenActivity extends Activity | ||
{ | ||
|
@@ -37,6 +40,8 @@ public class ListenActivity extends Activity | |
String _address; | ||
int _port; | ||
String _name; | ||
Context _mParentContext = this; | ||
NotificationManagerCompat _mNotifyMgr; | ||
|
||
Thread _listenThread; | ||
private void streamAudio(final Socket socket) throws IllegalArgumentException, IllegalStateException, IOException | ||
|
@@ -93,6 +98,9 @@ protected void onCreate(Bundle savedInstanceState) | |
_address = b.getString("address"); | ||
_port = b.getInt("port"); | ||
_name = b.getString("name"); | ||
// Gets an instance of the NotificationManager service | ||
_mNotifyMgr = | ||
NotificationManagerCompat.from(_mParentContext); | ||
|
||
setContentView(R.layout.activity_listen); | ||
|
||
|
@@ -101,6 +109,17 @@ protected void onCreate(Bundle savedInstanceState) | |
@Override | ||
public void run() | ||
{ | ||
NotificationCompat.Builder mBuilder = | ||
new NotificationCompat.Builder(_mParentContext) | ||
.setSmallIcon(R.drawable.ic_launcher) | ||
.setContentTitle("Baby Monitor Listener") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can these strings be added to the strings.xml file instead? That way others can provide translations for other languages. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, hijacked the existing application name and connection status string. Will refresh the pull-request with the fixes. |
||
.setContentText("Listener is running"); | ||
|
||
|
||
// Sets an ID for the notification | ||
int mNotificationId = 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not make this ID a final and static class member, as it is the same for both notifications. That way it is not a magic number. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
_mNotifyMgr.notify(mNotificationId, mBuilder.build()); | ||
|
||
final TextView connectedText = (TextView) findViewById(R.id.connectedTo); | ||
connectedText.setText(_name); | ||
|
||
|
@@ -146,6 +165,14 @@ public void run() | |
|
||
final TextView statusText = (TextView) findViewById(R.id.textStatus); | ||
statusText.setText(R.string.disconnected); | ||
NotificationCompat.Builder mBuilder = | ||
new NotificationCompat.Builder(_mParentContext) | ||
.setSmallIcon(R.drawable.ic_launcher) | ||
.setContentTitle("Baby Monitor Listener") | ||
.setContentText("Disconnected!"); | ||
// Sets an ID for the notification | ||
int mNotificationId = 1; | ||
_mNotifyMgr.notify(mNotificationId, mBuilder.build()); | ||
} | ||
}); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason for capturing the context here? For the lifetime of the Activity the context will not change. Is it to allow the Runnables to access the context? Why not use ListenActivity.this instead:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that is cleaner way for doing this. Updated the code to this.