一,集成腾讯X5
1,直接在build.gradle中添加,然后sync
api 'com.tencent.tbs.tbssdk:sdk:43697'
2,自定义一个X5WebView,集成腾讯X5的webview,直接看代码:
public class X5WebView extends WebView {
private BaseActivity baseActivity;
private WebViewClient client = new WebViewClient() {
// 防止加载网页时调起系统浏览器
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
};
public X5WebView(Context arg0) {
super(arg0);
setBackgroundColor(85621);
}
@SuppressLint("SetJavaScriptEnabled")
@Deprecated
public X5WebView(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
this.setWebViewClient(client);
// this.setWebChromeClient(chromeClient);
// WebStorage webStorage = WebStorage.getInstance();
initWebViewSettings();
this.getView().setClickable(true);
}
@SuppressLint("SetJavaScriptEnabled")
@Deprecated
private void initWebViewSettings() {
WebSettings webSetting = this.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
webSetting.setAllowFileAccess(true);
webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSetting.setSupportZoom(true);
webSetting.setBuiltInZoomControls(true);
webSetting.setUseWideViewPort(true);
webSetting.setSupportMultipleWindows(true);
// webSetting.setLoadWithOverviewMode(true);
webSetting.setAppCacheEnabled(true);
// webSetting.setDatabaseEnabled(true);
webSetting.setDomStorageEnabled(true);
webSetting.setGeolocationEnabled(true);
webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
// webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
// webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
//设置默认的JS Object 这里可以自定义
// addJavascriptInterface(new JsInterface(baseActivity), "android");
}
}
3,接下来直接布局文件上自定义view
4,其余的跟原生的webview一样用,直接一个loadurl完事
wv = findViewById(R.id.wv);
wv.addJavascriptInterface(new JsInterface(this), "android");//这里是需要和H5交互的,有需要则加wv("http:www.baidu.com);//添加浏览器地址
5,如果需要和H5交互调用原生方法,就写一个类,跟前端沟通好,贴一个我的
public class JsInterface {
private BaseActivity activity;
/**
* Instantiate the interface and set the context
*/
public JsInterface(BaseActivity activity) {
this.activity = activity;
}
@JavascriptInterface
public void toOpenQQ(String qq) {//打开QQ
if (qq != null) {
//获取剪贴板管理器:
ClipboardManager cm = (ClipboardManager) activity.getSystemService(activity.CLIPBOARD_SERVICE);
// 创建普通字符型ClipData
ClipData mClipData = ClipData.newPlainText("Label", qq);
// 将ClipData内容放到系统剪贴板里。
cm.setPrimaryClip(mClipData);
TipsUtils.toastShort(activity,"复制成功,即将跳转");
Utils.toOpenQQ(activity, qq);
// new AlertDialog.Builder(activity).setMessage("复制成功,即将跳转").setNegativeButton("取消", null).setPositiveButton("确定", (dialog, which) -> Utils.toOpenQQ(activity, qq)).show();
} else {
TipsUtils.toastShort(activity, "复制失败,暂无数据!");
}
}
@JavascriptInterface
public void toOpenWx(String wx) {//打开微信
if (wx != null) {
//获取剪贴板管理器:
ClipboardManager cm = (ClipboardManager) activity.getSystemService(activity.CLIPBOARD_SERVICE);
// 创建普通字符型ClipData
ClipData mClipData = ClipData.newPlainText("Label", wx);
// 将ClipData内容放到系统剪贴板里。
cm.setPrimaryClip(mClipData);
TipsUtils.toastShort(activity,"复制成功,即将跳转");
Utils.toOpenWx(activity);
// new AlertDialog.Builder(activity).setMessage("复制成功,即将跳转").setNegativeButton("取消", null).setPositiveButton("确定", (dialog, which) -> Utils.toOpenWx(activity)).show();
} else {
TipsUtils.toastShort(activity, "复制失败,暂无数据!");
}
}
@JavascriptInterface
public void toOpenTelephoneDial(String phone) {//打开手机 拨打电话
if (phone != null) {
//获取剪贴板管理器:
ClipboardManager cm = (ClipboardManager) activity.getSystemService(activity.CLIPBOARD_SERVICE);
// 创建普通字符型ClipData
ClipData mClipData = ClipData.newPlainText("Label", phone);
// 将ClipData内容放到系统剪贴板里。
cm.setPrimaryClip(mClipData);
TipsUtils.toastShort(activity,"复制成功,即将跳转");
Utils.toOpenTelephoneDial(activity, phone);
} else {
TipsUtils.toastShort(activity, "复制失败,暂无数据!");
}
}
}
完事!
二,集成极光推送
1,前面的注册极光账号以及各种配置包名之类的就不说了,直接主题,先在build.gradle里添加依赖
implementation 'cn.jiguang.sdk:jpush:3.5.6' // 此处以JPush 3.5.6 版本为例。
implementation 'cn.jiguang.sdk:jcore:2.2.8' // 此处以JCore 2.2.8 版本为例。
记得参考极光的文档使用最新版的,还要在defaultConfig里添加配置,不要忘
ndk {
//选择要添加的对应 cpu 类型的 .so 库。
abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
// 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
}
manifestPlaceholders = [
JPUSH_PKGNAME : applicationId,
JPUSH_APPKEY : "44444444444", //JPush 上注册的包名对应的 Appkey.
JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
]
2,在注册文件里添加所需要的权限以及自定义的集成极光的service和receiver
3,自定义的service和receiver类
package com.test.test.service;
import cn.jpush.android.service.JCommonService;
public class JPushService extends JCommonService {
}
package com.test.test.receiver;
import android.content.Context;
import android.util.Log;
import com.hengtong.biying.utils.TipsUtils;
import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;
public class JPushReceiver extends JPushMessageReceiver {
@Override
public void onMessage(Context context, CustomMessage customMessage) {
super.onMessage(context, customMessage);
//消息到达 可指定类型,指定操作
// NotificationBar.showNotifiBar(context,"我是消息","我是消息内容");
TipsUtils.toastShort(context, customMessage.toString());
Log.d("jpush", "onMessage: " + customMessage.message + "/" + customMessage.extra);
}
@Override
public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
super.onNotifyMessageArrived(context, notificationMessage);
// 通知到达
Log.d("jpush", "onNotifyMessageArrived: " + notificationMessage.notificationExtras);
}
@Override
public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
super.onNotifyMessageOpened(context, notificationMessage);
// 点击通知栏消息 可指定类型,指定操作
Log.d("jpush", "onNotifyMessageOpened: " + notificationMessage.notificationExtras);
}
@Override
public void onRegister(Context context, String s) {
super.onRegister(context, s);
// 保存极光id
Log.d("jpush", "onRegister: id=" + s);
}
@Override
public void onConnected(Context context, boolean b) {
super.onConnected(context, b);
Log.d("jpush", "onConnected: " + b);
if (b) {
// 链接监听 保存极光id
String registrationID = JPushInterface.getRegistrationID(context.getApplicationContext());
Log.d("jpush", "onConnected: 连接成功获取id=" + registrationID);
}
}
}
4,在Application里或者开屏页面添加初始化事件,网上看了一些文章都推荐在开屏页面添加初始化事件,这里贴下我的,记得要在create里初始化,
JPushInterface.setDebugMode(true);
JPushInterface.init(context);
if (!isNotificationEnabled(this)) {
gotoSet();//去设置开启通知
} else {
//当前app允许消息通知
}
//注意开启通知权限
private void gotoSet() {
Intent intent = new Intent();
if (Build.VERSION.SDK_INT >= 26) {
// android 8.0引导
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());
} else if (Build.VERSION.SDK_INT >= 21) {
// android 5.0-7.0
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("app_package", getPackageName());
intent.putExtra("app_uid", getApplicationInfo().uid);
} else {
// 其他
intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setData(Uri.fromParts("package", getPackageName(), null));
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
完事收工,极光后台发送一个测试消息试一下吧