AlertDialog和服务

时间:2016-07-14 10:13:25

标签: android service android-alertdialog

您好我正在开发一个Android应用程序,我使用服务上传多个图像到服务器,这意味着我上传图像到服务器甚至我的应用程序从背景关闭.. 此外,我还显示了Nootification并显示了通知中图像上传的进度。 主要问题是我在完成所有上传后显示警告对话框。但是当我关闭应用程序时,屏幕上也会出现该对话框。 即 如果我暂停应用程序或隐藏应用程序,那么该警告对话框将显示在主屏幕

我想显示警告对话框

这是我的服务

 static public class UploadService extends Service {
    private String LOG_TAG = "BoundService";
    private IBinder mBinder = new MyBinder();
    ArrayList<CustomGallery> listOfPhotos;
    int i = 0;
    long totalSize=0;
    NotificationManager manager;
    String response_str = null;
    long totalprice = 0;
    Notification.Builder builder;
    SelectedAdapter_Test selectedAdapter;
    NotificationCompat.Builder mBuilder;
    String strsize, strtype, usermail, total, strmrp, strprice, strlab, strcity, abc, strdel_type, struname, imageName;
    ArrayList<CustomGallery> dataT = new ArrayList<CustomGallery>();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.v(LOG_TAG, "in onBind");
        return mBinder;
    }


    @Override
    public void onRebind(Intent intent) {
        Log.v(LOG_TAG, "in onRebind");
        super.onRebind(intent);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.v(LOG_TAG, "in onUnbind");
        return true;
    }


    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        selectedAdapter = new SelectedAdapter_Test(getApplicationContext(), dataT);
        Toast.makeText(UploadService.this, "Service Started ", Toast.LENGTH_SHORT).show();
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        listOfPhotos = (ArrayList<CustomGallery>) intent.getSerializableExtra("listof");
        strsize = intent.getStringExtra("strsize");
        strtype = intent.getStringExtra("strtype");
        usermail = intent.getStringExtra("user_mail");
        strmrp = intent.getStringExtra("strmrp");
        strprice = intent.getStringExtra("strprice");
        strlab = intent.getStringExtra("strlab");
        strcity = intent.getStringExtra("strcity");
        struname = intent.getStringExtra("strusername");
        strdel_type = intent.getStringExtra("strdel_type");
        abc = intent.getStringExtra("foldername");


        manager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setContentTitle("Picture Upload")
                .setContentText("Upload in progress")
                .setSmallIcon(R.drawable.ic_launcher);
        Intent resultIntent = new Intent(this, SelectPhotos.class);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        resultIntent.setAction("android.intent.action.MAIN");
        resultIntent.addCategory("android.intent.category.LAUNCHER");

        new UploadFileToServer().execute();



        return Service.START_NOT_STICKY;
    }


    public class MyBinder extends Binder {
        UploadService getService() {
            return UploadService.this;
        }
    }



    private class UploadFileToServer extends AsyncTask<String, Integer, String> {
        @Override
        protected void onPreExecute() {
            // setting progress bar to zero

            super.onPreExecute();

            pb.setProgress(0);
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {


            Log.v("Abhijit122", "" + String.valueOf(progress[0]) + "%");
            pb.setProgress(progress[0]);
            tp.setText(String.valueOf(progress[0]) + "%");
            Log.e("ef", "df" + incr);
            ti.setText((incr+1)+"/"+listOfPhotos.size());
        }

        @Override
        protected String doInBackground(String... params) {
            return uploadFile();
        }

        @SuppressWarnings("deprecation")
        private String uploadFile() {

            for (incr = 0; incr < listOfPhotos.size(); incr++) {
                mBuilder.setProgress(listOfPhotos.size(), incr, false);
                manager.notify(1, mBuilder.build());
                try {
                    File f = new File(listOfPhotos.get(i).sdcardPath.toString());
                    int j = i + 1;
                    j++;
                    imageName = f.getName();
                    totalprice = totalprice + Long.parseLong(strprice);
                    total = String.valueOf(totalprice);
                    Log.e("Totalprice", " " + total);
                    String responseString = null;
                    final HttpClient httpclient = new DefaultHttpClient();

                    final HttpPost httppost = new HttpPost(URL);     //TODO - to hit URL);
                    AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
                            new AndroidMultiPartEntity.ProgressListener() {

                                @Override
                                public void transferred(long num) {
                                    publishProgress((int) ((num / (float) totalSize) * 100));
                                }
                            });
                    File sourceFile = new File(listOfPhotos.get(i).sdcardPath);
                    // Adding file data to http body
                    entity.addPart("image", new FileBody(sourceFile));
                    entity.addPart("foldername", new StringBody(abc));
                    entity.addPart("size",
                            new StringBody(strsize));
                    entity.addPart("type",
                            new StringBody(strtype));
                    entity.addPart("username",
                            new StringBody(usermail));
                    entity.addPart("total",
                            new StringBody(total));
                    Log.e("Totalprice", "adf " + total);
                    entity.addPart("mrp",
                            new StringBody(strmrp));
                    entity.addPart("price",
                            new StringBody(strprice));
                    entity.addPart("lab",
                            new StringBody(strlab));
                    entity.addPart("city",
                            new StringBody(strcity));
                    entity.addPart("imagename",
                            new StringBody(imageName));
                    entity.addPart("deltype",
                            new StringBody(strdel_type));

                    entity.addPart("initflag",
                            new StringBody(""+initflag++));
                    entity.addPart("lab_username",
                            new StringBody(struname));
                    totalSize = entity.getContentLength();
                    httppost.setEntity(entity);

                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity r_entity = response.getEntity();
                    response_str = EntityUtils.toString(r_entity);
                    Log.d("Dhruva", "" + response_str);
                    if (r_entity != null) {
                        Log.v("Abhijit", "" + response_str);
                    }
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return   response_str;

        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            mBuilder.setContentText("Upload complete")
                    // Removes the progress bar
                    .setProgress(0, 0, false);
            manager.notify(1, mBuilder.build());


            AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext())
                    .setTitle("Success")
                    .setMessage("Successfully uploaded images...")
                    .setCancelable(false)
                  .create();

            alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            alertDialog.show();

        }

    }


}

我希望在完成所有上传后显示警告对话框,当我点击确定按钮时,我想开始下一个活动。

这是我在通过服务成功上传图片后打开下一个活动的方式。此外,我实施了通知,然后当我点击通知时我应该打开哪个活动

如果还有其他方式,请给我建议。

0 个答案:

没有答案