如何在通知进度条上处理待处理的意图?

时间:2015-07-09 07:51:02

标签: android android-intent android-pendingintent android-popupwindow

在我的应用程序中,我将图像上传到服务器,通知进度条显示上传进度。当用户点击通知时,弹出窗口将显示取消或继续上传。所有这些都正常工作。当我点击后退按钮时,当我在上一次活动时,如果我点击通知弹出窗口将不会显示而只是加载活动文件上传。我怎样才能解决这个问题??当用户点击通知时一个挂起的Intent将被传递,并且onNewIntent方法我正在调用弹出窗口方法。当我处理文件上传活动时,这工作正常。我不知道实际问题是什么。我认为未决的意图是交易与我的onCreate方法的另一个意图,任何人都可以帮助??

对通知进度的待定意图

 public void ProgressBarNotification() {
            mBuilder = new NotificationCompat.Builder(ImageUploadActivity.this);
            mBuilder.setContentTitle("Upload")
                        .setContentText("Upload in progress")
                        .setSmallIcon(R.drawable.ic_launcher);

  mBuilder.setProgress(100, 0, false);

    mBuilder.setAutoCancel(true);
  Intent myIntent = new Intent(this, ImageUploadActivity.class);  //Pending Intent
  myIntent.putExtra("first_state",3);
  PendingIntent pendingIntent = PendingIntent.getActivity(ImageUploadActivity.this, 1, myIntent, PendingIntent. FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(pendingIntent);
    mNotifyManager.notify(MY_NOTIFICATION_ID, mBuilder.build());
}

onNewIntent方法

 @Override
 protected void onNewIntent(Intent intent) {
   super.onNewIntent(intent);
    //new Intent(this, ImageUploadActivity.class);
   Bundle extras = intent.getExtras();
   state = extras.getInt("first_state");

      if(state==3)
  { 
   initiatePopupWindow();
   }}

InitiatePopupwindow方法

 public void initiatePopupWindow() {
    try {

        LayoutInflater inflater = (LayoutInflater) ImageUploadActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.popupmenu,
                (ViewGroup) findViewById(R.id.popup_element));
        pwindo = new PopupWindow(layout, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
        pwindo.setFocusable(true);
        pwindo.setOutsideTouchable(true);
        Button btnStopUpload = (Button) layout.findViewById(R.id.btn_upload);
        btnStopUpload.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                mNotifyManager.cancel(MY_NOTIFICATION_ID);
                //  mTask.cancel(true);
                Log.e(TAG, "Notification Cancelled ");
                // mTask.cancel(true);
                Toast.makeText(getApplicationContext(),
                        "Upload Cancelled", Toast.LENGTH_SHORT).show();
                ImageUploadActivity.this.finish();
            }
        });


        Button btnCancelPopup = (Button) layout.findViewById(R.id.btn_cancel);

        btnCancelPopup.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                pwindo.dismiss();

            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private View.OnClickListener cancel_button_click_listener = new View.OnClickListener() {
    public void onClick(View v) {
        pwindo.dismiss();

    }
};

1 个答案:

答案 0 :(得分:0)

ImageUploadActivity回来时,它已从活动后台堆栈中删除,因此当用户点击通知时,pendingIntent会创建活动的新实例。OnNewIntent()始终会调用singleTop/Task除了第一次创建活动时的活动。那时调用onCreate

onCreate()的{​​{1}}中,您可以致电ImageUploadActivity,在onNewIntent(getIntent())中检查onNewIntent()是否为空且包含extras。< / p>