我想在我的刻录机服务运行时显示通知。用户应该能够直接从通知中暂停或停止录像机。所以我在Notification中添加了两个按钮,并实现了一个处理按钮点击的BroadcastReceiver。
它几乎像我想要的那样工作,但有一个问题我无法解决。当用户点击按钮时,我想显示活动。这是我的代码:
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());
}
目前,每个按钮都会执行正确的操作,但如果用户点击时该应用不可见,则不会显示该应用。我怎样才能做到这一点?
在这里实现BroadcastReceiver是否有必要?是否有可能使用getActivity而不是getBroadcast并确定以另一种方式单击了哪个按钮?
答案 0 :(得分:0)
好的,我找到了一个更容易且有效的解决方案。不需要BroadcastReceiver,我只是在我的意图中添加额外的信息并在onNewIntent中读取它们。
我的代码如下所示:
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());
}
有人能告诉我这是否是实现目标的最佳途径?我是Android编程的新手,想要正确学习: - )