gpt4 book ai didi

android - Intent 和 PendingIntent 的区别

转载 作者:IT老高 更新时间:2023-10-28 13:06:59 28 4
gpt4 key购买 nike

我阅读了一些文章。所有人似乎都在做同样的事情,我想知道启动以下服务有什么区别:

Intent intent = new Intent(this, HelloService.class);
startService(intent);

或以下:

Calendar cal = Calendar.getInstance();
Intent intent = new Intent(this, MyService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);

当我通读时,这两个做同样的事情,如果在服务中你返回一个参数 START_STICKY;

最佳答案

Intent

Android Intent 是一个携带 Intent 的对象,即从一个组件到应用程序内部或外部的另一个组件的消息。 Intent 可以在应用程序的三个核心组件( Activity 、服务和广播接收器)中的任何一个之间传递消息。

intent 本身,一个 Intent 对象,是一个被动的数据结构。它包含要执行的操作的抽象描述。

例如:假设您有一个 Activity 需要启动电子邮件客户端并发送电子邮件。为此,您的 Activity 会向 Android Intent Resolver 发送带有操作 ACTION_SEND 的 Intent 以及适当的选择器:

Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this

指定的选择器为用户提供了适当的界面来选择如何发送您的电子邮件数据。

显式 Intent

// Explicit Intent by specifying its class name
Intent i = new Intent(this, TargetActivity.class);
i.putExtra("Key1", "ABC");
i.putExtra("Key2", "123");

// Starts TargetActivity
startActivity(i);

隐含 Intent

// Implicit Intent by specifying a URI
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.example.com"));

// Starts Implicit Activity
startActivity(i);

待定意向

PendingIntent 是您提供给外部应用程序(例如 NotificationManager、AlarmManager、主屏幕 AppWidgetManager 或其他 3rd 方应用程序)的 token ,它允许外部应用程序使用您的应用程序的权限执行一段预定义的代码。

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: almost always, for example, the base Intent you supply should have the component name explicitly set to one of your own components, to ensure it is ultimately sent there and nowhere else.

待定 Intent 示例:http://android-pending-intent.blogspot.in/

来源:Android IntentsAndroid Pending Intents

希望这会有所帮助。

关于android - Intent 和 PendingIntent 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24257247/

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