2017-07-27 204 views
0

我試過這麼多的解決方案,但仍然沒有成功發送消息到特定的WhatsApp聯繫人在我的應用程序。這裏是我的代碼:通過WhatsApp在特定的聯繫人發送消息android

Intent sendIntent = new Intent("android.intent.action.MAIN"); 
       sendIntent.setAction(Intent.ACTION_SEND); 
       sendIntent.setType("text/plain"); 
       sendIntent.putExtra(Intent.EXTRA_TEXT, "Hey"); 
       sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators(smsNumber) + "@s.whatsapp.net"); 
       sendIntent.setPackage("com.whatsapp"); 
       mContext.startActivity(sendIntent); 

它只是打開特定聯繫人的聊天窗口,新的對話與沒有接觸名稱,個人檔案相片和舊的轉換。

請幫我解決這個問題。還附上截圖。

enter image description here

回答

1

我想你想要這樣。

private void openWhatsApp() { 
     String text = message.getText().toString(); 
     if(whatsappInstalledOrNot("com.whatsapp")){ 
      Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("whatsapp://send?text="+text+"&phone="+mobileNumber.getText().toString())); 
      startActivity(browserIntent); 
     }else { 
      Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT) 
        .show(); 
     } 
    } 
private boolean whatsappInstalledOrNot(String uri) { 
     PackageManager pm = getPackageManager(); 
     boolean app_installed = false; 
     try { 
      pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); 
      app_installed = true; 
     } catch (PackageManager.NameNotFoundException e) { 
      app_installed = false; 
     } 
     return app_installed; 
    } 

希望這會有所幫助。

+0

可讀性註釋:'whatsappInstalledOrNot()'應該更正確地命名'isWhatsappInstalled' –

+1

感謝您的建議。 –

+0

@Amrish Kakadiya我嘗試過,發現它的唯一接受號碼與國家代碼也需要時間來搜索whatsapp聯繫人列表中的特定聯繫人。 –

0
Intent sendIntent = new Intent("android.intent.action.MAIN"); 
     sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.Conversation")); 
     sendIntent.putExtra(Intent.EXTRA_TEXT, "Hey"); 
     sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("91xxxx008686")+"@s.whatsapp.net"); 
//phone number without "+" prefix (countrycode & contact number without '+') 

     startActivity(sendIntent); 

您錯過了從「對話」類中檢索舊對話的設置組件部分。

相關問題