2011-11-30 17 views
27

私のアプリケーションでは、アラームマネージャを使用してサービスを開始しようとしています。私がボタンをクリックしているとき、私が与えている特定の時間にサービスを開始する必要があります。 アラームマネージャのための私のコードは以下のとおりである:Androidでアラームマネージャを使用してサービスを開始するには?

public void onClick(View view) 
{ 
    if(view == m_btnActivate) 
    { 
     Calendar cur_cal = Calendar.getInstance(); 
     cur_cal.setTimeInMillis(System.currentTimeMillis()); 
     cur_cal.add(Calendar.SECOND, 50); 
     Log.d("Testing", "Calender Set time:"+cur_cal.getTime()); 
     Intent intent = new Intent(DashboardScreen.this, ServiceClass.class); 
     Log.d("Testing", "Intent created"); 
     PendingIntent pi = PendingIntent.getService(DashboardScreen.this, 0, intent, 0); 
     AlarmManager alarm_manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
     alarm_manager.set(AlarmManager.RTC, cur_cal.getTimeInMillis(), pi); 
     Log.d("Testing", "alarm manager set"); 
     Toast.makeText(this, "ServiceClass.onCreate()", Toast.LENGTH_LONG).show(); 
    } 
} 

そして、ベローズは私のサービスクラスである:

public class ServiceClass extends Service{ 

    @Override 
    public void onCreate() { 
     // TODO Auto-generated method stub 
     super.onCreate(); 
     Log.d("Testing", "Service got created"); 
     Toast.makeText(this, "ServiceClass.onCreate()", Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // 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); 
     Toast.makeText(this, "ServiceClass.onStart()", Toast.LENGTH_LONG).show(); 
     Log.d("Testing", "Service got started"); 
    } 

} 

サービスは、サービスクラスのログメッセージを印刷する必要があります開始されます。しかし、それは表示されていません。誰も助けることができますか?

+0

名前は記載しましたが、Android:enabled = "true"ではありません。 –

+1

alarmManager.cancel(pintent)を使用してください。アラームマネージャをキャンセルします。 – user370305

+1

デフォルトでは 'Android:enabled'は' true'です – Tautvydas

答えて

72

EDIT ...これは私が使用しているものである、現在の時刻から30秒後にサービスを開始するため、

Intent intent = new Intent(DashboardScreen.this, ServiceClass.class); 
PendingIntent pintent = PendingIntent.getService(DashboardScreen.this, 0, intent, 0); 
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent); 

はそれを試してみて、そして私が起こるか知ってみましょう:

あなたのmanifest.xmlファイルで

+1

はい、それは働いています...おかず..しかし、今私は特定の時間にサービスを停止できますか? –

+1

あなたは上記の質問のために私を助けることができますか?特定の時間にどのようにアラームマネージャを停止できますか? –

+0

alarmManager.cancel(pintent)を使用します。アラームマネージャを停止します。 – user370305

12

私はこれが古い質問だと知っていますが、あなたのサービスを存続させる方法はここにあります。

Intent ishintent = new Intent(this, HeartBeat.class); 
    PendingIntent pintent = PendingIntent.getService(this, 0, ishintent, 0); 
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
    alarm.cancel(pintent); 
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),5000, pintent); 

サービスが強制終了され、5秒ごとに再起動されます。

+0

値は、アンドロイド5の60000ミリ秒まで強制され、依存しないでください...あなたは1分を待たなければなりません。とにかくありがとう – thanhbinh84

関連する問題