1
我的應用正在註冊一個接收URL的意圖,所以當用戶共享一個url時,我的應用將會在應用列表中。如何從ACTION_SEND獲取網址?
<intent-filter
android:label="my app">
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
-
在我的MainActivity的onCreate()方法:
String action = intent.getAction();
if (action.equalsIgnoreCase(Intent.ACTION_SEND)) {
Uri data = intent.getData();
String s = data.toString();
output.setText(s); //output: a TextView that holds the URL
}
-
我的問題是:數據爲空這是奇怪的。由於此代碼與ACTION_VIEW
完美配合。但是,它不適用於ACTION_SEND
。
我該如何修改它才能正常工作?
完美!還有一個問題:爲什麼我需要檢查'Intent.hasExtra()'? – iTurki 2012-03-13 00:05:02
@iturki我只是覺得最好在檢索數據之前檢查數據是否存在。避免惱人的NullPointerException。 – 2012-03-13 00:10:26
對。謝謝:) – iTurki 2012-03-13 00:15:15