2017-02-09 106 views
1

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

intent.getIntent()返回null?也许在方法名称中有一个错字?你的意思是intent.getType? –

+0

@RobertEstivill对不起,这是getType(); –

+0

Docs令人惊讶的帮助:https://developer.android.com/reference/android/content/Intent.html#getType() –

回答

0

您所使用的明确意图,这并没有得到由系统和解决IntentFilters。

startActivity(new Intent(Cover.this, MainActivity.class)); 

如果你仍然想使用类开始活动,你将不得不调用setAction on the Intent.

Intent intent = new Intent(Cover.this,MainActivity.class); 
intent.setAction("android.intent.action.SEND"); 
startActivity(intent); 

或无视明确的部分,以及刚刚成立的动作

Intent intent = new Intent(); 
intent.setAction("android.intent.action.SEND"); 
startActivity(intent); 
+0

雅,我做了你所说的,现在,马getAction()值为android.intent.action.SEND但getType()返回null –

+0

因为你需要通过调用setType '。显式意图不设置默认值。阅读下面的答案,以了解显式和隐式意图之间的区别http://stackoverflow.com/questions/2914881/android-implicit-intents-vs-explicit-intents –

+0

我传intent.setType(“text/plain”);这是正确的价值吗?我没有得到共享文本:( –

0

我在启动画面中做了这个。 txt是这里的关键。

Intent intent = new Intent(Cover.this,MainActivity.class); 
    intent.setAction("android.intent.action.SEND"); 
    intent.setType("text/plain"); 
    intent.putExtra("txt",getIntent().getStringExtra(Intent.EXTRA_TEXT)); 
    startActivity(intent); 
    this.finish(); 

MainActivity.java

Intent intent = getActivity().getIntent(); 
    String action = intent.getAction(); 
    String type = intent.getType(); 

    if (Intent.ACTION_SEND.equals(action) && type != null) { 
     if ("text/plain".equals(type)) { 
      handleSendText(intent); 
     } 
    } 


    } 
    public void handleSendText(Intent intent) { 
    String sharedText = getActivity().getIntent().getExtras().getString("txt"); 
    if (sharedText != null) { 
     textView.setText(sharedText); 
    } 
} 

谢谢你这么多@Robert Estivill让我发现问题!