Android에서 프로그래밍 방식으로 알림 표시 줄에서 알림을 제거하는 방법은 무엇입니까?
누구나 Pending 인 텐트를 사용하여 호출되는 프로그래밍 방식으로 애플리케이션에서 알림을 제거 할 수있는 방법을 알고 있습니다.
다음과 같은 방법으로 알림을 취소했습니다.
AlarmManager am=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(Display.this, TwoAlarmService.class);
PendingIntent pi = PendingIntent.getBroadcast(Display.this, AlarmNumber, intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.cancel(pi);
그러나 문제는 알림 표시 줄에서 제거되지 않은 이미 실행 된 알림입니다.
미리 감사드립니다 ...
어쩌면 이것을 시도하십시오 :
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
또는 주어진 컨텍스트에서 모든 알림을 취소하기 위해 이렇게 할 수도 있습니다.
notificationManager.cancelAll();
문서에 대한이 링크를 참조하십시오. NotificationManager
알림은 다음 중 하나가 발생할 때까지 계속 표시됩니다.
사용자는 개별적으로 또는 "모두 지우기"를 사용하여 알림을 해제합니다 (알림을 지울 수있는 경우). 사용자가 알림을 클릭하고 알림을 만들 때 setAutoCancel ()을 호출했습니다. 특정 알림 ID에 대해 cancel ()을 호출합니다. 이 방법은 진행중인 알림도 삭제합니다. 이전에 발행 한 모든 알림을 제거하는 cancelAll ()을 호출합니다. setTimeoutAfter ()를 사용하여 알림을 생성 할 때 제한 시간을 설정하면 시스템은 지정된 기간이 경과 한 후 알림을 취소합니다. 필요한 경우 지정된 제한 시간이 경과하기 전에 알림을 취소 할 수 있습니다.
public void cancelNotification() {
String ns = NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) getActivity().getApplicationContext().getSystemService(ns);
nMgr.cancel(NOTIFICATION_ID);
}
모든 알림 (다른 앱 알림 포함)은 NotificationListenerService 구현에 언급 된대로 'NotificationListenerService'를 수신하여 제거 할 수 있습니다.
서비스에서 당신은 전화해야합니다 cancelAllNotifications()
.
'앱 및 알림'-> '특별 앱 액세스'-> '알림 액세스'를 통해 애플리케이션에 대해 서비스를 활성화해야합니다.
매니페스트에 추가 :
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:label="Test App" android:name="com.test.NotificationListenerEx" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
그런 다음 코드에서;
public class NotificationListenerEx extends NotificationListenerService {
public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
NotificationListenerEx.this.cancelAllNotifications();
}
};
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
super.onNotificationRemoved(sbn);
}
@Override
public IBinder onBind(Intent intent) {
return super.onBind(intent);
}
@Override
public void onDestroy() {
unregisterReceiver(broadcastReceiver);
super.onDestroy();
}
@Override
public void onCreate() {
super.onCreate();
registerReceiver(broadcastReceiver, new IntentFilter("com.test.app"));
}
그 후 브로드 캐스트 수신기를 사용하여 모두 지우기를 트리거합니다.
위의 코드에 따라 방송 사용을 트리거합니다.
getContext().sendBroadcast(new Intent("com.test.app"));
'Program Tip' 카테고리의 다른 글
Xamarin.Forms-새 페이지를 만들 때 InitializeComponent가 존재하지 않습니다. (0) | 2020.11.19 |
---|---|
목록에서 대소 문자 구분을 무시하는 방법 (0) | 2020.11.19 |
C #을 사용하여 텍스트 파일의 내용 지우기 (0) | 2020.11.19 |
장고에서 관리자 CSS 재정의 (0) | 2020.11.19 |
Pause 모나드 (0) | 2020.11.19 |