单击时,通知打开活动中的android进度条

时间:2014-06-30 05:03:05

标签: android notifications

我只是想知道是否有可能在通知中创建进度条并在点击它时打开一个活动。

我们来做个样子: 我有活动A,B和C,其中C是带有进度条和通知的文件上传活动。

因此,当我单击HOME按钮然后单击通知图标时,我想返回活动C,单击后退按钮转到活动B并再次单击转到活动A.

这是我的Fileupload和通知类

活动C

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_c);

    WindowManager.LayoutParams params = getWindow().getAttributes();
    params.x = -20;

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        params.height = 750;
        params.width = 550;
    }
    else
    {
        params.height = 550;
        params.width = 750;
    }

    params.y = -10;

    this.getWindow().setAttributes(params);

    pg = (ProgressBar) findViewById(R.id.progressBar1);
    pg.setProgress(0);
    new RunProgress().execute();
}

public class RunProgress extends AsyncTask<String, String, String>
{
    @Override
    protected void onPreExecute()
    {
        if(extraFlag == 0)
        {
            // Call Notification method
            // backup is executed
            displayNotification();
        }

        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params)
    {
        for (int i = 0; i < fileName.size(); i++)
        {
            name = fileName.get(i);

            WebDav writer = new WebDav(getApplicationContext(),
                    name);
            try
            {
                x += percentage;
                publishProgress("" + x);
                writer.uploadFile(name);

                if (isCancel)
                    break;
                File file = new File(new FilePathClass().returnSimple()
                            + name);
                file.delete();
            }
            catch (Exception e)
            {
                system.out.println(e.toString());
            }
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(String... progress)
    {
        pg.setProgress(Integer.parseInt(progress[0]));
    }

    @Override
    protected void onPostExecute(String result)
    {
        //  This makes Notification icon CLEAR
        myNotificationManager.cancelAll();
    }
}

and notification method
private void displayNotification()
{
    n = new Notification.Builder(getApplicationContext());
    n.setContentTitle(getResources().getString(R.string.msg_action_bar_title));
    n.setTicker("Uploading...");
    n.setContentText("Upload progress started");
    n.setSmallIcon(R.drawable.ic_launcher);

// This makes return to Same Activity 
// when clicking notification icon
    Intent i = new Intent(getApplicationContext(), ActivityC.class);
//      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.putExtra("extraFlag", 1);
    i.putStringArrayListExtra("fileName", fileName);
    i.putExtra("numberOfContacts", fileName.size());

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(ActivityC.this);
    stackBuilder.addParentStack(ActivityC.class);
    stackBuilder.addNextIntent(i);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);
    n.setContentIntent(pendingIntent);

    myNotificationManager = (NotificationManager) getSystemService(ActivityC.NOTIFICATION_SERVICE);
    if (currentApiVersion < 16)
        myNotificationManager.notify(0, n.getNotification());
    else
        myNotificationManager.notify(0, n.build());
}

1 个答案:

答案 0 :(得分:0)

在通知对象上调用setLatestEventInfo(),提供PendingIntent,当他们点击通知抽屉中的条目时,会启动您的活动。 Check out this sample.