0

AlarmManagerを使用して定期的に呼び出すサービスを1つ作成しました。私は同じコードを書いていますが、それは呼び出していません。私がAlarmManagerを通してBroadcasrRecieverを呼び出すと、正常に動作しています。以下はAlarmManagerを使用したコールサービス

私のサービスクラスは、

public class LocationUpdateService1 extends Service { 
    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     Toast.makeText(getApplicationContext(), "I created", Toast.LENGTH_LONG); 
     return null; 
    } 
} 

マイmanifiestファイル、

<service 
      android:name=".Services.LocationUpdateService1"> 
     </service> 

が、私は私のサービスを呼び出すためにBroadcastReceiverクラスを作成しなければならないのである

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_home); 

     Intent alarmIntent = new Intent(this, LocationUpdateService1.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0); 
     AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
     manager.setRepeating(AlarmManager.RTC_WAKEUP, 20000, 11000, pendingIntent); 

    } 

、私のコードです? 私のコードで何が間違っていますか?それが定期的に呼び出されない理由は何ですか?

+1

あなたは' PendingIntent.getService() 'を使用して' PendingIntent'を取得する必要があります。また、 'onBind()'は何かが 'Service'をバインドしない限り起動しません。おそらく 'onCreate()'や 'onStartCommand()'が必要でしょう。 –

+0

[AlarmManagerでPendingIntent.getService()またはgetBroadcastを使用する必要がありますか?](http://stackoverflow.com/questions/7308298/should-i-use-pendingintent-getservice-or-getbroadcast-with-alarmmanager) –

+0

getBroadcast()をgetService()に変更しましたが、まだ動作しません –

答えて

1

を使用しないでください。PendingIntent.getBroadcastを使用しても機能しません。 getService()を使用すると、代わりに、次のように: - ない `getBroadcast()` - あなたはそれが `Service`を開始したい場合は

PendingIntent pendingIntent = PendingIntent.getService(this, 0, alarmIntent, 0); 
+0

パラメータを理解できません。可能であれば、私のコードを正しいものに変換してください。 –

+0

@KevalPatelは単純ですが、ちょうど答えを更新:) – alway5dotcom

関連する問題