AlarmManager没有发射

编程入门 行业动态 更新时间:2024-10-11 01:20:30
本文介绍了AlarmManager没有发射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经两天(约12小时)在搜索有关alarmmanager和广播接收器的信息和我设法得到它的工作,但我尝试一个非常奇怪的问题。

I've been for two days (about 12 hours) searching for information about alarmmanager and broadcastreceiver and I manage to get it working but I'm experimenting a really weird issue.

我试图触发一个服务,然后触发一个intentservice。有时,它的工作原理,但它只是停止工作。这真的很奇怪,但解决方案呼吁报警意图广播接收器,然后改变类服务的之一。正如我所说的,这真是不可思议,但这样做之后,它工作一段时间(直到第三或第四编译,然后再次停止工作)。

I'm trying to fire a service which then fires an intentservice. Sometimes, it works, but then it just stops working. It's really weird but the solution is calling the broadcast receiver on the alarm intent and then changing the class to the service's one. As I said, that's really weird, but after doing that, it works for a while (until the third or fourth compile, then it stops working again).

我只是不明白为什么它的工作原理,但有时它没有一些人。

I just cannot understand why it works sometimes but it don't some others.

下面是我的广播接收器类:

Here's my broadcastreceiver class:

公共类BootCompletat扩展广播接收器{

public class BootCompletat extends BroadcastReceiver{

@Override public void onReceive(Context context, Intent intent) { if ((intent.getAction() != null) && (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))){ Log.d("BOOT COMPLETAT!! Inicio el servei","Iniciat"); context.startService(new Intent(context, ServeiConnexio.class)); }else if((intent.getAction() != null) && (intent.getAction().equals("com.example.primerprograma.ALARMA_DISPARADA"))){ Log.d("ALARMA!! Inicio el servei","Iniciat"); context.startService(new Intent(context, ServeiConnexio.class)); } Log.d("Detecto un esdeveniment: ",intent.getAction().toString()); //Toast.makeText(context, "Detecto un esdeveniment", Toast.LENGTH_SHORT).show(); } public static void SetAlarm(Context context){ Log.d("Crido l'alarma","Cridant"); int act = 1; //Toast.makeText(context, "Crido l'alarma", Toast.LENGTH_LONG).show(); AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); // When it stops working, I change on the line below ServeiConnexio.class for BootCompletat.class // Then it starts working (it updates every second) and I change it back to ServeiConnexio.class. After that, it works for a while (???) Intent i = new Intent(context, ServeiConnexio.class); i.setAction("com.example.primerprograma.ALARMA_DISPARADA"); PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * act, pi); // Millisec * Second * Minute }

}

下面是我的服务类:

公共类ServeiConnexio延伸服务{

public class ServeiConnexio extends Service {

// Definim les variables que utilitzarem private ArrayList<String> params_conn = new ArrayList<String>(); private String[] conn_params = null; private ServidorsSQL factory = null; private Funcions funcions = new Funcions(); @Override public IBinder onBind(Intent intent) { return null; } public void onStart(Intent intent, int startid) { //Toast.makeText(this, "Iniciant servei", Toast.LENGTH_LONG).show(); Log.d("SERVEI - He entrat a onStart", "onStart"); factory = new ServidorsSQL(this,"Servidors", null,9); Log.d("Carrego info de tots els servidors","Carrego dades"); funcions.llegeix(params_conn, factory); // Calling alarm Log.d("Executo alarma",""); BootCompletat.SetAlarm(this); // Search for data to create the intent // The loop works perfectly and the intentservice works like charm // So it doesn't have anything to do with the alarm not working for(int m=0;m<params_conn.size();m++){ Log.d("Entro al servei: " + Integer.toString(m),"Executo ConnectaSSH"); // Partim els resultats en un array per poder-los passar a l'intent conn_params = params_conn.get(m).split("\n"); Intent i = new Intent(); i.putExtra("Resultats", conn_params); i.setClass(ServeiConnexio.this,IntentServeiConnexio.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Log.d("Crido activity ConnectaSSH: " + Integer.toString(m),"IntentServeiConnexio"); startService(i); Log.d("Finalitzo activity IntentServeiConnexio: ",Integer.toString(m)); } } @Override public void onDestroy() { Toast.makeText(getApplicationContext(), "Servei destruït",Toast.LENGTH_SHORT).show(); super.onDestroy(); }

}

任何帮助将AP preciated,我越来越绝望

Any help will be appreciated, I'm getting desperate

推荐答案

这code为我工作:

// Start service using AlarmManager try { Calendar cal = Calendar.getInstance(); cal.add(Calendar.SECOND, 10); Intent intent = new Intent(this, YourService.class); PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0); AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 20000 , pintent); //20 second startService(new Intent(getBaseContext(), YourService.class)); Log.d(tag, "Timer Started.."); } catch (Exception e) { Log.d(tag, "timer error: "+e); e.printStackTrace(); }

更多推荐

AlarmManager没有发射

本文发布于:2023-11-27 13:18:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638236.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!