2012-03-03 2 views
2


IntentService를 AlarmManager에서 시작하려고하는데 서비스가 시작되지 않았습니다.AlarmManager에서 시작한 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(); 
     } 
    } 

} 
} 
: 여기

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입니다

FileMethods는 파일 읽기 및 쓰기를 처리하는 클래스입니다 (AlarmManager가 1 일마다 실행되도록)

고맙습니다!

답변

2

set()이 (가) 사용 중이므로 이제는 과거 시간을 지정했을 확률이 40 %입니다.

또한 기기가 잠자기 상태 인 경우 서비스가 시작되기 전에 잠자기 상태로 되돌아 갈 수 있습니다. BroadcastReceiverWakeLock을 포함하여 _WAKEUP 알람을 성공적으로 사용하는 데 매우 특정한 패턴이 있습니다. 내 WakefulIntentService은이 작업 중 일부를 처리하려고 시도합니다.

또한 onStart()은 일부 사용되지 않으며 IntentService에 구현해서는 안됩니다.

0

super.onCreate()을 쓰지 않고 #onCreate()을 대체합니다. 그러지 마.

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

IntentService 기본 구현은 실제로 IntentService#onHandleIntent(Intent) 호출 요구 (예 실행기 & 등등 초기화 등) 메소드에 필요한 설정을 많이한다.

관련 문제