0
我已經完成了一個應用程序,它在一定時間內發送了一個通知來訪問其他活動,但是當我設置時間時,通知纔會出現,只有當我關閉該應用程序並且符合所需時間時。有人知道如何更改我的代碼,使其顯示在選定的時間?即使該應用仍處於後臺。爲什麼在我的android項目中沒有正確顯示通知?
我的活動:
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();
}
};
報警接收機(有2,但它們是相同的,唯一改變的事情是數字)
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);
}
}
沒有足夠的代碼。你的報警接收器在哪裏?你應該有一個警報接收器。我只看到活動。 – shadox
完成:) @shadox –
選中此鏈接(http://androhub.com/android-notification-bar/)。 –