关于android API中的android.app.PendingIntent类的介绍

本文详细介绍了Android中PendingIntent的概念及其使用方式。通过PendingIntent,开发者可以授权其他应用执行特定操作,如同该操作由自身应用发出一样。文章强调了构建PendingIntent时的安全注意事项,以及PendingIntent在进程间的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android.app.PendingIntent

By giving a PendingIntent to another application, you are granting it the right to

perform the operation you have specified as if the other application was yourself (with the same permissions and identity). As such, you should be careful about how you

build the PendingIntent: often, for example, the base Intent you supply will have the component name explicitly set to one of your own components, to ensure it is ultimately sent there and nowhere else.

A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it.

Summary

 

翻译如下:

通过给另外一个应用程序一个PendingIntent,你授予了它你指定的正确的操作过程,就像这个应用程序是你自己的一样(拥有相同的权限和身份认证)。因此,你应该认真地考虑如何构建这个PendingIntent对象:通常状况下,例如,这个你提供的基本的Intent应该拥有的这些组件名称明确地设置给你自己的组件之一,通过这样来确保它最终被发送并且没有其他地方存在。

 

一个PendingIntent对象仅仅是一个参考,它被系统维护,描述为通常被检索的原始数据。如果之后又创建一个应用程序,再次检索同样的PendingIntent(拥有同样的操作,同样的Intent动作、数据、类别、组件及相同的标志),它将接收到一个PendingIntent代表同样的指令,如果它仍有效,并且可以因此调用cancel()方法去清除掉它 。

 

 

 

@RequiresApi(api = Build.VERSION_CODES.O) @Override public void onCreate() { super.onCreate(); Log.d(“MyService”, “onCreate”); Intent intent = new Intent(this,MainActivity.class); PendingIntent pi = PendingIntent.getActivity(this,0,intent, PendingIntent.FLAG_IMMUTABLE); String channelId = "my_service_channel"; String channelName = "My Background Service"; int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel(channelId, channelName, importance); channel.setDescription("Foreground Service Channel"); NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); manager.createNotificationChannel(channel); Notification notification = new NotificationCompat.Builder(this,channelId) .setContentTitle("this is a title") .setContentText("this is a text") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) .setContentIntent(pi) .build(); //manager.notify(1,notification); startForeground(1,notification); }上述代码报错:FATAL EXCEPTION: main Process: com.example.cameratest, PID: 14112 java.lang.RuntimeException: Unable to create service com.example.cameratest.MyService: android.app.MissingForegroundServiceTypeException: Starting FGS without a type callerApp=ProcessRecord{b959cbf 14112:com.example.cameratest/u0a194} targetSDK=36 at android.app.ActivityThread.handleCreateService(ActivityThread.java:4705) at android.app.ActivityThread.-$$Nest$ mhandleCreateService(Unknown Source:0) at android.app.ActivityThread$ H.handleMessage(ActivityThread.java:2305) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:205) at android.os.Looper.loop(Looper.java:294) at android.app.ActivityThread.main(ActivityThread.java:8225) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$ MethodAndArgsCaller.run(RuntimeInit.java:573) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1058) Caused by: android.app.MissingForegroundServiceTypeException: Starting FGS without a type callerApp=ProcessRecord{b959cbf 14112:com.example.cameratest/u0a194} targetSDK=36 at android.app.MissingForegroundServiceTypeException$ 1.createFromParcel(MissingForegroundServiceTypeException.java:53) at android.app.MissingForegroundServiceTypeException$ 1.createFromParcel(MissingForegroundServiceTypeException.java:49) at android.os.Parcel.readParcelableInternal(Parcel.java:4870) at android.os.Parcel.readParcelable(Parcel.java:4852) at android.os.Parcel.createExceptionOrNull(Parcel.java:3052) at android.os.Parcel.createException(Parcel.java:3041) at android.os.Parcel.readException(Parcel.java:3024) at android.os.Parcel.readException(Parcel.java:2966) at android.app.IActivityManager$ Stub$ Proxy.setServiceForeground(IActivityManager.java:6795) at android.app.Service.startForeground(Service.java:775) at com.example.cameratest.MyService.onCreate(MyService.java:79) at android.app.ActivityThread.handleCreateService(ActivityThread.java:4692) at android.app.ActivityThread.-$$Nest$ mhandleCreateService(Unknown Source:0) at android.app.ActivityThread$ H.handleMessage(ActivityThread.java:2305) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:205) at android.os.Looper.loop(Looper.java:294) at android.app.ActivityThread.main(ActivityThread.java:8225) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$ MethodAndArgsCaller.run(RuntimeInit.java:573) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1058)
最新发布
07-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值