【问题标题】:Sharing URL from other app从其他应用程序共享 URL
【发布时间】:2016-11-18 16:28:09
【问题描述】:

我想从其他应用分享 URL 并将其添加到 EditText 字段中

清单:

   <intent-filter>
    <action android:name="android.intent.action.SEND" />
     <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
        </intent-filter>

活动:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et1=(EditText)findViewById(R.id.et1);

    Bundle extras = getIntent().getExtras();

    String value1 = extras.getString(Intent.EXTRA_TEXT);
    et1.setText(value1);

但是当我尝试关闭它运行应用程序时,我做错了什么?我在共享列表中看到我的应用,所以清单还不错

添加:字符串通道的 LogCat 错误

【问题讨论】:

  • 使用 LogCat 检查与您的应用关联的 Java 堆栈跟踪:stackoverflow.com/questions/23353173/…
  • 您的 Activity 的onCreate() 中可能存在严重错误,我们不知道,因为代码不在问题中。有合适的setContentView() 电话吗?是否有(成功的)findViewById() 呼叫EditTextsetText() 是在这些之后而不是之前等等。您应该分享更多代码。 (当然,按照建议从 LogCat 中检查并分享崩溃日志。)
  • 我编辑并添加了完整代码,但我认为一切正常,编辑 logcat 警告

标签: java android parameter-passing intentfilter


【解决方案1】:

固定代码:

    Intent receivedIntent = getIntent();
    String receivedAction = receivedIntent.getAction();
    String receivedType = receivedIntent.getType();
    //make sure it's an action and type we can handle
    if(receivedAction.equals(Intent.ACTION_SEND)){
        //content is being shared
    }
    else if (receivedAction.equals(Intent.ACTION_MAIN)){

    }

    String receivedText = receivedIntent.getStringExtra(Intent.EXTRA_TEXT);

    if (receivedText != null) {

        et1.setText(receivedText);
    }

大家好

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-17
    • 2017-04-26
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 2022-11-15
    相关资源
    最近更新 更多