I want to show a notification when my recorder service is running. The user should be able to pause or stop the recorder directly from the notification. So I added two buttons to the Notification and implemented a BroadcastReceiver that handles the button clicks.
It works almost like I want, but there is one problem I cannot solve. When the user clicks on a button, I want to show the activity. Here is my code:
private void showNotification() {
int requestID = (int) System.currentTimeMillis();
IntentFilter filter = new IntentFilter();
filter.addAction("STOP_RECORDING");
filter.addAction("PAUSE_RECORDING");
filter.addAction("SHOW_ACTIVITY");
this.broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case "STOP_RECORDING": onStopClick(); break;
case "PAUSE_RECORDING": onPauseClick(); break;
case "SHOW_ACTITIVY": break;
default: Log.i(T, "UNKNOWN ACTION IN NOTIFICATION INTENT");
}
}
};
Intent stdIntent = new Intent(getApplicationContext(), MyActivityClass.class);
stdIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), requestID, stdIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent1 = new Intent();
intent1.setAction("STOP_RECORDING");
PendingIntent stopIntent = PendingIntent.getBroadcast(getApplicationContext(), requestID, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent2 = new Intent();
intent2.setAction("PAUSE_RECORDING");
PendingIntent pauseIntent = PendingIntent.getBroadcast(getApplicationContext(), requestID, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder;
notificationBuilder = new NotificationCompat.Builder(this);
// ... configure notification (title, text, icon)
notificationBuilder.addAction(R.drawable.ic_pause_white_48dp, "Pause", pauseIntent);
notificationBuilder.addAction(R.drawable.ic_stop_white_48dp, "Stop", stopIntent);
registerReceiver(this.broadcastReceiver, filter);
((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(this.notificationID, notificationBuilder.build());
}
Currently the correct action for each button is executed, but if the app is not visible when the user clicks, the app is not shown. How can I achieve this?
Is it even neccessary to implement a BroadcastReceiver here? Is there a possibility to use getActivity instead of getBroadcast and determine which button has been clicked in another way?
Okay, I found a solution that is much easier and works. No BroadcastReceiver needed, I just add extra information to my intents and read them in onNewIntent.
My code looks like this:
public void onNewIntent(Intent intent) {
String action = intent.getStringExtra("_ACTION_");
if(action != null) {
switch (action) {
case "STOP_RECORDING": onStopClick(); break;
case "PAUSE_RECORDING": onPauseClick(); break;
default: Log.i(T, "UNKNOWN ACTION IN NOTIFICATION INTENT");
}
}
}
private void showNotification() {
int requestID = (int) System.currentTimeMillis();
Intent stdIntent = new Intent(getApplicationContext(), MyActivity.class);
stdIntent.putExtra("_ACTION_", "test_dummy_value");
stdIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), requestID, stdIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent1 = new Intent(getApplicationContext(), MyActivity.class);
intent1.setAction("STOP_RECORDING");
intent1.putExtra("_ACTION_", "STOP_RECORDING");
intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent stopIntent = PendingIntent.getActivity(getApplicationContext(), requestID, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent2 = new Intent(getApplicationContext(), MyActivity.class);
intent2.putExtra("_ACTION_", "PAUSE_RECORDING");
intent2.setAction("PAUSE_RECORDING");
intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent pauseIntent = PendingIntent.getActivity(getApplicationContext(), requestID, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder;
notificationBuilder = new NotificationCompat.Builder(this);
// ... irrelevant code
notificationBuilder.addAction(R.drawable.ic_pause_white_48dp, "Pause", pauseIntent);
notificationBuilder.addAction(R.drawable.ic_stop_white_48dp, "Stop", stopIntent);
((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(this.notificationID, notificationBuilder.build());
}
Can someone tell me if this is the best way to achieve my goal? I am new to Android programming and would like to learn it correctly :-)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.