android, how can I detect if notification clear button is clicked? |
[En]
Very easy.
When you make Notification instance, just add delete intent with setDeleteIntent() method.
private Notification getNotification( Context context, Intent intent, String pushMessage ){
int notiCount = Application.getNotiMessageCount();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon( R.drawable.ic_launcher )
.setContentTitle(getString(R.string.app_name))
.setContentText( pushMessage )
.setContentInfo( Integer.toString( notiCount ) )
.setNumber( notiCount )
.setAutoCancel( true )
.setContentIntent( getPendingIntent( context, intent ) )
.setDeleteIntent( getDeletePendingIntent( context ) );
return builder.getNotification();
}
private PendingIntent getPendingIntent( Context context, Intent intent ){
Intent pushIntent = new Intent( context, MainActivity.class );
return PendingIntent.getActivity( context, 0, pushIntent, PendingIntent.FLAG_UPDATE_CURRENT );
}
private PendingIntent getDeletePendingIntent( Context context ){
Intent deleteIntent = new Intent( context, NotificationDeleteBroadcastReceiver.class );
return PendingIntent.getBroadcast( context, 0, deleteIntent, 0 );
}
If you code like above, NotificationDeleteBroadcastReceiver receives intent when the user pressed "clear" button.
Don't forget to register your broadcast receiver to the manifest!!
[Kr]
간단하다.
Notification 인스턴스를 만들 때 setDeleteIntent() 를 이용하여 delete intent 를 넣어주기만 하면 된다.
private Notification getNotification( Context context, Intent intent, String pushMessage ){
int notiCount = Application.getNotiMessageCount();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon( R.drawable.ic_launcher )
.setContentTitle(getString(R.string.app_name))
.setContentText( pushMessage )
.setContentInfo( Integer.toString( notiCount ) )
.setNumber( notiCount )
.setAutoCancel( true )
.setContentIntent( getPendingIntent( context, intent ) )
.setDeleteIntent( getDeletePendingIntent( context ) );
return builder.getNotification();
}
private PendingIntent getPendingIntent( Context context, Intent intent ){
Intent pushIntent = new Intent( context, MainActivity.class );
return PendingIntent.getActivity( context, 0, pushIntent, PendingIntent.FLAG_UPDATE_CURRENT );
}
private PendingIntent getDeletePendingIntent( Context context ){
Intent deleteIntent = new Intent( context, NotificationDeleteBroadcastReceiver.class );
return PendingIntent.getBroadcast( context, 0, deleteIntent, 0 );
}
위와 같이 코딩하면, NotificationDeleteBroadcastReceiver 에서 유저가 "Clear(지우기)" 버튼을 눌렀을 때 intent 를 받을 수 있다.
물론 manifest 에 broadcast receiver 를 등록하는 것을 잊지 말자.
'프로그래밍 놀이터 > 안드로이드, Java' 카테고리의 다른 글
[java] full 한글 판별 코드 (0) | 2013.10.21 |
---|---|
[android] apply different colors on the texts in the textview using xml (0) | 2013.10.21 |
[android] at GingerBread device GCM register returns SERVICE_NOT_AVAILABLE (4) | 2013.10.15 |
[android] HttpClient vs. HttpURLConnection (0) | 2013.10.11 |
[Java] JDBC, Database 연결하여 사용하기 (0) | 2013.10.11 |
댓글