Skip to content

Instantly share code, notes, and snippets.

@Sp4Rx
Created September 25, 2016 09:37
Show Gist options
  • Save Sp4Rx/0cede56375801ecd63e83a3344c7609f to your computer and use it in GitHub Desktop.
Save Sp4Rx/0cede56375801ecd63e83a3344c7609f to your computer and use it in GitHub Desktop.
ServiceDemo 0.2
package com.example.suvajit.servicedemo;
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() {
Toast.makeText(BackgroundService.this, "Service Will Be Restarted...", Toast.LENGTH_SHORT).show();
sendBroadcast(new Intent("RestartMe"));
}
}
package com.example.suvajit.servicedemo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.firebase.iid.FirebaseInstanceId;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnShowToken = (Button) findViewById(R.id.buttonShowToken);
btnShowToken.setOnClickListener(this);
Intent intentService = new Intent(this,BackgroundService.class);
startService(intentService);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.buttonShowToken:
//Code to show Token
String token = FirebaseInstanceId.getInstance().getToken();
Toast.makeText(MainActivity.this, token, Toast.LENGTH_SHORT).show();
Log.i("MainActivity: FB Token",token);
break;
}
}
}
package com.example.suvajit.servicedemo;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class RestartBackgroundService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// I have also used AlarmManager , but it doesn't make any difference for me
// AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, BackgroundService.class), PendingIntent.FLAG_UPDATE_CURRENT);
// int interval = 5000;
// am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, interval, pi);
context.startService(new Intent(context.getApplicationContext(),BackgroundService.class));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment