如何在通知进度条上处理待处理的意图?(How to Handle Pending Intent on Notification Progress bar?)

编程入门 行业动态 更新时间:2024-10-28 16:28:25
如何在通知进度条上处理待处理的意图?(How to Handle Pending Intent on Notification Progress bar?)

在我的应用程序中,我将图像上传到服务器,通知进度条显示上传进度。当用户点击通知时,弹出窗口将显示取消或继续上传。所有这些都正常工作。当我单击后退按钮,当我在上一个活动时,如果我点击通知弹出窗口将不会显示而只是加载活动文件上传。我怎么能解决这个问题??当用户点击通知时挂起的意图将被传递,并在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(); } };

In my app I am uploading an image to a server,a notification progress bar is there to show upload progress.when a user click on the notification ,a popup window will show to cancel or continue upload.All these are working fine.When I click back button and when I am at previous activity,if I click notification the popup window will not be displayed instead it only load the Activity for file Upload.How can I solve this issue??.When user clicks on the notification a pending Intent will be passed and at onNewIntent method I am calling popup window method.This is working fine when I am on the file upload activity.I don't know what is the actual problem.I think the pending intent is deals with another intent in my onCreate method, Can anyone help??

Pending Intent on Notification progress

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 method

@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 method

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

最满意答案

从ImageUploadActivity按回来时,它已从活动后台删除,因此当用户单击通知时,pendingIntent会创建活动的新实例。 除了第一次创建活动时, OnNewIntent()总是被调用singleTop/Task活动。 那时onCreate被召唤。

在ImageUploadActivity onCreate() ,您可以调用onNewIntent(getIntent()) ,在onNewIntent()检查extras是否为null并且包含first_state 。

On pressing back from ImageUploadActivity it has been removed from the activity backstack so when the user click on notification the pendingIntent create a new instance of the activity.OnNewIntent() always get called for singleTop/Task activities except for the first time when activity is created. At that time onCreate is called.

In onCreate() of ImageUploadActivity you can call onNewIntent(getIntent()), In onNewIntent() check if extras isn't null and contains first_state.

更多推荐

本文发布于:2023-07-05 02:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1032137.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:意图   进度条   通知   如何在   Handle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!