私は現在、チュートリアルの後に毎日同じ時刻にオフになる繰り返しアラームを作成しようとしていますが、うまく動作しないようです。それは消えますが、設定された時間の代わりにアプリケーションが起動すると、私は理由が分かりません。私はまた、サービスがマニフェストで宣言されたことを確認しました。私が間違っているかどうか、これが正しいのか?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);
}
}
は、それが移動したり、同じ場所に滞在しなければならないとmyalarmboardcastreciverは同じクラス内にいますか? –
でも問題ありません。あなたはいつでもアラームを設定することができます。 oncreate、onclickなど – uguboz