Run service even though app is not open

1

I'm creating a wakeup app and in tests I would like it to wake up (in this example it still only vibrates), but I'm not able to make it run in the background and so it does not end up waking up, how can I even make it app has been destroyed it keeps checking the time and if the schedule knocks then it manifests itself, the way the code is I see two problems: it is waiting for a certain trigger to be activated and it does not wake up if the screen is in the same sleep that the app is open, and therefore does not run the time validation if the app is closed, only installed as an alarm: AndroidManifest.xml

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    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>

    <service android:name="com.rafah.screenstate.LockService">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </service>
</application>

MainActivity.class

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //starting the service Class
    startService(new Intent(getApplicationContext(), LockService.class));

   }
}

LockService.class

public class LockService extends Service {

   @Nullable
   @Override
   public IBinder onBind(Intent intent) {
       return null;
   }

   @Override
   public void onCreate() {
       super.onCreate();
   }

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
       final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
       filter.addAction(Intent.ACTION_SCREEN_OFF);
       filter.addAction(Intent.ACTION_USER_PRESENT);

       //Calling the BroadCast
       final BroadcastReceiver mReceiver = new ScreenReceiver();
       registerReceiver(mReceiver, filter);

       return super.onStartCommand(intent, flags, startId);
   }

   //AINDA NÃO SEI PARA QUE SERVE
   public class LocalBinder extends Binder{
       LockService getService(){
           return LockService.this;
       }
   }

}

ScreenReceiver.class

public class ScreenReceiver extends BroadcastReceiver {

public static boolean wasScreenOn = true;

@Override
public void onReceive(Context context, Intent intent) {
    Log.e("test", "onReceive Broadcast");

    SimpleDateFormat Dformat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    Date date = new Date();

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    Date data_atual = cal.getTime();
    String sysdate  = Dformat.format(data_atual);

    if (sysdate.equals("03/04/2018 22:09:02")){
        //Vibrate test
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(6000);
     }

   }
}

What I need is for it to vibrate at the pre-programmed schedule even though the app has gone through the destroy state.

    
asked by anonymous 04.04.2018 / 03:17

1 answer

-2

Try to put this attribute in your tag.

<service android:name="service" android:stopWithTask="true"/>
    
04.04.2018 / 18:18