Dro*_*man 80 service notifications android temporary-files swipe
我正在使用android通知在服务完成(成功或失败)后提醒用户,并且我想在完成该过程后删除本地文件.
我的问题是,如果发生故障 - 我想让用户进行"重试"选项.如果他选择不重试并解除通知我想删除为处理目的而保存的本地文件(图像......).
有没有办法捕获通知的刷卡到解雇事件?
Mr.*_*.Me 140
DeleteIntent:DeleteIntent是一个PendingIntent对象,可以与通知关联,并在通知被删除时被触发,ether by:
您可以将Pending Intent设置为广播Receiver,然后执行您想要的任何操作.
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
Builder builder = new Notification.Builder(this):
..... code for your notification
builder.setDeleteIntent(pendingIntent);
Run Code Online (Sandbox Code Playgroud)
MyBroadcastReceiver
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
.... code to handle cancel
}
}
Run Code Online (Sandbox Code Playgroud)
Chr*_*ght 80
一个完全刷新的答案(感谢我先生的答案):
1)创建一个接收器来处理滑动到解除事件:
public class NotificationDismissedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int notificationId = intent.getExtras().getInt("com.my.app.notificationId");
/* Your code to handle the event here */
}
}
Run Code Online (Sandbox Code Playgroud)
2)在清单中添加一个条目:
<receiver
android:name="com.my.app.receiver.NotificationDismissedReceiver"
android:exported="false" >
</receiver>
Run Code Online (Sandbox Code Playgroud)
3)使用未决意图的唯一ID(此处使用通知ID)创建待定意图,因为没有这个,将为每个解雇事件重复使用相同的额外内容:
private PendingIntent createOnDismissedIntent(Context context, int notificationId) {
Intent intent = new Intent(context, NotificationDismissedReceiver.class);
intent.putExtra("com.my.app.notificationId", notificationId);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context.getApplicationContext(),
notificationId, intent, 0);
return pendingIntent;
}
Run Code Online (Sandbox Code Playgroud)
4)建立你的通知:
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle("My App")
.setContentText("hello world")
.setWhen(notificationTime)
.setDeleteIntent(createOnDismissedIntent(context, notificationId))
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, notification);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
37884 次 |
最近记录: |