Reputation: 117
I'm trying to keep the vibrator run even after sleep mode (screen locks), but the app won't work.I don't know what i'm missing..
Is there any solutions other than Wake Lock,and BroadcastReceiver?
(NO prejudgements please,it's vibrate once every 4:57 minutes)
public class MainActivity extends Activity {
public BroadcastReceiver vibrateReceiver = new 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);
}
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Upvotes: 4
Views: 688
Reputation: 4993
Just wanted to add the information that you can give the user a distinct and unique haptic effect by using the free haptic effect library from Immersion Corp. That will help the user understand that this is coming from your app, and not a regular notification etc (without needing a 3 second long buzz).
That library contains 120+ fine-tuned haptic effects, and you can download it from here with a quickstart guide to linking the library and calling it over here.
Upvotes: 0
Reputation: 3684
First of all create your service scheduler based on for instance alarm service. Sth. like that.
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 your broadcast receiver in 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 that scheme and you will succeed!
Upvotes: 1
Reputation: 3223
BroadcastReceiver.onReceive()
is not designed for long operations. You should call vibrate()
somewhere else (in a Service
or better an IntentService
, for that matter).
Also, the vibration pattern takes too long to run. If I was you, I'd schedule vibrate()
to run a short pattern every 4:57 via Android's scheduling mechanism, called AlarmManager
. The idea is, write a BroadcastReceiver.onReceive()
that calls startService(yourservice)
and schedule that BroadcastReceiver to be run each time (or better, since you will need a partial wake lock to circumvent sleep mode write a WakefulBroadcastReceiver.onReceive()
that calls startWakefulService()
). Don't forget to call YourWakefulBroadcastReceiver.completeWakefulIntent()
at the end of your service to release the wake lock.
If you still want your service to execute a vibration pattern that runs that long, the idea is the same.
Upvotes: 0