提供因特网接入Android应用程序通过的PendingIntent

问题描述:

Android应用程序可以访问Internet:提供因特网接入Android应用程序通过的PendingIntent

<uses-permission android:name="android.permission.INTERNET"/> 

应用B没有上网。所以我想通过PendingIntent从应用程序A访问应用程序B。这是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). 

我就是这样在应用一个发送PendingIntent

Intent mainApp = new Intent(Intent.ACTION_MAIN); 
mainApp.addCategory(Intent.CATEGORY_LAUNCHER); 
mainApp.setClassName("com.other.package", "com.other.package.MainActivity"); 
int flags = PendingIntent.FLAG_UPDATE_CURRENT | Intent.FLAG_ACTIVITY_NEW_TASK; 
PendingIntent pi = PendingIntent.getActivity(this, 23894729834, mainApp, flags); 
try { 
    pi.send(); 
} 
catch (CanceledException e) { } 

这是我尝试接收它的应用程序B(其中有launchMode = singleTask):

@Override 
protected void onNewIntent(Intent i) { 
    setIntent(i); 
    // do some things with Internet access here 
} 

但它不起作用!你知道为什么吗?我做错了什么?

在此先感谢!

因此,我想通过PendingIntent为应用程序A提供互联网访问。

这是不可能的,对不起。

这是PendingIntent的用途,不是吗?

让我们仔细看一下,你所引用的文件的部分:

通过赋予的PendingIntent到另一个应用程序,即表示您授权它来执行的权您已指定,就好像其他应用程序是您自己的(具有相同的权限和标识)。

(为了强调粗体矿)

App B就可以了PendingIntent通过send()执行,它将运行好像应用一个已经执行它,如果你到PendingIntent发送到应用B(例如,作为额外的Intent)。但是:

  • 你有一个应用程序的PendingIntent通过send()执行,启动应用程序B,而不是将PendingIntent到App B.

  • 应用B不会奇迹般地得到应用A的权限,只是因为它发生通过由应用程序A.创建了一些PendingIntent要调用

有没有办法让你“给从应用上网A到应用程序B“。应用B可以要求应用A代表其执行请求(例如,通过发送到由应用A公开的服务的命令)。但是,在执行此操作时,您需要非常小心,因为默认情况下,不仅可以请求应用程序A执行任何操作,设备上的任何其他应用程序也可以这样做,除非您使用签名级定制许可保护App A的导出服务。

PendingIntent不授予执行任何操作的权限。它所做的就是使这个意图的启动,就好像待定内容创建者本身所做的那样。

例如,您有一个应用程序有权启动受保护的广播。您的应用可以创建pendingBroadcast并将该对象传递给任何其他没有此权限的应用,但其他应用可以使用此pendingIntent进行广播。