我試圖在按下分享按鈕時從其他應用程序接收數據。應用程序顯示在選擇器中,當我按下應用程序時,它會打開,但我無法獲得文本!intent.getAction()和intent.getType()返回null
這是我的啓動畫面,如果它有任何意義。
Cover.java
public class Cover extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(Cover.this,MainActivity.class));
this.finish();
}
}
MainActivity.java
onCreate(...)
setContentView(....)
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
Log.d("nikesh"," "+action); //this prints null
Log.d("nikesh"," "+type); //this prints null
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent);
}
}
private void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
Log.d("khee",sharedText); //these are
if (sharedText != null) { //not printed
Log.d("khee",sharedText);
textView.setText(sharedText);
// Update UI to reflect text being shared
}
}
的manifest.xml
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
intent.getIntent()返回null?也許在方法名稱中有一個錯字?你的意思是intent.getType? –
@RobertEstivill對不起,這是getType(); –
Docs令人驚訝的幫助:https://developer.android.com/reference/android/content/Intent.html#getType() –