【问题标题】:How to delete an image using Notification pending intent action?如何使用通知待处理意图操作删除图像?
【发布时间】:2019-08-19 13:46:51
【问题描述】:

我用动作构建了通知,其中一个动作是Delete。

我有一个截图和他的路径...当用户点击通知图像中的Delete 按钮时,应该删除。

问题是:如何配置PendingIntent,用户可以点击Delete按钮并通过路径删除图片...?

编辑

我想过WorkManager的一些Foreground service实现,但我不确定它是不是正确的方法......

【问题讨论】:

    标签: android push-notification notifications android-notifications


    【解决方案1】:

    前台服务在这种情况下可以很好地工作,示例如下:

    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);
        }
    }
    

    【讨论】:

      【解决方案2】:

      感谢@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版本

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-04
        • 1970-01-01
        • 1970-01-01
        • 2014-09-11
        • 2014-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多