透過 Android 中的 WhatsApp 傳送資訊?
此示例演示了透過 Android 中的 WhatsApp 傳送訊息
步驟 1 - 在 Android Studio 中建立一個新專案,轉到檔案 ⇒ 新專案並填寫所有必需詳細資訊以建立一個新專案。
步驟 2 - 將以下程式碼新增到 res/layout/activity_main.xml 中。
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "match_parent" android:gravity = "center" android:layout_height = "match_parent"> <TextView android:id = "@+id/text" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:text = "click" android:textSize = "30sp" /> </LinearLayout>
在上面的程式碼中,我們使用了文字檢視。
步驟 3 - 將以下程式碼新增到 src/MainActivity.java 中
<?xml version = "1.0" encoding = "utf-8"?>
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PackageManager pm = MainActivity.this.getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "YOUR TEXT HERE";
PackageInfo info = pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (PackageManager.NameNotFoundException e) {
Toast.makeText(MainActivity.this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}
});
}
}讓我們嘗試執行你的應用程式。我認為你已將實際的 Android 移動裝置連線到計算機。若要從 Android Studio 執行應用,請開啟一個專案的活動檔案並單擊工具欄中的執行
圖示。選擇你的移動裝置作為選項,然後檢查你的移動裝置,它將顯示你的預設螢幕 -

現在單擊文字檢視開啟 WhatsApp

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP