簡體   English   中英

Android:從另一個應用程序的服務訪問數據

[英]Android: Access Data from a Service of another application

有兩個應用程序,App1和App2。 我已經在App1中啟動了這樣的服務:

Intent sendIntent = new Intent(this, HelloService.class);
sendIntent.setAction("getDataFromApp1");
sendIntent.putExtra("dataKey", "This is data I am sending.");
startService(sendIntent);

現在,我想從該服務獲取data [我使用putExtra()在Intent中設置的String對象]並將其顯示在App2中。

所以我的問題是如何從另一個應用程序創建的服務訪問數據。 我已經在清單文件中使用android:exported = true聲明了該服務。

您需要在App1中公開內容提供程序,該提供程序提供可在App2中使用的數據。 是的,您需要確保將export設置為true。

您可以從這里開始。 http://developer.android.com/guide/topics/providers/content-provider-creating.html

您可以使用Intent實現此Intent 您可以將所需的任何數據附加到Intent ,也可以在任何應用程序中偵聽某種類型的Intent

例如,您可以發送以下Intent

Intent sendIntent = new Intent();
sendIntent.setAction(your.custom.action.for.app.two);
sendIntent.putExtra("dataKey", "Pass this text to app two.");
startActivity(sendIntent);

然后在第二個應用程序中,將一個意圖過濾器附加到要在AndroidManifest.xml啟動的Activity ,如下所示:

<intent-filter>
        <action android:name="your.custom.action.for.app.two" />
</intent-filter>

然后在該Activity中,檢查您的onCreate是否通過該Intent啟動,並按如下方式處理數據:

 if (getIntent().getAction().equals(your.custom.action.for.app.two) {
     String yourText = getIntent().getStringExtra("dataKey");
 }

您還需要考慮很多其他事項。 就像檢查用戶電話中是否存在第二個應用程序,以及該應用程序未喚醒該怎么辦? 有關更多信息,請參見此處

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM