无法从另一个Android应用程序访问whatsapp和fb

时间:2015-10-13 08:34:17

标签: android facebook api whatsapp

我想开发一个简单的Android应用程序,它有文本视图和几个按钮。我已经在我的应用程序2 whatsapp和2 facebook上放了4个按钮。我希望每当有人点击whatsapp按钮时,文本视图中的文本被转发用户希望发送消息的whatsapp上的联系人。同样我希望该用户能够将fb中的私人消息发送给他的朋友。该消息与textview中的文本相同。我的MainActivity.java文件位于

package com.example.shalabh.gratbites;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    public TextView textView;
    public TextView textView2;
    public Button button;
    public Button button2;
    public Button button3;
    public Button button4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void onClickListenerButton2()
    {
        textView=(TextView)findViewById(R.id.textView);
        button2=(Button)findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String fb = textView.getText().toString();
                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.setType("text/plain");
                shareIntent.putExtra(Intent.EXTRA_TEXT, fb);
                startActivity(Intent.createChooser(shareIntent,"Share gratitude"));

            }
        });
    }



    public void onClickListenerButton3()
    {
        textView2 = (TextView)findViewById(R.id.textView2);
        button3= (Button)findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String whatsAppMessage = textView2.getText().toString();

                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);
                sendIntent.setType("text/plain");

                // Do not forget to add this to open whatsApp App specifically
                sendIntent.setPackage("com.whatsapp");
                startActivity(sendIntent);

            }
        });


    }

    public void onClickListenerButton()
    {
        textView= (TextView)findViewById(R.id.textView);
        button= (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String whatsAppMessage = textView.getText().toString();

                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);
                sendIntent.setType("text/plain");

                // Do not forget to add this to open whatsApp App specifically
                sendIntent.setPackage("com.whatsapp");
                startActivity(sendIntent);

            }
        });














    }


}

我的main_activity.xml文件是

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:background="#ffffff">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="At times our own light goes out and is rekindled by a spark from another person. Each of us has cause to think with deep gratitude of those who have lighted the flame within us. "
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_marginTop="38dp"
        android:editable="false"
        android:elegantTextHeight="false"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Whatsapp"
        android:id="@+id/button"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="40dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignRight="@id/button"
        android:layout_alignTop="@+id/button"
        android:text="Facebook" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="I wanted to say thanks... and share my gratitude for everything I&apos;ve been blessed with. Family, friends, and continued support from everyone."
        android:id="@+id/textView2"
        android:layout_below="@+id/button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="74dp"
        android:layout_alignRight="@+id/button2"
        android:layout_alignEnd="@+id/button2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="whatsapp"
        android:id="@+id/button3"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="40dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Facebook"
        android:id="@+id/button4"
        android:layout_alignTop="@+id/button3"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />


</RelativeLayout>

问题在于所有按钮都没有响应。我无法理解原因。如果可能的话,建议您采用替代方法,帮我找到问题的解决方案。

P.S。我还没有为button4编码

1 个答案:

答案 0 :(得分:0)

您尚未调用已创建的方法。因此未设置 onClickListeners 。所以只需在onCreate中调用此方法就可以了,你就可以了。

@Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onClickListenerButton2();
        onClickListenerButton3();
        onClickListenerButton();
    }