前提:手机里要有qq软件
1.先去这个网址把自己的qq设置成推广功能。
2.代码实现:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_below="@+id/myte"
android:id="@+id/bt_1"
android:layout_marginTop="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QQ客服"
/>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt1=(Button)findViewById(R.id.bt_1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//发起qq咨询
if(isQQClientAvailable(MainActivity.this)){
final String qqUrl = "mqqwpa://im/chat?chat_type=wpa&uin=945750315&version=1";/*uin=945750315,把qq号换成你自己的*/
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(qqUrl)));
}else{
Toast.makeText(MainActivity.this,"请安装QQ客户端!",Toast.LENGTH_SHORT).show();
}
}
});
}
/**
* 判断QQ客户端是否安装
* @param context
* @return
*/
public static boolean isQQClientAvailable(Context context) {
final PackageManager packageManager = context.getPackageManager();
List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
if (pinfo != null) {
for (int i = 0; i < pinfo.size(); i++) {
String pn = pinfo.get(i).packageName;
if (pn.equals("com.tencent.mobileqq")) {
return true;
}
}
}
return false;
}
}