- 直接调用QQ客户端的聊天功能,用一个输入客服QQ号就能直接用了
直接上效果图片
- 页面布局,就一个文本框:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="QQ客服"
android:padding="15dp"
android:gravity="center" />
</LinearLayout>
- activity代码:
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private TextView tv_custom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_custom = (TextView) findViewById(R.id.tv_custom);
tv_custom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isSpecialApplInstalled(MainActivity.this,"com.tencent.mobileqq")) {
String url = "mqqwpa://im/chat?chat_type=wpa&uin=客服的QQ号";
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}else{
showToast(MainActivity.this,"检测到没有安装QQ哦!");
}
}
});
}
private void showToast(MainActivity mainActivity, String s) {
Toast.makeText(mainActivity,s,Toast.LENGTH_SHORT).show();
}
/**
* 判断手机设备是否安装指定包名的apk应用程序
*
* @param context
* @param packageName
* @return
*/
public boolean isSpecialApplInstalled(Context context, String packageName) {
PackageManager packageManager = context.getPackageManager();
try {
packageManager.getPackageInfo(packageName, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return false;
}
}
参考原文:http://blog.csdn.net/qq_26665903/article/details/52912743