Button clickButton = (Button) findViewById(R.id.newbutton); 
 
        assert clickButton != null; 
 
        clickButton.setOnClickListener(new View.OnClickListener() { 
 
            @Override 
 
            public void onClick(View v) { 
 
 
    Intent uriIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("whatsapp://send?text=" + "Text you want to share")); 
 
  startActivity(uriIntent); 
 
 } 
 
   }); 

请您参考如下方法:

这是预期的行为,因为某些用户没有安装某些应用程序绝对是合理的。您需要通过调用intent.resolveActivity()并检查结果是否不为空来检查是否有任何 Activity 可以处理该意图。

有关意图和意图过滤器的开发文档将其声明为here:

Caution: It's possible that a user won't have any apps that handle the implicit intent you send to startActivity(). Or, an app may be inaccessible because of profile restrictions or settings put into place by an administrator. If that happens, the call fails and your app crashes. To verify that an activity will receive the intent, call resolveActivity() on your Intent object. If the result is non-null, there is at least one app that can handle the intent and it's safe to call startActivity(). If the result is null, do not use the intent and, if possible, you should disable the feature that issues the intent. The following example shows how to verify that the intent resolves to an activity. This example doesn't use a URI, but the intent's data type is declared to specify the content carried by the extras.


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!