我有一个启动我的服务的BroadcastReceiver,这个服务使用异步来发出一个HTTP请求,然后显示一个通知,但是我没有得到每次系统自动启动时运行的服务,然后每隔1分钟运行一次。
这是我的AndroidManifest:
<receiver android:name=".notificar.InicializarServico" android:exported="false"> <intent-filter> <action android:name="INICIALIZAR_SERVICO"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </receiver> <service android:enabled="true" android:name=".notificar.ServicoNotificar"> <intent-filter> <action android:name="SERVICO_ATUALIZAR"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </service>BroadcastReceiver的代码在这里:
public class InicializarServico extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.v("NAMURA LANCHES", "O SISTEMA FOI INICIADO"); Log.v("NAMURA LANCHES", "Iniciando o serviço de atualização"); // Preparar Intent para inicializar o serviço agendar(context, 5); Log.v("NAMURA LANCHES", "O Serviço sera iniciado em 15 segundos"); } private void agendar(Context context, int i) { Intent myIntent = new Intent(context, ServicoNotificar.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0); AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.add(Calendar.SECOND, i); // first time long frequency= i * 1000; // in ms alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), frequency, pendingIntent); } }这是我的服务班:
public class ServicoNotificar extends Service{ private String RequestURL = "http://sandbox.omeuprofissional.com/requests/meus_pedidos.php"; private final int duration = 3000; private static final String ACTION_RESCHEDULE = "br.com.namuralanches.android.intent.action.SERVICE_RESCHEDULE"; Handler myHandler; @Override public IBinder onBind(Intent intent) { return null; } Thread testThread; boolean threadRunning = false; @Override public void onCreate() { super.onCreate(); myHandler = new Handler(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("NAMURA LANCHES", "Service started"); myHandler.postDelayed(new Runnable() { @Override public void run() { SharedPreferences preferencias = getSharedPreferences("mesa", MODE_PRIVATE); String registro = preferencias.getString("REGISTRO_MESA", null); String hashMesa = preferencias.getString("HASH_MESA", null); if ((registro != null) || (hashMesa != null)) { new ListarCompras().execute(RequestURL, hashMesa, registro); } } }, 10000); stopSelf(startId); return START_NOT_STICKY; } @Override public void onDestroy() { Log.v("NAMURA LANCHES", "SERVICO DESTRUIDO"); super.onDestroy(); threadRunning = false; } }我现在做了一些改变,我迷失了方向,甚至没有看到服务创建的日志,只有日志BroadcastReceiver
I have a BroadcastReceiver that starts my service, this service it uses async to make an HTTP request and then display a notification, but I'm not getting the service to be run every time the system starts automatically, and then every 1 minute.
Here is my AndroidManifest:
<receiver android:name=".notificar.InicializarServico" android:exported="false"> <intent-filter> <action android:name="INICIALIZAR_SERVICO"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </receiver> <service android:enabled="true" android:name=".notificar.ServicoNotificar"> <intent-filter> <action android:name="SERVICO_ATUALIZAR"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </service>The code of BroadcastReceiver is here:
public class InicializarServico extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.v("NAMURA LANCHES", "O SISTEMA FOI INICIADO"); Log.v("NAMURA LANCHES", "Iniciando o serviço de atualização"); // Preparar Intent para inicializar o serviço agendar(context, 5); Log.v("NAMURA LANCHES", "O Serviço sera iniciado em 15 segundos"); } private void agendar(Context context, int i) { Intent myIntent = new Intent(context, ServicoNotificar.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0); AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.add(Calendar.SECOND, i); // first time long frequency= i * 1000; // in ms alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), frequency, pendingIntent); } }And here is my service class:
public class ServicoNotificar extends Service{ private String RequestURL = "http://sandbox.omeuprofissional.com/requests/meus_pedidos.php"; private final int duration = 3000; private static final String ACTION_RESCHEDULE = "br.com.namuralanches.android.intent.action.SERVICE_RESCHEDULE"; Handler myHandler; @Override public IBinder onBind(Intent intent) { return null; } Thread testThread; boolean threadRunning = false; @Override public void onCreate() { super.onCreate(); myHandler = new Handler(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("NAMURA LANCHES", "Service started"); myHandler.postDelayed(new Runnable() { @Override public void run() { SharedPreferences preferencias = getSharedPreferences("mesa", MODE_PRIVATE); String registro = preferencias.getString("REGISTRO_MESA", null); String hashMesa = preferencias.getString("HASH_MESA", null); if ((registro != null) || (hashMesa != null)) { new ListarCompras().execute(RequestURL, hashMesa, registro); } } }, 10000); stopSelf(startId); return START_NOT_STICKY; } @Override public void onDestroy() { Log.v("NAMURA LANCHES", "SERVICO DESTRUIDO"); super.onDestroy(); threadRunning = false; } }I made some changes now I'm lost, I'm not even see the log that the service was created, only the log BroadcastReceiver
最满意答案
您必须使用AlarmManager来启动您的服务。 要使用AlarmManager,您需要一个您想要警报触发的待处理Intent。 像这样:
首先,创建一个将由一个Intent触发的Receiver,
在您的活动中,创建触发Receiver的意图,
为触发接收器的意图创建一个挂起的意图,最后4.使用警报管理器触发挂起的意图。 请记住,当收到来自您的报警管理器的内容时,您的Receiver将通过Intent启动您的服务。 您可以使用alarmManager的setRepeating方法来设置重复时间。看看这个堆栈溢出帖子例如如何在Android中使用Alarm Manager启动服务? 。
有关详细信息,请查看此链接(与AlarmManager一起使用定期任务) https://guides.codepath.com/android/Starting-Bground-Services
希望这个帮助。
You will have to use AlarmManager to start your service. To use AlarmManager, you need a pending Intent which you want the alarm to fire. like so:
First, create a Receiver that will will be fired by an Intent,
in your activity, create the intent that fires the Receiver,
create a pending intent for the intent that fires the receiver, finally 4. use alarm manager to fire the pending intent. Remember, your Receiver will fire your service via Intent when it receives content from your alarm manager. You can use the setRepeating method of alarmManager to set the repeat time.Look at this stack overflow post for example How to start Service using Alarm Manager in Android?.
For details, checkout this link(Using with AlarmManager for Periodic Tasks) https://guides.codepath.com/android/Starting-Background-Services
Hope this help.
更多推荐
public,android,@Override,intent,void,电脑培训,计算机培训,IT培训"/> <meta nam
发布评论