隐含意图不起作用

时间:2017-12-06 20:07:14

标签: android android-intent

您好我从互联网上可以学习的一门课程中学习意图 并且任务是构建使用隐式意图的应用程序:

1.与网页连接 - 并且工作正常 2.连接显示位置的地图 - 不起作用 3.分享共享数据的意图 - 不是工作

这是Java代码:

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ShareCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import java.net.URI;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClickOpenWebpageButton(View v) {
        String urlAsString = "http://example.com";
        openWebPage(urlAsString);


    }

    public void onClickShareTextButton(View v) {
        String textThatYouWantToShare = "Sharing the coolest thing I've learned so far. You should " +
                "check out ###### and ####### Android ######!";

        shareText(textThatYouWantToShare);
    }
    public void onClickOpenAddressButton(View v) {
        String addressString = "1600 Amphitheatre Parkway, CA";

        Uri.Builder builder = new Uri.Builder();
        builder.scheme("geo")
                .path("0.0")
                .query(addressString);
        Uri addressUri = builder.build();

        showMap(addressUri);
    }

    private void openWebPage(String url) {

        Uri webpage = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }

    }

    private void showMap(Uri geoLocation) {

        Intent intent = new Intent(Intent.ACTION_VIEW);

        intent.setData(geoLocation);

        if (intent.resolveActivity(getPackageManager()) != null) {

            startActivity(intent);
        }
    }

    private void shareText(String textToShare) {

        String mimeType = "text/plain";

        String title = "Learning how to share";

        ShareCompat.IntentBuilder
                .from(this)
                .setType(mimeType)
                .setChooserTitle(title)
                .setText(textToShare)
                .startChooser();

    }
}

和XML布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="32dp"
    tools:context=".MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:onClick="onClickOpenWebpageButton"
        android:text="Open Website"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:onClick="onClickOpenAddressButton"
        android:text="Open Location in Map"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:onClick="onClickShareTextButton"
        android:text="Share Text Content"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:onClick="createYourOwn"
        android:text="Create Your Own"/>

</LinearLayout>

我以为我忘记了清单中的一些权限,但我没有&#39;知道哪一个

0 个答案:

没有答案