2016-12-08 5 views
0

Android 서비스에서 백그라운드 서비스의 진행률 백분율을 채우는 방법. 각 하위 항목에 다운로드 아이콘이있는 확장 가능한 목록보기가 있습니다. 해당 아이콘을 클릭하면 해당 항목과 관련하여 미디어를 다운로드해야합니다. 사용자가 다운로드 아이콘을 클릭하고 즉시 응용 프로그램을 닫으면 자원을 다운로드하고 다운로드 한 후에 알림을 표시해야합니다. 백그라운드 서비스에서 진행률 표시 줄을 채우는 방법을 알아낼 수 없었습니다. 제발 도와주세요. 서비스에 대한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> 

어댑터 :

아래

코드입니다 16,

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> 

어댑터 자식 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> 
+0

서비스가 UI와 함께 작동하지 않아야합니다. 'ProgressBar'를 업데이트하기 위해 Activity로 메시지를 보내야합니다 –

+0

ur url 오류 로그 – Nithinlal

+0

몇 가지 코드를 제공해 주시겠습니까? 귀하의 질문은 무엇인가? – Nico

답변

0

당신이 찾고있는 시스템의 구성 요소 사이의 통신에 사용되는 도구를 사용해야합니다. Android는 LocalBroadcastManager을 제공합니다. 일부는 이벤트 버스 라이브러리를 사용하는 것을 선호합니다. 이 도구 중 하나를 사용하는 방법을 배우고 구성 요소를 연결하여 상태 업데이트를 게시하고 다른 업데이트를받을 수 있도록해야합니다.