如何使用通知未决意图操作删除图像?
How to delete an image using Notification pending intent action?
我用操作构建了通知,其中一个操作是 Delete
。
我有一个屏幕截图和他的路径...当用户点击通知图像中的 Delete
按钮时应该删除。
问题是:如何配置 PendingIntent
用户可以单击 Delete
按钮并按路径删除图像...?
编辑
我考虑了一些 Foreground service
实施 WorkManager
实施,但我不确定这是正确的方法...
前台服务在这种情况下运行良好,示例如下:
public class DeleteImageService extends IntentService {
public DeleteImageService(String name) {
super(name);
}
@Override
public void onCreate() {
super.onCreate();
startForeground(1, notification);
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
if (intent != null) {
if (ACTION_DELETE.equals(intent.getAction()) && intent.hasExtra(EXTRA_PATH)) {
string path = intent.getString(EXTRA_PATH);
// Delete image in path
}
}
}
static PendingIntent pendingIntent(Context context) {
final Intent intent = new Intent(context, DeleteImageService.class);
intent.setAction(ACTION_DELETE);
intent.putExtra(EXTRA_PATH, path);
return PendingIntent.getForegroundService(context, 1, retrieve, 0);
}
}
感谢@JakeB,我对他的回答有了一点改进,这就是我得到的
public class DeleteImageService extends IntentService
{
private final static String TAG = DeleteImageService.class.getSimpleName();
private static final int DELETE_IMAGE_SERVICE_REQUEST_CODE = 1;
public static final String EXTRA_SCREENSHOT_PATH = "extra_screenshot_path";
public static final String DELETE_IMAGE_SERVICE = "delete_image_service";
public DeleteImageService()
{
super(DELETE_IMAGE_SERVICE);
}
@Override
public void onCreate()
{
super.onCreate();
startService(new Intent(this, DeleteImageService.class));
}
@Override
protected void onHandleIntent(@Nullable Intent intent)
{
if (intent != null && Intent.ACTION_DELETE.equals(intent.getAction()) && intent.hasExtra(EXTRA_SCREENSHOT_PATH))
{
String path = intent.getStringExtra(EXTRA_SCREENSHOT_PATH);
FileManager.deleteFileBy(path);
Logger.log(Log.ERROR, TAG, path);
PushNotificationManager.getInstance(this).getScreenshotNotificator(this).closeNotification();
}
}
@NonNull
public static PendingIntent pendingIntent(@NonNull final Context context,//
@NonNull final String iPath)
{
final Intent intent = new Intent(context, DeleteImageService.class);
intent.setAction(Intent.ACTION_DELETE);
intent.putExtra(EXTRA_SCREENSHOT_PATH, iPath);
PendingIntent pIntent;
pIntent = PendingIntent.getService(context, DELETE_IMAGE_SERVICE_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pIntent;
}
}
此代码支持android版本<26,也不请求前台服务权限。
我用操作构建了通知,其中一个操作是 Delete
。
我有一个屏幕截图和他的路径...当用户点击通知图像中的 Delete
按钮时应该删除。
问题是:如何配置 PendingIntent
用户可以单击 Delete
按钮并按路径删除图像...?
编辑
我考虑了一些 Foreground service
实施 WorkManager
实施,但我不确定这是正确的方法...
前台服务在这种情况下运行良好,示例如下:
public class DeleteImageService extends IntentService {
public DeleteImageService(String name) {
super(name);
}
@Override
public void onCreate() {
super.onCreate();
startForeground(1, notification);
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
if (intent != null) {
if (ACTION_DELETE.equals(intent.getAction()) && intent.hasExtra(EXTRA_PATH)) {
string path = intent.getString(EXTRA_PATH);
// Delete image in path
}
}
}
static PendingIntent pendingIntent(Context context) {
final Intent intent = new Intent(context, DeleteImageService.class);
intent.setAction(ACTION_DELETE);
intent.putExtra(EXTRA_PATH, path);
return PendingIntent.getForegroundService(context, 1, retrieve, 0);
}
}
感谢@JakeB,我对他的回答有了一点改进,这就是我得到的
public class DeleteImageService extends IntentService
{
private final static String TAG = DeleteImageService.class.getSimpleName();
private static final int DELETE_IMAGE_SERVICE_REQUEST_CODE = 1;
public static final String EXTRA_SCREENSHOT_PATH = "extra_screenshot_path";
public static final String DELETE_IMAGE_SERVICE = "delete_image_service";
public DeleteImageService()
{
super(DELETE_IMAGE_SERVICE);
}
@Override
public void onCreate()
{
super.onCreate();
startService(new Intent(this, DeleteImageService.class));
}
@Override
protected void onHandleIntent(@Nullable Intent intent)
{
if (intent != null && Intent.ACTION_DELETE.equals(intent.getAction()) && intent.hasExtra(EXTRA_SCREENSHOT_PATH))
{
String path = intent.getStringExtra(EXTRA_SCREENSHOT_PATH);
FileManager.deleteFileBy(path);
Logger.log(Log.ERROR, TAG, path);
PushNotificationManager.getInstance(this).getScreenshotNotificator(this).closeNotification();
}
}
@NonNull
public static PendingIntent pendingIntent(@NonNull final Context context,//
@NonNull final String iPath)
{
final Intent intent = new Intent(context, DeleteImageService.class);
intent.setAction(Intent.ACTION_DELETE);
intent.putExtra(EXTRA_SCREENSHOT_PATH, iPath);
PendingIntent pIntent;
pIntent = PendingIntent.getService(context, DELETE_IMAGE_SERVICE_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pIntent;
}
}
此代码支持android版本<26,也不请求前台服务权限。