我试图在按下分享按钮时从其他应用程序接收数据。应用程序显示在选择器中,当我按下应用程序时,它会打开,但我无法获得文本!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() –