Run service even if the app is not open

Asked

Viewed 67 times

1

I am creating an alarm clock app and in the tests I would like it to wake up (in this example it still vibrates), but I am not able to make it run in the background and so does not end up awakening, how can I make sure that even if the app has been destroyed it keeps checking the time and if the time beats then it manifests itself, the way the code is seeing two problems: it is waiting for a certain trigger to be activated and ends up not awakening if the screen is in Sleep even if the app is open, and consequently does not run the validation of the time if the app is closed, just installed as an alarm clock even: 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 that it vibrates in the pre-programmed schedule even the app has passed by the state.

  • See the Ramaral response on this link https://answall.com/questions/281880/alarmmanager-executableno-intervado-configured. .... sometimes answers your question.

  • Look at this question

1 answer

-2

Try to put this attribute in your tag.

<service android:name="service" android:stopWithTask="true"/>
  • 4

    The answer should only be provided when there is certainty that it will resolve the question, otherwise use the comments....

  • 1

    Andrey, unless you change the stackoverflow setting so I can comment on having created my account 15 minutes ago, I will post the same answer. And the solution worked for me.

  • 1

    Buddy, unless you read the Sopt usage guidelines, it’s hard to help you. I’m not criticizing you, I’m not denying your response, I’m just showing you how to act here on the site. And no, you should not use subterfuges to give a comment if you are not yet allowed to do so. Learn to deal with constructive criticism...

  • 1

    Thank you, it was very constructive.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.