Android目标:O以上 我想为我的应用程序创建一个通知提醒,它将提醒用户他今天所花的费用。我只是做了一个测试代码,以检查应用程序是否发出通知,但不幸的是没有发出通知。
NotifyService类
public class NotifyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate(){
Toast.makeText(NotifyService.this, "Startttttttttttttttttttttttttt Alarm", Toast.LENGTH_LONG).show();
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("Log Steps!")
.setContentText("Log your steps for today")
.setSmallIcon(R.drawable.notification_icon)
.setContentIntent(pIntent)
.build();
mNM.notify(1, mNotify);
}
}
NewAccountClass.java
public class NewAccount extends AppCompatActivity {
private DataBaseHelper db = new DataBaseHelper(this);
private EditText user_name, pass_word, re_password;
private TextView submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_account2);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
int i = preferences.getInt("numberoflaunches", 1);
if (i < 2){
alarmMethod();
i++;
editor.putInt("numberoflaunches", i);
editor.commit();
}
initialize();
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (pass_word.getText().toString().equals(re_password.getText().toString())) {
DataBaseHelper.writeNewAccountData(db.getWritableDatabase(), user_name.getText().toString(), pass_word.getText().toString());
Toast.makeText(getApplicationContext(), "Account Details Saved !", Toast.LENGTH_LONG).show();
finish();
} else {
Toast.makeText(getApplicationContext(), "Password Doesn't Match", Toast.LENGTH_LONG).show();
//displayNotification(getApplicationContext());
//alarmMethod();
//new NotifyService().onCreate();
}
}
});
}
private void alarmMethod(){
Intent myIntent = new Intent(this , NotifyService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.add(Calendar.DAY_OF_MONTH, 1);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent);
Toast.makeText(NewAccount.this, "Start Alarm", Toast.LENGTH_LONG).show();
}
private void initialize() {
user_name = findViewById(R.id.User_Name_New_Account);
pass_word = findViewById(R.id.Password_New_Account);
re_password = findViewById(R.id.Re_Password_New_Account);
submit = findViewById(R.id.Submit_New_Account);
}
}
当我将时间设置为晚上11:59时,我没有收到任何通知,并且notifyservice中创建函数的Toast.makeText(NotifyService.this,“ Startttttttttttttttttttttttttttt Alarm ...”)并没有得到执行。有什么原因吗?