- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我阅读了一些文章。所有人似乎都在做同样的事情,我想知道启动以下服务有什么区别:
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 Intents和 Android Pending Intents
希望这会有所帮助。
关于android - Intent 和 PendingIntent 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24257247/
我想了解 PendingIntent.FLAG_NO_CREATE 的行为。我想知道它什么时候返回 null 什么时候不返回。这是事件发生的顺序: alarmIntent = new Intent(c
我正在尝试请求网络,因此我构建了 NetworkRequest 并构建了 PendingIntent, ConnectivityManager conn_manager = (Connectivit
我一直在使用警报管理器。一旦我设置了一个 PendingIntent 来触发警报,我想取消它。 How do I achieve this? 帮助将不胜感激。 最佳答案 下面这几行代码肯定可以帮助您删
我正在尝试在 Android 中创建一个 PendingIntent。这是代码 mNotificationIntent = new Intent(getApplicationContent(), My
我有三个 Activity (Home、Search、Destination),我可以用它们来描述我的用户体验流程。 Home Activity 是我的启动器 Activity ,然后是搜索 Acti
我试图从 API 中了解 PendingIntent 的用途。 有时我使用的一些方法需要这样做,但我仍然没有得到正确的想法。 谁能解释一下吗?为什么我不能只使用上下文? 谢谢 莫希克 最佳答案 Pen
我在 Android 中开发闹钟时遇到问题。当我触发包含必须在计划时间启动的 Activity 的 PendingIntent 时,它运行良好。但是,当闹钟响起并且我按贪睡 5 分钟并使用不同的请求代
我得到了一个 IntentService,它在 onHandleIntent(Intent) 中同步执行一些长时间运行的工作。因此,只要服务运行,我就会显示一个通知。现在我想在用户点击通知后停止服务。
我有这个... button = (Button) findViewById(R.id.start_repeating); button.setOnClickListener(new OnClickL
在我的应用程序中,我有一个显示 2 个选项的对话框:Copy 和 Dismiss 当用户单击 Copy 时,我希望在状态栏中收到通知。 public void showDialog(final Str
我有一个会显示通知的服务。通知显示正确,但当我点击通知时没有任何反应。它应该在我的应用程序中打开一个 Activity 。这是我的代码: public class ServiceCheckExpens
我正在我的游戏中创建一个通知。如果玩家有一段时间没有玩游戏,我想显示通知。我正在使用以下带有警报设置的 PendingIntent 来创建通知。有了这个,我就可以创建通知。 问题:如果玩家玩游戏,我需
所以这段代码是在我的服务开始在前台启动后从内部运行的。这也会创建将留在那里的通知。我有很多问题/疑问: 1) 点击通知只会打开应用信息,不会打开我的 PendingIntent。 2) 未设置我的内容
如何创建多个 PendingIntent?我有几个通知,当用户按下最后一个通知时,一切正常,但点击通知没有响应。我的代码是: Intent notificationIntent = new Inten
我正在运行一项服务,该服务会在特定事件的通知栏上发出通知。在该通知中,我设置了一个 PendingIntent 以启动另一个应用程序。出于某种原因,当我根据通知采取行动时,它不会启动其他应用程序。这是
我有一个名为 sendNotification 的方法,可以多次调用。 private PendingIntent createNotificationIntent(Context context,
我有一个导航应用程序。我想获得可能暂时不会到达的位置信息,具体取决于 GPS 锁定所需的时间——或者接收信号不好时的时间。 我计划使用 LocationManager.requestLocationU
我有一个 Android 库,它有一个创建 Notification 的 Service。据我了解,Notification 必须有一个 contentIntent (PendingIntent) 集
当我们将 0 作为标志传递给 PendingIntent 时,如下所示: PendingIntent pi=PendingIntent.getActivity(this, 1, i, 0); does
我在尝试通过 Intent 和未决 Intent 将数据传递给 BroadcastReceiver 时遇到了一些问题,涉及接近警报。更具体地说,我试图传递一个对象,其中包括用户不断变化的位置。我已经尝
我是一名优秀的程序员,十分优秀!