Skip to content

Instantly share code, notes, and snippets.

@talhahasanzia
Last active December 6, 2016 16:28
Show Gist options
  • Save talhahasanzia/559f1c1aec1b4dcd8fd768bce19621dc to your computer and use it in GitHub Desktop.
Save talhahasanzia/559f1c1aec1b4dcd8fd768bce19621dc to your computer and use it in GitHub Desktop.
Background service sample
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testbed.projects.stackoverflow">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//here service is declared
<service android:name=".BackgroundService">
</service>
</application>
</manifest>
public class BackgroundService2 extends Service {
private static BroadcastReceiver mTickReceiver;
public BackgroundService2()
{
}
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onCreate()
{
registerReceiver();
}
@Override
public void onDestroy()
{
unregisterReceiver(mTickReceiver);
mTickReceiver = null;
}
private void registerReceiver()
{
mTickReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) // this will fire every minute
{
Log.d("TAG", "TICKS");
THIS CODE WILL RUN EVERYMINUTE
1. CALL METHOD FOR GET DATA OF ALL ENTRIES
AFTER GETTING DATA UPDATE LIST IN ANDROID
2. CALL METHOD TO GET MY_CUSTOMERS
IF MY_CUSTOMERS IS NOT EMPTY
CALL METHOD TO GET THAT SPECIFIC CUSTOMER DETAILS
SHOW USER THAT DETAILS AND CONTACTS
// do location update, data sync etc
// do something, e.g. send Intent to main app
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK); // this will broadcast Intent every minute
registerReceiver(mTickReceiver, filter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment