Keep the vibrator in sleep mode

I'm trying to get the vibrator to work even after hibernation (screen lock), but the application will not work. I don’t know what is missing.

Are there any solutions besides Wake Lock and BroadcastReceiver?

(NO biased requests, it vibrates 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;
    }
}
+4
source share
3 answers

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!

+1
source

BroadcastReceiver.onReceive() . vibrate() - ( Service IntentService, ).

, . , vibrate() 4:57 Android, AlarmManager. , BroadcastReceiver.onReceive(), startService(yourservice), , BroadcastReceiver ( , , , WakefulBroadcastReceiver.onReceive(), startWakefulService()), YourWakefulBroadcastReceiver.completeWakefulIntent() , .

, , , .

0

, , , Immersion Corp. , , .. ( 3- ).

120 + , .

0

All Articles