Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法使用通知按钮停止异步任务_Android_Android Asynctask - Fatal编程技术网

Android 无法使用通知按钮停止异步任务

Android 无法使用通知按钮停止异步任务,android,android-asynctask,Android,Android Asynctask,我正在尝试制作一个应用程序,用户可以在其中拍照并使用php将其上传到数据库。我在创建通知时遇到了问题,该通知告诉用户照片正在上传,并允许用户从通知中取消上传 我的代码现在如下所示: boolean running = true; NotificationCompat.Builder builder; NotificationManager NotifyMgr; @Override protected void onCancelled(){ run

我正在尝试制作一个应用程序,用户可以在其中拍照并使用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;
    }
}
boolean running=true;
通知建筑商;
通知经理通知经理;
@凌驾
受保护的void onCancelled(){
运行=错误;
}
专用最终BroadcastReceiver=新的BroadcastReceiver(){
@凌驾
公共void onReceive(上下文、意图){
String action=intent.getAction();
if(action.equals(“com.app.example.MyServiceClass.STOP”)){
取消(真);
通知经理取消(001);
}
}
};
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
Intent intent2=新Intent();
intent2.setAction(“com.app.example.MyServiceClass.STOP”);
PendingEvent pIntent=PendingEvent.getBroadcast(confirmationActivity.this,0,intent2,0);
builder=新通知compat.builder(confirmationActivity.this)
.setSmallIcon(R.drawable.refresh)
.setContentTitle(“PhotoApplication”)
.setContentText(“正在上载照片…”)
.setPriority(通知兼容优先级高)
.addAction(R.drawable.cancel,“取消上传”,pIntent);
IntentFilter=newintentfilter();
filter.addAction(“com.app.example.MyServiceClass.STOP”);
寄存器接收器(接收器、过滤器);
NotifyMgr=(NotificationManager)getSystemService(通知服务);
NotifyMgr.notify(001,builder.build());
Intent i=新的Intent(getApplicationContext(),MainActivity.class);
i、 putExtra(“第一次”,假);
星触觉(i);
}
受保护的字符串doInBackground(字符串…args){
while(running==true){
final BitmapFactory.Options=new BitmapFactory.Options();
Bitmap secondBitmap=BitmapFactory.decodeFile(文件路径,选项);
int height2=secondBitmap.getHeight()/(secondBitmap.getWidth()/200);
secondBitmap=Bitmap.createScaledBitmap(secondBitmap,200,高度2,false);
字符串bitmapEncoded2=encodeTobase64(第二个位图);
位图=BitmapFactory.decodeFile(文件路径,选项);
int height=bitmap.getHeight()/(bitmap.getWidth()/1080);
位图=位图.createScaledBitmap(位图,1080,高度,false);
字符串bitmapEncoded=encodeTobase64(位图);
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“标题”,标题Q));
参数add(新的BasicNameValuePair(“description”,descriptionq));
添加(新的BasicNameValuePair(“timestamp”,timestamp));
添加(新的BasicNameValuePair(“照片”,位图编码));
添加(新的BasicNameValuePair(“smallphoto”,bitmapEncoded2));
JSONObject json=jsonParser.makeHttpRequest(url\u create\u product,“POST”,params);
d(“创建响应”,json.toString());
NotificationCompat.Builder mBuilder=新建NotificationCompat.Builder(confirmationActivity.this).setmallicon(R.drawable.refresh).setContentTitle(“PhotoApplication”).setContentText(“您的照片已上载!”);
Intent resultIntent=新的Intent(getApplicationContext(),MainActivity.class);
PendingEvent ResultPendingEvent=PendingEvent.getActivity(getApplicationContext(),0,resultIntent,PendingEvent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingContent);
int mNotificationId=002;
NotificationManager mNotifyMgr=(NotificationManager)getSystemService(通知服务);
mNotifyMgr.notify(mNotificationId,mBuilder.build());
运行=错误;
}
完成();
返回null;
}
受保护的void onPostExecute(字符串文件\u url){
}
}
类DeleteProduct扩展了异步任务{
受保护的字符串doInBackground(字符串…args){
成功;
试一试{
List params=new ArrayList();
添加(新的BasicNameValuePair(“timestamp”,timestamp));
JSONObject json=jsonParser.makeHttpRequest(url_delete_product,“POST”,params);
Log.d(“删除产品”,json.toString());
success=json.getInt(“success”);
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
}

应用程序正在完全工作,应用程序唯一缺少的是“工作取消”按钮。它正在破坏通知,但照片仍在上传。

我想这就是原因,你只是取消了通知。取消(true)可能不会取消正在进行的异步任务。为了确认,请将一些日志放入onCancelled()。然后尝试像这样取消异步任务。这不起作用,即使我手动更改running=false照片仍在上载我猜这是原因,您只是取消了通知。取消(true)可能不会取消正在进行的异步任务。为了确认,请将一些日志放入onCancelled()。然后尝试像这样取消异步任务。这不起作用,即使我手动更改running=false照片仍在上载