Reputation: 61
i want to start the service when the application is closed.
java code
public class AudioRecorder extends Service {
public static final String SENDER_ID = "274211616343";
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, " MyService Created ", Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, " MyService Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
}
Upvotes: 2
Views: 81
Reputation: 691
You've written in comment: "i want to start the service when the application closed by the server side data recieved in the application"
In this case why do not you use AlarmManager like this (it's a Kotlin):
private fun startServiceAndExit() {
val startService = Intent(appContext, YourService::class.java)
val pendingIntentId = 123456
PendingIntent pendingIntent = PendingIntent.getService(
appContext,
pendingIntentId,
intent,
0);
val mgr = appContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, pendingIntent)
System.exit(0)
}
Upvotes: 0