我已经两天(约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没有发射
发布评论