여기 튜토리얼/가이드에 따라 서비스를 사용하는 첫 번째 앱을 코딩하고 있습니다. http://www.vogella.com/articles/AndroidServices/article.html 내 서비스가 작동 중이며 작동하는 BOOT_COMPLETED 용 브로드 캐스트 리시버를 설명하고 몇 분마다 서비스를 실행할 수 있습니다. .재부팅하지 않고 서비스 일정을 시작 하시겠습니까?
내가 가진 문제는 사용자가 휴대 전화를 재부팅 할 때까지 작동하지 않는다는 점입니다. 서비스는 활동으로 시작하지만 장치가 재부팅되지 않으면 활동으로 인해 사망 한 것으로 보입니다.
재부팅없이 처음 실행될 때 활동에서 스케줄러를 시작할 수있는 방법이 있습니까? 다음과 같이 스케줄러에 대한
내 코드는 다음과 같습니다
public class ScheduleReceiver extends BroadcastReceiver {
// Restart service every 30 seconds
private static final long REPEAT_TIME = 1000 * 60; // check every minute.
@Override
public void onReceive(Context context, Intent intent) {
Log.d("schedulereceiver", "starting schedule");
AlarmManager service = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, StartServiceReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
// Start 30 seconds after boot completed
cal.add(Calendar.SECOND, 30);
//
// Fetch every 30 seconds
// InexactRepeating allows Android to optimize the energy consumption
service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), REPEAT_TIME, pending);
// service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
// REPEAT_TIME, pending);
}
}
내 매니페스트 파일은 다음과 같습니다 Service
와
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ProximityActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".ProximityService"
android:enabled="true"
android:icon="@drawable/ic_launcher"
android:label="@string/service_name" >
</service>
<receiver android:name="ScheduleReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name="StartServiceReceiver" >
</receiver>
</application>
</manifest>
나중에 여러 달이 지났지 만 답변을 주셔서 감사합니다. 나는 아직 그 단계에서 많은 것을 배우고 있었고 개념의 일부를 완전히 이해하지 못했습니다. 당신은 매우 친절하게 설명했습니다, 감사합니다! 나는 그 코드로 더 이상 일하지 않기 때문에 "활동으로 죽는다"는 수수께끼에 관한 질문에 대답 할 수 없다. 나는 (하드웨어 제한 때문에) 내가 원하는 방식으로 작동하지 않는 아이디어를 망쳐 놓고 있었기 때문에 그것을 폐기했다. – hyarion