【问题标题】:intent.getAction() and intent.getType() returns nullintent.getAction() 和 intent.getType() 返回 null
【发布时间】:2017-02-10 12:31:11
【问题描述】:

按下共享按钮时,我正在尝试从其他应用程序接收数据。应用程序显示在选择器中,当我按下应用程序时,它会打开但我无法获取文本!

如果有意义的话,这是我的启动画面。

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();
  • 同样的,你需要在 Intent 对象上设置类型。
  • 哦让我现在试试

标签: android android-intent


【解决方案1】:

您正在使用显式意图,系统和 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);

【讨论】:

  • 是的,我按照你说的做了,现在,获取 getAction() 值作为 android.intent.action.SEND 但 getType() 返回 null
  • 因为需要调用setType来设置。显式意图不设置默认值。阅读以下答案以了解显式意图和隐式意图之间的区别stackoverflow.com/questions/2914881/…
  • 我通过了 intent.setType("text/plain");这是正确的值吗?我没有收到共享文本:(
  • 因为您没有使用键 Intent.EXTRA_TEXT 设置任何 Intent extra
  • 我没有得到你!你能更新你的答案吗?我是一个完整的初学者我遵循了这个developer.android.com/training/sharing/receive.html我在我的封面活动中做了这样的意图意图=新意图(Cover.this,MainActivity.class); //这只是一个启动画面 intent.setAction("android.intent.action.SEND"); intent.setType("文本/纯文本");开始活动(意图); this.finish();
【解决方案2】:

我是在启动画面中完成的。 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让我发现问题!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 2012-01-26
    • 2021-04-11
    • 2015-09-08
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多