1

I'm trying to make app where user is able to take photo and upload it to database using php. I have problem with creating notification which is telling user that photo is being uploaded and giving him possibility to cancel upload from notification.

My code now looks like this :

boolean running = true;
    NotificationCompat.Builder builder;
    NotificationManager NotifyMgr;

    @Override
    protected void onCancelled(){
        running = false;
    }

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if(action.equals("com.app.example.MyServiceClass.STOP")){
                cancel(true);
                NotifyMgr.cancel(001);
            }
        }
    };

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        Intent intent2 = new Intent();
        intent2.setAction("com.app.example.MyServiceClass.STOP");
        PendingIntent pIntent = PendingIntent.getBroadcast(confirmationActivity.this, 0, intent2, 0);
        builder = new NotificationCompat.Builder(confirmationActivity.this)
                .setSmallIcon(R.drawable.refresh)
                .setContentTitle("PhotoApplication")
                .setContentText("Photo is being uploaded...")
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .addAction(R.drawable.cancel, "Cancel Uploading", pIntent);

        IntentFilter filter = new IntentFilter();
        filter.addAction("com.app.example.MyServiceClass.STOP");
        registerReceiver(receiver, filter);

        NotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotifyMgr.notify(001, builder.build());

        Intent i = new Intent(getApplicationContext(), MainActivity.class);
        i.putExtra("firstTime", false);
        startActivity(i);
    }

    protected String doInBackground(String... args) {
        while(running == true) {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            Bitmap secondBitmap = BitmapFactory.decodeFile(filePath, options);
            int height2 = secondBitmap.getHeight() / (secondBitmap.getWidth() / 200);
            secondBitmap = Bitmap.createScaledBitmap(secondBitmap, 200, height2, false);
            String bitmapEncoded2 = encodeTobase64(secondBitmap);
            bitmap = BitmapFactory.decodeFile(filePath, options);
            int height = bitmap.getHeight() / (bitmap.getWidth() / 1080);
            bitmap = Bitmap.createScaledBitmap(bitmap, 1080, height, false);
            String bitmapEncoded = encodeTobase64(bitmap);
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("title", titleq));
            params.add(new BasicNameValuePair("description", descriptionq));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("photo", bitmapEncoded));
            params.add(new BasicNameValuePair("smallphoto", bitmapEncoded2));

            JSONObject json = jsonParser.makeHttpRequest(url_create_product, "POST", params);

            Log.d("Create Response", json.toString());

            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(confirmationActivity.this).setSmallIcon(R.drawable.refresh).setContentTitle("PhotoApplication").setContentText("Your photo has been uploaded!");
            Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
            PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);
            int mNotificationId = 002;
            NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            mNotifyMgr.notify(mNotificationId, mBuilder.build());

            running = false;
        }
        finish();

        return null;
    }

    protected void onPostExecute(String file_url) {

    }
}

class DeleteProduct extends AsyncTask<String, String, String> {
    protected String doInBackground(String... args) {
        int success;
        try {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("timestamp", timeStamp));

            JSONObject json = jsonParser.makeHttpRequest(url_delete_product, "POST", params);

            Log.d("Delete Product", json.toString());

            success = json.getInt("success");
        } catch (JSONException e){
            e.printStackTrace();
        }

        return null;
    }
}

App is fully working, the only thing that app is missing is that working cancel button. It's destroying notification, but photo is being uplaoded anyway.

Aᴍɪʀ
  • 7,623
  • 3
  • 38
  • 52
LisSkis
  • 101
  • 1
  • 10
  • I guess that's cause , you just cancelling the notification. That cancel(true) might not be cancelling the going on Async task. For confirmation put some logs in onCancelled(). And try cancelling the Async task like this. http://stackoverflow.com/questions/7821284/how-to-stop-asynctask-thread-in-android – Vins Dec 23 '15 at 05:12
  • This is not working, even if I change manually running = false photo is still being uploaded – LisSkis Dec 23 '15 at 13:46

0 Answers0