我已经完成了一个应用程序,它会在一定时间内发送通知来访问其他活动,但是当我设置时间时,通知只会在我关闭应用程序时及其所需的时间内出现。有人知道如何更改我的代码,使其在选定的时间出现?即使应用程序仍在后台。
我的活动:
package app.alexdickson.com.workout1;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
public class Main2Activity extends AppCompatActivity implements View.OnClickListener{
ImageButton botoFlexio;
ImageButton botoAbdominals;
static final int DIALOG_ID = 0;
int hour_x;
int minute_x;
int hourDefinitivaFlexio;
int minuteDefinitvaFlexio;
int hourDefinitivaAbs;
int minuteDefinitivaAbs;
PendingIntent pendingIntent1;
PendingIntent pendingIntent2;
Context context;
AlarmManager alarmManager1;
AlarmManager alarmManager2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
this.context = this;
botoFlexio = (ImageButton) findViewById(R.id.botoFlexio);
botoAbdominals = (ImageButton) findViewById(R.id.botoAbdominals);
botoFlexio.setOnClickListener(this);
botoAbdominals.setOnClickListener(this);
alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);
}
private void alarmMethod1(){
Intent myIntent1 = new Intent(this, NotifyService1.class);
AlarmManager alarmManager1 = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent1 =PendingIntent.getService(this,0,myIntent1,0);
Calendar calendari1 = Calendar.getInstance();
calendari1.set(Calendar.MINUTE, minute_x);
calendari1.set(Calendar.HOUR,hour_x);
alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP,calendari1.getTimeInMillis(),
1000 * 60 * 60 * 24, pendingIntent1);
}
private void alarmMethod2(){
Intent myIntent2 = new Intent(this, NotifyService2.class);
AlarmManager alarmManager2 = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent2 =PendingIntent.getService(this,0,myIntent2,0);
Calendar calendari2 = Calendar.getInstance();
calendari2.set(Calendar.MINUTE,minute_x);
calendari2.set(Calendar.HOUR, hour_x);
alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP,calendari2.getTimeInMillis(),
1000 * 60 * 60 * 24, pendingIntent2);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.botoFlexio:
botoFlexio.setBackgroundResource(R.drawable.flexioclicat);
showDialog(DIALOG_ID);
hourDefinitivaFlexio = hour_x;
minuteDefinitvaFlexio = minute_x;
alarmMethod1();
break;
case R.id.botoAbdominals:
botoAbdominals.setBackgroundResource(R.drawable.abdominalsclicat);
showDialog(DIALOG_ID);
hourDefinitivaAbs = hour_x;
minuteDefinitivaAbs = minute_x;
alarmMethod2();
break;
}
}
@Override
protected Dialog onCreateDialog(int id) {
if (id == DIALOG_ID)
return new TimePickerDialog(Main2Activity.this, kTimePickerListener, hour_x, minute_x, true);
return null;
}
protected TimePickerDialog.OnTimeSetListener kTimePickerListener =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
hour_x = hourOfDay;
minute_x = minute;
Toast.makeText(Main2Activity.this, hour_x + ": " + minute_x, Toast.LENGTH_LONG).show();
}
};
警报接收器:(有'但它们是相同的,唯一改变的是数字)
package app.alexdickson.com.workout1;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;
public class NotifyService1 extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onCreate(){
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager nNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),Main3Activity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);
Notification mNotify = new Notification.Builder(this)
.setContentTitle("Hora d'entrenar!")
.setContentText("Clica per començar entrenament de pit i braços")
.setSmallIcon(R.drawable.logofinal)
.setContentIntent(pIntent)
.setSound(sound)
.build();
nNM.notify(1,mNotify);
}
}