不能通过通知按钮

时间:2015-12-23 03:03:02

标签: android android-asynctask

我试图制作应用,用户可以使用php拍照并将其上传到数据库。我在创建通知时遇到问题,该通知告诉用户照片正在上传,并且可以从通知中取消上传。

我的代码现在看起来像这样:

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

应用程序完全正常工作,唯一缺少应用程序的是取消按钮。它破坏了通知,但无论如何照片都被上传了。

0 个答案:

没有答案