Android - How to fill progress bar from background service
<p>How to fill progress bar percentage from a background service in android. I have a expandable listview with an icon of download in each of child item. On click of that icon, I need to download the media with respect to that item. If the user clicks on the download icon and immediately close the app then we need to still download the resource and show a notification once it's downloaded. but I couldn't figure out how to fill the progress bar from the background service. Please help me out.</p>

<p>Below is code :</p>

<p>for service </p>

<pre><code>public class MyService extends Service {

    public MyService() {
    }
    static int i =0;


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //TODO do something useful
        System.out.println("Intent values .... "+intent.getStringExtra("KEY1"));


        new DownloadResource().execute();
        return Service.START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onDestroy(){
        super.onDestroy();

    }

    public class DownloadResource extends AsyncTask&lt;String, Integer, String&gt; {
        @Override
        protected String doInBackground(String... strings) {
            //download the file here 
            return null;
        }

        protected void onPostExecute(String result) {
            Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
            NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            final Notification.Builder builder = new Notification.Builder(getBaseContext());
            builder.setContentTitle("Lanes");
            builder.setContentText("Starting ...");
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setAutoCancel(true);
            inboxStyle.setBigContentTitle("Enter Content Text");
            inboxStyle.addLine("hi events "+i);
            builder.setStyle(inboxStyle);
            nManager.notify("App Name",1000,builder.build());
        }


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

            //need to fill progres update here 
            super.onProgressUpdate(values);
        }
        }

    }
</code></pre>

<p>My Activity code : </p>

<pre><code>public class MainActivity2 extends AppCompatActivity {

  ExpandableListView expandableListView;
    ExpandableListAdapter expandableListAdapter;
    List&lt;String&gt; expandableListTitle;
    HashMap&lt;String, List&lt;String&gt;&gt; expandableListDetail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.android_listview);
        expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
        expandableListDetail = ExpandableListDataPump.getData();
        expandableListTitle = new ArrayList&lt;String&gt;(expandableListDetail.keySet());
        expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail);
        expandableListView.setAdapter(expandableListAdapter);
        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        expandableListTitle.get(groupPosition) + " List Expanded.",
                        Toast.LENGTH_SHORT).show();
            }
        });

        expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        expandableListTitle.get(groupPosition) + " List Collapsed.",
                        Toast.LENGTH_SHORT).show();

            }
        });

        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                Toast.makeText(
                        getApplicationContext(),
                        expandableListTitle.get(groupPosition)
                                + " -&gt; "
                                + expandableListDetail.get(
                                expandableListTitle.get(groupPosition)).get(
                                childPosition), Toast.LENGTH_SHORT
                ).show();
                return false;
            }
        });
    }
    }
</code></pre>

<p>activity xml file :</p>

<pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" &gt;
    &lt;ExpandableListView
        android:id="@+id/expandableListView"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
        android:divider="@android:color/darker_gray"
        android:dividerHeight="0.5dp" /&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p>Adapter : </p>

<pre><code>public class CustomExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List&lt;String&gt; expandableListTitle;
    private HashMap&lt;String, List&lt;String&gt;&gt; expandableListDetail;

    public CustomExpandableListAdapter(Context context, List&lt;String&gt; expandableListTitle,
                                       HashMap&lt;String, List&lt;String&gt;&gt; expandableListDetail) {
        this.context = context;
        this.expandableListTitle = expandableListTitle;
        this.expandableListDetail = expandableListDetail;
    }

    @Override
    public Object getChild(int listPosition, int expandedListPosition) {
        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                .get(expandedListPosition);
    }

    @Override
    public long getChildId(int listPosition, int expandedListPosition) {
        return expandedListPosition;
    }

    @Override
    public View getChildView(int listPosition, final int expandedListPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        final String expandedListText = (String) getChild(listPosition, expandedListPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_item, null);
        }
        final TextView expandedListTextView = (TextView) convertView
                .findViewById(R.id.expandedListItem);
        ImageView imageView =(ImageView) convertView.findViewById(R.id.download);
       final ProgressBar progressBar = (ProgressBar) convertView.findViewById(R.id.progressBar_cyclic);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                progressBar.setVisibility(View.VISIBLE);
                Intent i =new Intent(context,MyService.class);
                i.putExtra("KEY1",expandedListTextView.getText().toString());
                context.startService(i);
            }
        });
        expandedListTextView.setText(expandedListText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int listPosition) {
        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                .size();
    }

    @Override
    public Object getGroup(int listPosition) {
        return this.expandableListTitle.get(listPosition);
    }

    @Override
    public int getGroupCount() {
        return this.expandableListTitle.size();
    }

    @Override
    public long getGroupId(int listPosition) {
        return listPosition;
    }

    @Override
    public View getGroupView(int listPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String listTitle = (String) getGroup(listPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_group, null);
        }
        TextView listTitleTextView = (TextView) convertView
                .findViewById(R.id.listTitle);
        listTitleTextView.setTypeface(null, Typeface.BOLD);
        listTitleTextView.setText(listTitle);
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int listPosition, int expandedListPosition) {
        return true;
    }
}
</code></pre>

<p>Adapter group xml file:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"&gt;
    &lt;TextView
        android:id="@+id/listTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textColor="@android:color/black"
        android:paddingTop="10dp"
        android:paddingBottom="10dp" /&gt;
&lt;/LinearLayout&gt;
</code></pre>

<p>Adapter child xml :</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dp"
    &gt;
    &lt;TextView
        android:id="@+id/expandedListItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:text="hdhdh"
         /&gt;
    &lt;ImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:id="@+id/download"
        android:layout_alignParentRight="true"
        android:layout_marginTop="13dp"
        android:layout_marginRight="13dp"
        android:src="@mipmap/ic_cloud_download_black_24dp"
        /&gt;
    &lt;ProgressBar
        android:id="@+id/progressBar_cyclic"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:minHeight="50dp"
        android:minWidth="50dp"
        android:layout_alignParentRight="true"
        android:visibility="gone"
        /&gt;
&lt;/RelativeLayout&gt;
</code></pre>