2012-01-25 3 views
2

毎日午後7時に新しいサービスを開始しようとしていますが、起動時に通知されますが、誰もがここでthankx私を助けることができるここでは、コードalarmmanagerを使用して毎日サービスを実行するにはどうすればいいですか


は、ここで私はDaysCounterActivityクラスで書いたコード

Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.HOUR_OF_DAY, 19); 
    calendar.set(Calendar.MINUTE, 05); 
    calendar.set(Calendar.SECOND, 00); 


    Intent intent = new Intent(this, MyReciever.class); 
    PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0); 


    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pintent); 

とされているMyRevieverクラスonRevcieve方法である

public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 

    Intent serviceIntent = new Intent(); 
    serviceIntent.setAction("com.saaram.MyService"); 
    context.startService(serviceIntent); 
} 
+0

はまた、関連のAndroidマニフェストレシーバのコードについての私を助けて – Saaram

答えて

3

あなたの問題pintentはサービスを実行しようとしていますが、MyReceiverはブロードキャスト受信者です。 MyReceiverをサービスに変更した場合は正常に機能します。マニフェストで

public class MyReceiver extends Service { 


    public int onStartCommand(Intent intent, int flags, int startId) { 
     //Anything you put here is run at the time you set the alarm to 
     } 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
     } 

    } 

あなただけそうのようにそれを宣言します:

<service android:name=".MyReceiver"></service> 
関連する問題