First of all, create your service planner based, for example, on alert services. STH. like this.
public class ScheduledLocalisationExecutor {
private Context context;
private AlarmManager alarmManager;
private Intent broadcastIntent;
private PendingIntent pendingIntent;
private DbxStart dbxStart;
public ScheduledLocalisationExecutor(Context appContext) {
context = appContext;
dbxStart = new DbxStart();
}
public void setUpScheduledService(long updateTime) {
if (dbxStart.getOpenedDatastore() == null) {
Log.e("DROPBOX", "Dropbox account is not linked...");
return;
}
Log.w("scheduled factory","updating Service!");
broadcastIntent = new Intent(context, LocalisationUpdatesReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis() + updateTime, pendingIntent);
}
}
Now register the broadcast receiver in the Android manifest.
<receiver android:name=".receivers.LocalisationUpdatesReceiver">
</receiver>
And create your broadcast receiver.
public class LocalisationUpdatesReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
long[] pattern = {0, 3000, 297000};
v.vibrate(pattern, 0);
}
}
}
Follow this pattern and you will succeed!
source
share