gpt4 book ai didi

android - 如何从 pendingintent 中接收额外信息?

转载 作者:行者123 更新时间:2023-11-30 02:13:58 24 4
gpt4 key购买 nike

我有一个启动通知的服务:

public class Notify extends Service{
@Override
public void onCreate() {

Intent resultIntent = new Intent(this, Main.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);

Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

Notification noti_builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("Title")
.setContentText("Text")
.setSound(uri)
.setContentIntent(pIntent)
.build();

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

noti_builder.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(0, noti_builder);
}

@Override
public IBinder onBind(Intent intent) {
return null;
}
}

服务在其他类中开始:

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancelAll();
Intent intent = new Intent(AddAlarm.this, Notify.class);
intent.putExtra("id", ID);
AlarmManager manager = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(AddAlarm.this, 0, intent, 0);
manager.set(AlarmManager.RTC_WAKEUP, notifyDate.getTimeInMillis(), pendingIntent);

我想获得额外的内部服务。任何建议如何做到这一点?谢谢。

最佳答案

在 Notify 类中,将您的代码从 onCreate 方法移动到具有以下签名的 onStartCommand 方法

onStartCommand (Intent intent, int flags, int startId)

然后您可以从传入方法的 Intent 中访问额外内容。

关于android - 如何从 pendingintent 中接收额外信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29680911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com