2016-04-12 12 views
0

私は基本的に、スケジュールされた時刻に毎日の通知を表示しようとしています(たとえば、毎日午前7時30分)。しかし、実装したコードは通知を一切表示しません。Android:スケジュール通知が表示されません。

私は時間を設定し活動:

//This method is called by a button onClick method 
private void SaveData() { 
     //I get the hour, minute and the AM/PM from 3 edittexts 
     String hours = hoursBox.getText().toString(); 
     String minutes = minutesBox.getText().toString(); 
     String ampm = ampmBox.getSelectedItem().toString(); 

     if (hours.length() != 0 && minutes.length() != 0 && ampm.length() != 0) { 
      Calendar calendar = Calendar.getInstance(); 
      calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hours)); 
      calendar.set(Calendar.MINUTE, Integer.parseInt(minutes)); 
      calendar.set(Calendar.SECOND, 0); 
      //calendar.set(Calendar.AM_PM, Calendar.AM); 

      Intent intent=new Intent(this, ReminderService.class); 
      AlarmManager manager=(AlarmManager)getSystemService(Activity.ALARM_SERVICE); 
      PendingIntent pendingIntent=PendingIntent.getService(this, 0,intent, 0); 
      manager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),24*60*60*1000,pendingIntent); 
     } 
} 

ReminderService.java

public class ReminderService extends Service { 

    @Override 
    public void onCreate() 
    { 
     Intent resultIntent=new Intent(this, Dashboard.class); 
     PendingIntent pIntent=PendingIntent.getActivity(this,0,resultIntent,0); 


     Notification noti_builder= new Notification.Builder(this) 
       .setContentTitle("Hello from the other side!") 
       .setContentIntent(pIntent) 
       .setSmallIcon(R.drawable.helloicon) 
       .build(); 
     NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

     noti_builder.flags |=Notification.FLAG_AUTO_CANCEL; 

     notificationManager.notify(1,noti_builder); 

    } 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 
} 

私はここで間違って何をしているのですか?マニフェストにも何かを追加する必要がありますか?これらは私が現在行っている唯一の2つの実装です。前もって感謝します!

+0

"実装したコードでは、スケジュールされた時刻に通知が表示されません。 - それはまったく表示されないという意味ですか、希望しない時だけですか? –

+0

@MikeMはまったく表示されません。 – Dinuka

+0

マニフェストに 'Service'がリストされていますか? –

答えて

1

あなたのアプリで使用されているServiceはマニフェストにリストされている必要があります。また、Serviceはアプリのみで使用されるため、exported属性をfalseに設定することをおすすめします。 24時間クロックにCalendarセット時間にCalendar.HOUR_OF_DAY成分、また

<service android:name=".ReminderService" 
    android:exported="false" /> 

:例えば、マニフェストに<application>タグ内部

。 12時間制を使用する場合は、Calendar.HOURを使用し、Calendar.AM_PMコンポーネントも設定します。

最後に、電話がアクティブでなくてもNotificationが発行されるように、何とかWakeLockを取得したいと思うでしょう。 WakeLockを手渡す代わりに、いくつかのオプションがあります。 v4サポートライブラリのWakefulBroadcastReceiver classを使用してServiceを起動することができます。これを実行すると、受信者にロックを解除するよう通知することができます。また、Receiverコンポーネントを追加しない場合は、ServiceCommonsWareWakefulIntentService classに置き換えることもできます。

あなたがWakefulBroadcastReceiverを使用することを選択した場合、あなたはまだIntentServiceがその作業が行われたときに自分自身を停止するの面倒を見るように、それがどんな長時間実行操作を行うことはないだろう場合、あなたのServiceIntentServiceに変更することを検討することがあります。

関連する問題