2012-03-03 130 views
2


我想通過AlarmManager啓動IntentService,但服務未啓動。 (顯然,我的服務是在清單中定義...)
這裏的代碼一點點:由AlarmManager啓動的IntentService不起作用

啓動報警服務:

Intent myIntent = new Intent(Main.this, TestsNotification.class); 
    PendingIntent pendingIntent = PendingIntent.getService(Main.this, 0, myIntent, 0); 
      AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
      Calendar calendar = Calendar.getInstance(); 
      calendar.setTimeInMillis(System.currentTimeMillis()); 
      calendar.set(Calendar.HOUR_OF_DAY, 15); 
      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 

這裏是我的IntentService:

public class TestsNotification extends IntentService { 
    public TestsNotification() { 
     super("myApp"); 
    } 
    private SharedPreferences settings; 
    private final String PREFERENCE_SETTINGS_FILENAME = "Settings"; 
    private int number=0; 
    Tests tests; 
@Override 
public void onCreate() { 
// TODO Auto-generated method stub 
    settings = getSharedPreferences(PREFERENCE_SETTINGS_FILENAME, MODE_PRIVATE); 
    tests=new Tests(); 
} 

@Override 
public IBinder onBind(Intent intent) { 
// TODO Auto-generated method stub 
return null; 
} 

@Override 
public void onDestroy() { 
// TODO Auto-generated method stub 
super.onDestroy(); 
} 

@Override 
public void onStart(Intent intent, int startId) { 
// TODO Auto-generated method stub 
super.onStart(intent, startId); 
} 

@Override 
public boolean onUnbind(Intent intent) { 
// TODO Auto-generated method stub 
return super.onUnbind(intent); 
} 
public void Notify(String title) 
{ 
    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    Intent intent= new Intent (this,Splash.class); 
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0); 
    String body = " בליך"; 
    //String title = "יש מחר מבחן!"; 
    Notification n =new Notification(R.drawable.test, body, System.currentTimeMillis()); 
    n.flags |=Notification.FLAG_AUTO_CANCEL; 
    n.setLatestEventInfo(getApplicationContext(), title, body, pi); 
    n.defaults = Notification.DEFAULT_ALL; 
    number++; 
    n.number=number; 
    try { 
    nm.notify(0,n); 
    FileMethods FM = new FileMethods(this); 
    Date current = new Date(); 
    FM.Write("LOG", "I Dont Care!",FM.Read("LOG", this, "") + current.getDay()+"/"+current.getMonth()+"/"+ current.getYear()+" "+current.getHours()+":"+current.getMinutes() + "Notified" + title+ "\n"); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
@Override 
protected void onHandleIntent(Intent intent) { 
    // TODO Auto-generated method stub 
    String Attribute = "Class"; 
    String info = settings.getString(Attribute, "none"); 
    if(!info.equals("none")) { 
     String classLetter = info.substring(0, info.lastIndexOf(" ")); 
     String classNum1 = info.substring(info.lastIndexOf(" ")+1); 
     int classNum = Integer.parseInt(classNum1); 
     try { 
      Tests nextTest = this.tests.GetTests(classLetter, classNum)[0]; 
      Date current = new Date(); 
      Date testDate = new GregorianCalendar(nextTest.getDate().getYear(), nextTest.getDate().getMonth(), Integer.parseInt(nextTest.getDate().getDay())).getTime(); 
      long difference = testDate.getTime()-current.getTime(); 
      if (difference <=86400000) { 
       Notify("יש מחר מבחן!"); 
      }//SHOULDNT BE HARDCODED!!! 
      else { 
       Notify("אין מחר מבחן!!"); 
      } 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

} 
} 

FileMethods只是一個類來處理文件讀取和寫入(以確保AlarmManager每隔1天運行一次)

謝謝!

回答

2

由於您使用的是set(),因此現在有40%的機會指定了過去的時間。

此外,如果設備當時睡着了,它可能會在服務啓動之前回睡。對於成功使用_WAKEUP報警有一個非常具體的模式,涉及BroadcastReceiverWakeLock。我的WakefulIntentService試圖爲你處理這些工作。

此外,onStart()已被棄用相當一些,不應該在IntentService實施。

0

您在覆蓋IntentService 沒有調用super.onCreate()。不要這樣做。

@Override 
public void onCreate() { 
    super.onCreate(); 
    settings = getSharedPreferences(PREFERENCE_SETTINGS_FILENAME, MODE_PRIVATE); 
    tests=new Tests(); 
} 

IntentService的基本實現不會給實際調用IntentService#onHandleIntent(Intent)需要大量的在該方法中必要的設置(如初始化執行人&諸如此類的東西)。