1. 程式人生 > >android app跳轉到微信

android app跳轉到微信

今天寫這片文章主要是記錄下 app跳轉到微信的實現方法,我的專案需求是跳轉到微信公眾號,由於微信官方關閉了這個直接可以跳到公眾號的介面,只能 從app開啟微信,讓使用者自己去搜索。

我的專案需求:


點選跳轉微信的時候,我實現了點選複製的方法,這樣客戶也省去了輸入公眾號的繁瑣。

點選複製文字的程式碼:

ClipboardManager tvCopy = (ClipboardManager) getBaseActivity().getSystemService(Context.CLIPBOARD_SERVICE);
tvCopy.setText("XXX");

XXX即為你的公眾號。

如圖所示:點選去關注跳轉到微信,就開啟微信了。

/**
 * 跳轉到微信
*/
private void getWechatApi(){
    try {
        Intent intent = new Intent(Intent.ACTION_MAIN);
ComponentName cmp = new ComponentName("com.tencent.mm","com.tencent.mm.ui.LauncherUI");
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(cmp);
startActivity(intent); } catch (ActivityNotFoundException e) { // TODO: handle exception getBaseActivity().showToastLong("檢查到您手機沒有安裝微信,請安裝後使用該功能"); } }
裡面的 showToastLong方法即為自定義的Toast提示。

OK,使用者自己開啟微信公眾號直接貼上上搜索就可以了。

題外話:

點選複製,傳參

ClipboardManager tvCopy = (ClipboardManager) getBaseActivity().getSystemService(Context.CLIPBOARD_SERVICE
); tvCopy.setText("XXX");
XXX可以直接使用下面的方法替代:
tv.getText().toString().trim();
獲取複製的內容:
ClipboardManager tvPaste = (ClipboardManager) getBaseActivity().getSystemService(Context.CLIPBOARD_SERVICE);
String content = tvPaste.getText().toString().trim();
content就是你想要的內容。