2014-05-21 31 views
0

我已經兩天(大約12小時)搜索有關alarmmanager和broadcastreceiver的信息,並設法讓它工作,但我正在試驗一個非常奇怪的問題。AlarmManager未開火

我試圖啓動一個服務,然後觸發intentservice。有時候,它可以工作,但它只是停止工作。這真的很奇怪,但解決方案是根據鬧鐘意圖調用廣播接收器,然後將類更改爲服務的類。正如我所說,這真的很奇怪,但在這之後,它會有一段時間(直到第三次或第四次編譯,然後再次停止工作)。

我只是不明白爲什麼它有時會起作用,但它並不是其他人。

這裏是我的廣播接收器類:

公共類BootCompletat擴展廣播接收器{

@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延伸服務{

// 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(); 
} 

}

任何幫助將不勝感激,我越來越絕望

回答

0

此代碼爲我工作:

// 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(); 
} 
+0

完美的,我不知道哪裏是你的代碼之間的差異,我的,但你的工作就像魅力。 非常感謝。 – user3424020