3
I’m trying to implement this train and I’m not getting it. I wanted to do the same thing as the colleague who opened the topic, but I could not even follow these examples.
I have some questions: In which class do I set this Calendar bid? In the same receiver? from what I understood from the example, he is calling himself in this example of the topic(topic link):
Intent tarefaIntent = new Intent(context, ExecutarTarefaProgramadaReceiver.class);
What I would like: I am developing together with colleagues from the facul, a localization system. The idea is to check from time to time if there is an internet connection. If you have connection and information from our app not sent, then we will send the data to a webservice or anywhere else we imagine. This way I did, every time I unlock the screen of the phone it just opens the map. (but that’s not what I want, because I want the service to remain active and when "waking up(check a condition and if it is true executes)" perform a method).
Summary:
I would like my service to start on the phone boot and be "listening" until there is an internet connection. When there is, then do something - in that case, perform a method that I determine (which I also don’t know where to stand).
My Receive:
package com.teste.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import com.teste.MapsActivity;
/**
* Created by Jaquisson on 17/09/2015.
*/
public class TbReceiver extends BroadcastReceiver {
public static boolean wasScreenOn = true;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Intent intent = new Intent(context, MapsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
}
Meuservice is like this:
package com.teste.service;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.os.IBinder;
/**
* Created by Jaquisson-SENAC on 17/09/2015.
*/
public class TbService extends Service {
private BroadcastReceiver receiver;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
//IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
//filter.addAction(Intent.ACTION_SCREEN_OFF);
//filter.addAction(Intent.ACTION_USER_PRESENT);
//receiver = new TbReceiver();
//Log.d("service", "Receiver será iniciado");
//registerReceiver(receiver, filter);
//super.onCreate();
}
}
And down the stretch of my manifest:
<service
android:name=".service.TbService"
android:exported="false" >
</service>
<service
android:name=".BuscaLocalizacao"
android:exported="false" >
</service>
<receiver android:name=".receiver.TbReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
Dude, I’ve done a lot of research and I can’t figure out how to do it. I’ve looked at the documentation, but I don’t get it either. I’m starting out and a little lost in it.
I’m sorry to post this bunch of stuff, but I’m kind of detail-oriented.
Thank you.
I’ve done something similar and used the Observer design, will have to create a class that will be updated every time there is no internet connection or vice versa. Something of this kind of implementation : http://www.devmedia.com.br/padrao-projection-observer-java/26163
– user6796