'how do i start multiple same services to run at the same time?
i need to start a service whenever the user wants, its the same service with only different input data. I tried with workmanager and using threads libraries, its important to say that is a long running task. Down below is my code
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startThread(View view){
Value1 = findViewById(R.id.Value1);
Value2 = findViewById(R.id.Value2);
String value1 = Value1.getText().toString();
String Value2 = Value2.getText().toString();
Intent service = new Intent(this, servicio.class);
service.putExtra("value1", value1);
service.putExtra("value2", value2);
startService(service);
service file
public class servicio extends Service {
private static final String TAG = "service";
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String rapida = intent.getStringExtra("value1");
String lenta = intent.getStringExtra("value2");
int value = Integer.parseInt(rapida);
int to = Integer.parseInt(lenta);
for (int i = value; i<to; i++){
Log.d(TAG, "number: " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Intent notificationintent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationintent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Counting")
.setContentText("from" + rapida + "to" + lenta)
.setSmallIcon(R.drawable.ic_android)
.setContentIntent(pendingIntent)
.build();
startForeground(new Random().nextInt(), notification);
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
i need to make to run multiple of this at the same time no matter with what.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
