1

주 활동 (Home.java)의 알람 관리자를 사용하여 서비스 (NotificationService.java)를 시작하려고합니다. AlarmManager의 setInexactRepeating을 실행 한 후 로그를 저장하고 setInexactRepeating을 성공적으로 실행했음을 보여 주지만 서비스는 시작되지 않습니다. 매니페스트에 선언알람 관리자를 사용하여 서비스 시작하지 않음

여기
public void startService(Context context){ 
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    Intent i = new Intent(Home.this, NotificationService.class); 
    int minutes = 1 ; 
    PendingIntent pi = PendingIntent.getService(Home.this, 0, i, 0); 
    am.cancel(pi); 

     am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
      System.currentTimeMillis(), 
      minutes*60*1000, pi); 
     Log.e("Service Started","Halilujia"); 
} 

입니다 : 여기

이 서비스를 시작하는 코드 의견을 바탕으로

 <service 
     android:name=".services.NotificationService" 
     android:enabled="true"> 

</service> 

public class NotificationService extends Service { 

private WakeLock mWakeLock; 
private Context activity = getApplicationContext(); 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 


private void handleIntent(Intent intent) { 
    // obtain the wake lock 
    PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); 
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DebuggingTag"); 
    mWakeLock.acquire(); 

    // check the global background data setting 
    ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); 
    if (!cm.getBackgroundDataSetting()) { 
     stopSelf(); 
     return; 
    } 
    Log.e("Just before request","Just before Request"); 
      // send http request here and create notification 

} 


@Override 
public void onStart(Intent intent, int startId) { 
Log.e("Onstart Called","Onstart has been Called"); 
handleIntent(intent); 
} 


@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
Log.e("Onstart Called","Onstart has been Called"); 
handleIntent(intent); 
return START_NOT_STICKY; 
} 

public void onDestroy() { 
super.onDestroy(); 
mWakeLock.release(); 
} 


} 

감사

+0

'AlarmManager.ELAPSED_REALTIME_WAKEUP'을'AlarmManager.RTC_WAKEUP'으로 변경해보십시오. – Squonk

+0

작동하지 않았습니다. 당신은 그것의 경보 매니저를 생각하거나 서비스 자체 일 수 있 었는가? – Pacemaker

+0

'onStartCommand (...) '메소드의 로그 메시지가 보입니까? – Squonk

답변

0

을,이 (새로운이다 편집 됨) 답변 ...

에 대한 매니페스트 선언210 ... services 불리는 패키지에 포함되는 것으로 선언된다 NotificationService

<service 
    android:name=".services.NotificationService" 
    android:enabled="true"> 
</service> 

...이다. 예 ...

주 패키지는 com.example.MyApp이고 서비스는 com.example.MyApp.services입니다.

/src/services에 서비스의 코드 파일을 배치해도 android:name 특성에 사용되는 패키지 이름/경로가 변경되지 않습니다.

이 문제를 해결하려면 android:name=".NotificationService을 사용하도록 선언을 변경하거나 com.example.MyApp.services 패키지를 별도로 만들고 서비스 코드 파일을 넣으십시오.

+0

나는 그것을 시도했지만 새로운 것은 일어나지 않았다. 어쨌든 그것은 명백한 선언 일 수 있습니까? NotificationService.java를 src/services /에 넣습니다. 여기서 폴더 서비스를 만들었습니다. – Pacemaker

+0

@Pacemaker :'src/services /'는 패키지가 아닌 폴더 일뿐입니다. 그렇다면 네임드 선언은 틀린 것입니다. Name 속성을'android : name = ". NotificationService"'로 변경하십시오. 서비스가 메인 패키지에 있다면. 서비스를 다른 코드 파일과 분리하려면 새/src 폴더가 아닌 새 패키지 (예 : com.mycompany.myapp.services)를 사용하십시오. 그러면 당신은'android : name = ". services.NotificationService"' – Squonk

+0

을 사용할 수있게 될 것입니다. 좋아요. 시도해보고 알려 드리겠습니다. – Pacemaker

관련 문제