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.
Below is code :
for service
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<String, Integer, String> {
@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);
}
}
}
My Activity code :
public class MainActivity2 extends AppCompatActivity {
ExpandableListView expandableListView;
ExpandableListAdapter expandableListAdapter;
List<String> expandableListTitle;
HashMap<String, List<String>> 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<String>(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)
+ " -> "
+ expandableListDetail.get(
expandableListTitle.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT
).show();
return false;
}
});
}
}
activity xml file :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<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" />
</LinearLayout>
Adapter :
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<String>> 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;
}
}
Adapter group xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<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" />
</LinearLayout>
Adapter child xml :
<?xml version="1.0" encoding="utf-8"?>
<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"
>
<TextView
android:id="@+id/expandedListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:text="hdhdh"
/>
<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"
/>
<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"
/>
</RelativeLayout>
You should find and use a tool that's used for communicating between components in a system. Android provides LocalBroadcastManager. Some prefer to use an event bus library. You will have to learn how to use one of these tools, and wire up your components so that one can publish status updates and the other can receive them.