最近在搞个东西,遇到了一些奇葩的需求。其中一个就是在点击通知之后收起状态栏。
正常情况下还好,如果遇到自定义的RemoteViews就xxx,经过google之后发现一个有效的解决方法就是发送一个系统广播
通知创建部分代码:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContent(remoteViews);
builder.setAutoCancel(true);
builder.setDeleteIntent(PendingIntent.getBroadcast(getApplicationContext(), 1, new Intent("delete"), PendingIntent.FLAG_UPDATE_CURRENT));
remoteViews.setOnClickPendingIntent(R.id.notifyMessage, PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent("OnclickReceiver"), PendingIntent.FLAG_UPDATE_CURRENT));
remoteViews.setTextViewText(R.id.notifyMessage, String.format(context.getString(R.string.text_notify_message),networkName));
Notification n = builder.build();
NotificationManager manger = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manger.notify(NOTIFICATIONID, n);
BroadCastReceiver 部分代码:
public class ListenerReceive extends BroadcastReceiver {
final static public String TAGNAME = "OnclickReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equalsIgnoreCase("OnclickReceiver")){
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
PendingIntent pi = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
try {
pi.send(context, 0, new Intent());
} catch (CanceledException e) {
e.printStackTrace();
}
NotificationManager manage = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
manage.cancel(NOTIFICATIONID);
//收起状态栏
sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
}
}
}