我的程序有问题。我设置:.setContentText(powiadomienie)。它工作正常,但只有当应用程序打开时。当我关闭应用程序时,它显示空通知。你能告诉我我做错了什么吗?谢谢,祝你有愉快的一天!
nazwaLeku = (EditText) findViewById(R.id.editText);
dodaj.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View view) {
NotificationReceiver.xd(nazwaLeku.getText().toString());
if (nazwaLeku.getText().length() > 0 && ileDni.getText().length() > 0) {
Calendar cal = Calendar.getInstance();
cal.set(year, month-1, day, hour, minutes, 0);
Intent intx = new Intent(getApplicationContext(), NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intx, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
});
public class NotificationReceiver extends BroadcastReceiver{
static String powiadomienie;
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent repeating_intent = new Intent(context, RepeatingActivity.class);
repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = Uri.parse("android.resource://com.example.kuba.dsadsax/" + R.raw.audio);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.logo)
.setContentTitle("Weź pigułke")
.setContentText(powiadomienie)
.setSound(alarmSound)
.setOnlyAlertOnce(true)
.setAutoCancel(true);
notificationManager.notify(100, builder.build());
}
public static void xd(String lek) {
powiadomienie = lek;
}
}
答案 0 :(得分:1)
好的,你叫静态功能设定值!!!!
这是你的问题
如果你想传递你应该在意图中传递的参数
喜欢这个
dodaj.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View view) {
if (nazwaLeku.getText().length() > 0 && ileDni.getText().length() > 0) {
Calendar cal = Calendar.getInstance();
cal.set(year, month-1, day, hour, minutes, 0);
Intent intx = new Intent(getApplicationContext(), NotificationReceiver.class);
intx.PutExtra("Value",YOUR_VALUE_HERE_TO_PASS)
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intx, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
});
然后在你的BroadCast中你可以从Intent
获得它public class NotificationReceiver extends BroadcastReceiver{
static String powiadomienie;
@Override
public void onReceive(Context context, Intent intent) {
powiadomienie = intent.getStringExtra("Value");
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent repeating_intent = new Intent(context, RepeatingActivity.class);
repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = Uri.parse("android.resource://com.example.kuba.dsadsax/" + R.raw.audio);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.logo)
.setContentTitle("Weź pigułke")
.setContentText(powiadomienie)
.setSound(alarmSound)
.setOnlyAlertOnce(true)
.setAutoCancel(true);
notificationManager.notify(100, builder.build());
}
}