Android studio Can';当通过“PendingEvent”传递值时,似乎不会有意传递值`
我试图创建一个警报,在未来的特定时间弹出一个通知。我有下面的代码,大部分代码似乎都在工作,但由于某种原因,我似乎无法在Intent对象中传递任何值。我觉得我做的每件事都是对的。我甚至看过一些在线教程,据我所知,我做的和他们做的完全一样,但他们的工作和我的不一样 Receiver.javaAndroid studio Can';当通过“PendingEvent”传递值时,似乎不会有意传递值`,android-studio,android-intent,android-notifications,android-pendingintent,android-alarms,Android Studio,Android Intent,Android Notifications,Android Pendingintent,Android Alarms,我试图创建一个警报,在未来的特定时间弹出一个通知。我有下面的代码,大部分代码似乎都在工作,但由于某种原因,我似乎无法在Intent对象中传递任何值。我觉得我做的每件事都是对的。我甚至看过一些在线教程,据我所知,我做的和他们做的完全一样,但他们的工作和我的不一样 Receiver.java package com.example.mytermtracker.notifications; import android.app.AlarmManager; import android.app.Pen
package com.example.mytermtracker.notifications;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.example.mytermtracker.application.Date;
public class Receiver extends BroadcastReceiver {
private static final String TAG = "app: Receiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive: now: " + Date.now().toSQL());
Channel channel = intent.getParcelableExtra("channel");
if (channel == null)
throw new NullPointerException("Could not find the channel."); //<== the line that the breaks.
String title = intent.getStringExtra("title");
if (title == null) title = channel.NAME;
String message = intent.getStringExtra("message");
if (message == null) message = "";
int callerID = intent.getIntExtra("caller_id", 0);
channel.show(context, title, message, callerID);
}
public static void setAlarm(Context context, Channel channel, String title, String message, Date trigger, int callerID) {
Intent intent = new Intent(context, Receiver.class);
intent.putExtra("channel", channel);
intent.putExtra("title", title);
intent.putExtra("message", message);
intent.putExtra("caller_id", callerID);
Log.d(TAG, "setAlarm : now: " + Date.now().toSQL());
Log.d(TAG, "setAlarm : trigger: " + trigger.toSQL());
PendingIntent sender = PendingIntent
.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (alarmManager != null) {
alarmManager.set(AlarmManager.RTC_WAKEUP, trigger.getTimeInMillis(), sender);
}
}
}
package com.example.mytermtracker.notifications;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import com.example.mytermtracker.R;
import com.example.mytermtracker.application.Date;
import static com.example.mytermtracker.application.App.MANAGER;
public class Channel implements Parcelable {
private static final String TAG = "app: Channel";
public final String ID;
public final String NAME;
public final String DESCRIPTION;
public final int IMPORTANCE;
private Channel(NotificationManager manager, String id, String name, String description, int importance) {
this.ID = id;
this.NAME = name;
this.DESCRIPTION = description;
this.IMPORTANCE = importance;
if (manager != null) {
NotificationChannel termStartChannel = new NotificationChannel(ID, NAME, IMPORTANCE);
termStartChannel.setDescription(DESCRIPTION);
manager.createNotificationChannel(termStartChannel);
}
}
protected Channel(Parcel in) {
ID = in.readString();
NAME = in.readString();
DESCRIPTION = in.readString();
IMPORTANCE = in.readInt();
}
public static final Creator<Channel> CREATOR = new Creator<Channel>() {
@Override
public Channel createFromParcel(Parcel in) {
return new Channel(in);
}
@Override
public Channel[] newArray(int size) {
return new Channel[size];
}
};
public static Channel create(NotificationManager manager, String id, String name, String description) {
return new Channel(manager, id, name, description, NotificationManager.IMPORTANCE_DEFAULT);
}
public static Channel create(NotificationManager manager, String id, String name, String description, int importance) {
return new Channel(manager, id, name, description, importance);
}
public void show(Context context, String title, String message, int callerID) {
Log.d(TAG, "show: now: " + Date.now().toSQL());
Notification notification = new NotificationCompat.Builder(context, ID)
.setSmallIcon(R.drawable.ic_notification_icon)
.setContentTitle(title)
.setContentText(message)
.build();
MANAGER.notify(callerID, notification);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(ID);
dest.writeString(NAME);
dest.writeString(DESCRIPTION);
dest.writeInt(IMPORTANCE);
}
}
package com.example.mytermtracker.notifications;
导入android.app.AlarmManager;
导入android.app.pendingent;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.util.Log;
导入com.example.mytermtracker.application.Date;
公共类接收器扩展了广播接收器{
私有静态最终字符串TAG=“app:Receiver”;
@凌驾
公共void onReceive(上下文、意图){
Log.d(标记“onReceive:now:+Date.now().toSQL());
通道通道=intent.getParcelableExtra(“通道”);
如果(通道==null)
抛出新的NullPointerException(“找不到频道”);//好的。所以我找到了它。显然,FLAG\u UPDATE\u CURRENT
对输入做了一些事情,把它搞砸了。我只是把它改成了FLAG\u IMMUTABLE
,似乎解决了这个问题