I design a List view like bellow. I learning android and I got a assinment.
---------------------------------------
File Name 1 0% Download Button
---------------------------------------
File Name 2 0% Download Button
---------------------------------------
File Name 3 0% Download Button
---------------------------------------
I have a program that use bound service and a counter in it that count 1% to 100% after clicking a button.
Now how I can use this program for each item of list view.
NOTE: I want to use bound service for this.
Bellow program have a start service button that start a bound service and send brodcast to ui to show 1% to 100% progress. I have to do it for each item in my listview or recyclerview.
In my above layout when I click download button it should start bound service and show the progress.
activity_main.xml
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="start_service"
android:text="Start" />
<TextView
android:id="@+id/showCount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView showCount;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showCount = findViewById(R.id.showCount);
Intent intent = new Intent(MainActivity.this, MyService.class);
bindService(intent, sConnection, BIND_AUTO_CREATE);
}
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String s1 = intent.getStringExtra("DATAPASSED");
showCount.setText(s1);
}
};
public void start_service(View view) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MyService.MY_ACTION);
registerReceiver(broadcastReceiver, intentFilter);
myService.startCounting();
super.onStart();
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart: ");
}
@Override
protected void onStop() {
super.onStop();
if (isBind) {
unbindService(sConnection);
isBind = false;
Log.d(TAG, "onStop: ");
}
}
MyService myService;
boolean isBind = false;
public ServiceConnection sConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
MyService.LocalBinder binder = (MyService.LocalBinder) iBinder;
myService = binder.getService();
isBind = true;
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
isBind = false;
}
};
}
MyService.java
public class MyService extends Service {
private static final String TAG = "MyService";
static final String MY_ACTION = "MY_ACTION";
public static final String CHANNEL_ID = "CHANNEL_ID";
public final IBinder binder = new LocalBinder();
public class LocalBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind: start gn");
return binder;
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate: ");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String input = "Supper Text";
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Notification Title")
.setContentText(input)
.setSmallIcon(R.drawable.notification_icon)
.setContentIntent(pendingIntent)
.build();
startForeground(0, notification);
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy: ");
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind: ");
return super.onUnbind(intent);
}
@Override
public void onRebind(Intent intent) {
Log.d(TAG, "onRebind: ");
super.onRebind(intent);
}
public void startCounting() {
if (!running) {
running = true;
new GenerateNumber().start();
}
}
public void stopCounting() {
if (running) {
running = false;
}
}
int generatedNumber;
boolean running;
class GenerateNumber extends Thread {
GenerateNumber() {}
@Override
public void run() {
while (running) {
generatedNumber++;
Log.d(TAG, "run: " + generatedNumber);
try {
Intent intent = new Intent();
intent.setAction(MY_ACTION);
intent.putExtra("DATAPASSED", String.valueOf(generatedNumber));
sendBroadcast(intent);
Thread.sleep(500);
if (generatedNumber >= 100) {
running = false;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}