2016-04-08 8 views
0

私は現在、チュートリアルの後に毎日同じ時刻にオフになる繰り返しアラームを作成しようとしていますが、うまく動作しないようです。それは消えますが、設定された時間の代わりにアプリケーションが起動すると、私は理由が分かりません。私はまた、サービスがマニフェストで宣言されたことを確認しました。私が間違っているかどうか、これが正しいのか?Androidの通知が設定時間ではなく起動時に起動する

メインacitivty

public class MainActivity extends AppCompatActivity { 
private PendingIntent pendingIntent; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
    SharedPreferences.Editor editor = preferences.edit(); 
    int i = preferences.getInt("numberofLaunces", 1); 

    if (i < 2) { 
     alarmMethod(); 
     i++; 
     editor.putInt("numberoflaunches", i); 
     editor.commit(); 
    } 

    if (savedInstanceState == null) { 
     return; 
    } 

} 



private void alarmMethod() { 
    Intent myIntent = new Intent(this, NotifyService.class); 
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    pendingIntent = PendingIntent.getService(this, 0, myIntent, 0); 

    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.SECOND, 0); 
    calendar.set(Calendar.MINUTE, 32); 
    calendar.set(Calendar.HOUR, 0); 
    calendar.set(Calendar.AM_PM, Calendar.AM); 
    calendar.set(Calendar.DAY_OF_MONTH, 0); 

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent); 

    Toast.makeText(MainActivity.this, "start Alarm", Toast.LENGTH_LONG).show(); 
} 
} 

開発者[サイト](http://developer.android.com/reference/android/app/AlarmManager.html#set(int、長い、android.app.PendingIntent))から

public class NotifyService extends Service { 
@Override 
public IBinder onBind(Intent Intent) { 
    return null; 
} 

@Override 
public void onCreate(){ 
    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
    Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class); 
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1,0); 

    Notification mNotify = new Notification.Builder(this) 
      .setContentTitle("placeholder tital") 
      .setContentText("Placeholder text") 
      .setSmallIcon(R.drawable.ic_cat) 
      .setContentIntent(pIntent) 
      .setSound(sound) 
      .addAction(0,"hello", pIntent) 
      .build(); 

    mNM.notify(1, mNotify); 



} 
} 

答えて

1

Notifyservice

アラームが意図放送ですそれは、あなたがregisterReceiver(BroadcastReceiver、IntentFilter)で登録した放送受信機に行くか、またはn AndroidManifest.xmlファイル。

サービスの代わりにブロードキャスト保留インテントを定義する必要があります。この本

Intent myIntent = new Intent(this, MyAlarmBroadcastReceiver.class); 
pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0); 

Intent myIntent = new Intent(this, NotifyService.class); 
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0); 

そしてMyAlarmBroadcastReceiverを実装 変更)(onReceive内部BroadcastReceiverクラスと火災通知を拡張します。

+0

は、それが移動したり、同じ場所に滞在しなければならないとmyalarmboardcastreciverは同じクラス内にいますか? –

+0

でも問題ありません。あなたはいつでもアラームを設定することができます。 oncreate、onclickなど – uguboz

0

問題は非常に簡単です:アラームが発生する時間(setRepeating()の2番目のパラメータ)が過去にある場合は、直ちに起動します。指定した時刻は、0日(?)の日の夜0:32です。 これは過去のものです(毎月32分を除いて、月の0日が何であるかはわかりませんが)。

24時間ごとにこのような午後12時32分午前使用何かの時にアラームを発射する:aboutsはカレンダーのコードが配置されることになる

Calendar calendar = Calendar.getInstance(); 
calendar.set(Calendar.HOUR, 0); 
calendar.set(Calendar.MINUTE, 32); 
calendar.set(Calendar.SECOND, 0); 
calendar.set(Calendar.AM_PM, Calendar.AM); 
calendar.add(Calendar.DAY_OF_MONTH, new Date().after(calendar.getTime()) ? 1 : 0); //if the current time is after 0:32 am, one day is added 

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent); 
+0

@GeorgeHallsこれを試しても問題は解決しましたか? :) –

関連する問題