Android - 如何从后台服务填写进度条

时间:2016-12-08 13:26:47

标签: android android-service android-progressbar

如何从android中的后台服务填充进度条百分比。我有一个可扩展的列表视图,每个子项目都有一个下载图标。点击该图标,我需要下载与该项目相关的媒体。如果用户点击下载图标并立即关闭应用程序,那么我们仍然需要下载资源并在下载后显示通知。但我无法弄清楚如何从后台服务填充进度条。请帮帮我。

以下是代码:

服务

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

    }

我的活动代码:

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

活动xml文件:

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

适配器:

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

适配器组xml文件:

<?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>

1 个答案:

答案 0 :(得分:0)

您应该找到并使用一个用于在系统中的组件之间进行通信的工具。 Android提供LocalBroadcastManager。有些人更喜欢使用活动总线库。您将必须学习如何使用这些工具之一,并连接组件,以便可以发布状态更新,另一个可以接收它们。