意图和PendingIntent之间的差异 [英] Differences between Intent and PendingIntent

查看:130
本文介绍了意图和PendingIntent之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过阅读一些文章,似乎都做同样的事情,我想知道是什么开始之间的区别一样,该服务:

I read through some articles and both seem to do the same thing and I was wondering what is the difference between starting the service like that:

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); 

当我读到过,那些2做同样的事情,如果在服务返回的参数START_STICKY;

As I read through, those 2 do the same thing, if in the service you return a parameter START_STICKY;

推荐答案

意图

这是Android的意图是贯彻意图,即一个对象。来自一个组件的消息到另一个组件与-在应用程序或应用程序之外。该意图还可以在任何应用程序的三个核心组件的通信信息 - 活动,服务和广播接收器

An Android Intent is an object carrying an intent ie. message from one component to another component with-in the application or outside the application. The intents can communicate messages among any of the three core components of an application - activities, services, and broadcast receivers.

意图本身,一个意图对象,要执行一个无源数据结构保持的动作的抽象描述。

例如,让我们假设你有一个需要启动一个电子邮件客户端和发送使用Android设备的电子邮件的活动。为了这个目的,你的活动将发送ACTION_SEND以及适当的选择器,到Android意图解析器。指定选择器给用户适当的界面来接如何发送电子邮件的数据。

For example, let's assume that you have an Activity that needs to launch an email client and sends an email using your Android device. For this purpose, your Activity would send an ACTION_SEND along with appropriate chooser, to the Android Intent Resolver. The specified chooser gives the proper interface for the user to pick how to send your email data.

显式意图

// 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);

隐意图

// 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或其他第三方应用程序),它允许外国申请使用应用程序的权限执行predefined一块令牌的code。

A PendingIntent is a token that you give to a foreign application (e.g. NotificationManager, AlarmManager, Home Screen AppWidgetManager, or other 3rd party applications), which allows the foreign application to use your application's permissions to execute a predefined piece of code.

通过向其他应用程序提供一个PendingIntent,你给予它执行您指定的操作,就好像其他应用程序是你自己(有相同的权限和身份)的权利。因此,你应该小心你是如何构建的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: 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.

实例待定意图: http://android-pending-intent.blogspot.in/

来源: Android的意图和的 Android的未决意图

希望这有助于。

这篇关于意图和PendingIntent之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆