0

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 
} 

但它不起作用!你知道爲什麼嗎?我做錯了什麼?

在此先感謝!

回答

2

因此,我想通過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的導出服務。

1

PendingIntent不授予執行任何操作的權限。它所做的就是使這個意圖的啓動,就好像待定內容創建者本身所做的那樣。

例如,您有一個應用程序有權啓動受保護的廣播。您的應用可以創建pendingBroadcast並將該對象傳遞給任何其他沒有此權限的應用,但其他應用可以使用此pendingIntent進行廣播。

相關問題