我试着创建一个简单的应用程序,点击按钮打开whatsapp。我创建了一个打开whatsapp的意图。但是在单击按钮时,意图返回null。
package com.myown.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Telephony;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private final String LOG_TAG = MainActivity.class.getSimpleName();
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.whatsapp");
Log.i(LOG_TAG,"Onclick called");
if(LaunchIntent!=null)
{startActivity(LaunchIntent);}
else
{
Toast.makeText(MainActivity.this,"Application Not Found!",Toast.LENGTH_LONG).show();
}
}
});
}
}
单击按钮后,意图返回null,这是我收到的日志消息。
2022-07-28 08:47:25.211 26757-26757/com.myown.myapplication D/ViewRootImpl: enqueueInputEventMotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=462.0, y[0]=1368.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x2, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=131790073, downTime=131790073, deviceId=3, source=0x1002, displayId=0 }
2022-07-28 08:47:25.212 26757-26757/com.myown.myapplication D/ViewRootImpl[MainActivity]: processMotionEvent MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=462.0, y[0]=1368.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x2, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=131790073, downTime=131790073, deviceId=3, source=0x1002, displayId=0 }
2022-07-28 08:47:25.218 26757-26757/com.myown.myapplication D/ViewRootImpl[MainActivity]: dispatchPointerEvent handled=true, event=MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=462.0, y[0]=1368.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x2, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=131790073, downTime=131790073, deviceId=3, source=0x1002, displayId=0 }
2022-07-28 08:47:25.291 26757-26757/com.myown.myapplication D/ViewRootImpl[MainActivity]: processMotionEvent MotionEvent { action=ACTION_UP, actionButton=0, id[0]=0, x[0]=462.0, y[0]=1368.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x2, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=131790153, downTime=131790073, deviceId=3, source=0x1002, displayId=0 }
2022-07-28 08:47:25.292 26757-26757/com.myown.myapplication D/ViewRootImpl[MainActivity]: dispatchPointerEvent handled=true, event=MotionEvent { action=ACTION_UP, actionButton=0, id[0]=0, x[0]=462.0, y[0]=1368.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x2, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=131790153, downTime=131790073, deviceId=3, source=0x1002, displayId=0 }
2022-07-28 08:47:25.300 26757-26757/com.myown.myapplication I/MainActivity: Onclick called
这是我的xml代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="157dp"
android:layout_marginTop="345dp"
android:layout_marginEnd="160dp"
android:layout_marginBottom="338dp"
android:text="@string/openwa"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
发布于 2022-07-29 01:45:22
当我搜索这个问题时,我从官方的Whatsapp获得了一个解决方案:https://faq.whatsapp.com/1530794880435103/?locale=en_US
我在代码中尝试了这个代码段:
Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); sendIntent.setType("text/plain"); startActivity(sendIntent);
sendIntent.setPackage("com.whatsapp");
现在一切都很好
发布于 2022-07-28 04:59:42
在我们的活动中尝试这组代码,从您自己的android应用程序中打开另一个应用程序(这里是Whatsapp)。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openToChat(view, "+919000090000")
}
});
}
public static void openToChat(View v, String toNumber) {
String url = "https://wa.me/" + toNumber;
try {
PackageManager pm = v.getContext().getPackageManager();
pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
v.getContext().startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
Toast.makeText(MainActivity.activity, "Whatsapp app not installed in your phone", Toast.LENGTH_SHORT).show();
// v.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
}
https://stackoverflow.com/questions/73146872
复制相似问题