Skip to content

Instantly share code, notes, and snippets.

@Sp4Rx
Created September 25, 2016 09:22
Show Gist options
  • Save Sp4Rx/58a9c899ec838904d0a0ef52623e455f to your computer and use it in GitHub Desktop.
Save Sp4Rx/58a9c899ec838904d0a0ef52623e455f to your computer and use it in GitHub Desktop.
ServiceDemo 0.1
package com.example.suvajit.servicedemopersistant;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;
public class BackgroundService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(BackgroundService.this, "Service Started...", Toast.LENGTH_SHORT).show();
return START_STICKY;
}
@Override
public void onDestroy() {
Intent intentService = new Intent(this,BackgroundService.class);
startService(intentService);
Toast.makeText(BackgroundService.this, "Service Will Be Restarted...", Toast.LENGTH_SHORT).show();
}
}
package com.example.suvajit.servicedemopersistant;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this,BackgroundService.class);
startService(intent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment