掃二維碼下載apk并統(tǒng)計被掃描次數(shù)
需求:想讓用戶掃描一個二維碼就能下載APP,并統(tǒng)計被掃描次數(shù)。
兩種實現(xiàn)方法:
1.一般我們用草料生成二維碼,如果沒有注冊的話只能生成一個包含下載網(wǎng)址的靜態(tài)碼,沒有統(tǒng)計功能,而且出了自己截圖保存外,草料是不會保存你的二維碼的。
如果注冊草料后,可以選擇生成活碼。所謂活碼,就是一個指向頁面,然后通過這個指向頁面,再到你的下載鏈接。這個指向頁面內(nèi)嵌了統(tǒng)計代碼。你可以通過草料的統(tǒng)計功能,看你的二維碼相關(guān)的掃描數(shù)據(jù)。
2.你的App下載地址,自己內(nèi)嵌一個統(tǒng)計代碼,這樣來統(tǒng)計掃描數(shù)據(jù),這樣,你只要一個靜態(tài)碼就夠了。不需要在草料注冊,用戶掃描二維碼后,直接進(jìn)入下載界面,沒有中間的指向頁面。
由于不希望自己的app投放到應(yīng)用市場,因此微下載行不通。比如,把你的APK文件上傳到騰訊的開放平臺,申請通過后,會拿到一個移動推廣鏈接,然后替換原來的“android下載”的鏈接(直接此文件生成一個二維碼也行),這樣用戶就可以在微信中掃一掃直接下載了。
同時,一般用戶用掃一掃,大多都用微信自帶“掃一掃”工具,而微信打開的二維碼頁面,會自動屏蔽apk文件,所有顯然把a(bǔ)pk的url生成一個二維碼,讓用戶掃一掃后就能直接下載,這樣是行不通的。微信做了限制除了和微信有合作關(guān)系的應(yīng)用才能使用微信掃描后直接下載apk,其他的應(yīng)用只能點擊微信右上角的菜單跳轉(zhuǎn)到普通瀏覽器下載apk。
嘗試:
用草料生成二維碼:
“文件”方式生成二維碼:上傳需要下載的文件,生成二維碼,掃描二維碼跳轉(zhuǎn)到它默認(rèn)的模板頁面,點擊可下載該文件。但是不支持.apk這樣的特殊格式。
“網(wǎng)址”方式生成二維碼:直接將.apk的下載地址url生成二維碼,掃描下載行不通。微信將其屏蔽了(QQ中的“掃一掃”功能是可以的,易信、360也都可以掃出來)。
所以最終采用第二種方法。
實現(xiàn)方案:
直接判斷微信的ua,然后彈出一個遮罩提示用戶在新的瀏覽器中打開下載,并且加關(guān)閉的按鈕,類似于如圖。

(1)在你的服務(wù)器上寫一個下載詳情頁面,將app下載鏈接放上去。這里設(shè)計的是:使用JS+HTML+CSS結(jié)合的方式,用移動h5技術(shù)適配了手機(jī)版網(wǎng)頁,不會在已進(jìn)入微信就彈出提示它在新瀏覽器中打開,因為你還可以在這個頁面里做一些提交表單查看信息等操作。只有用戶點擊應(yīng)用下載鏈接才彈出遮罩提示跳轉(zhuǎn)至新的瀏覽器下載,如圖:

(2)把下載頁面的URL地址,通過"草料二維碼"生成一個二維碼,如圖:

(3)如果是在微信里掃一掃打開的,當(dāng)用戶點擊“安卓版下載”的時候,就提示用戶要在默認(rèn)瀏覽器中打開,如圖:

(4)其實掃描二維碼,就是訪問一個url,可以在后臺統(tǒng)計url被訪問的次數(shù),就是掃描二維碼的次數(shù)了。
貼出關(guān)鍵代碼:
public class Counter{
private int count;
public Counter(){
this(0);
}
public Counter(int count){
this.count=count;
}
public void setCount(int count){
this.count=count;
}
public int getCount(){
return count;
}
public void add(int step){
count+=step;
}
}
/**
* 統(tǒng)計頁面訪問的次數(shù),并在關(guān)閉應(yīng)用時將其保存到文件,待下次啟應(yīng)用時讀取次數(shù)。
* @author Joanna.Yan
*
*/
public class MyServletContextListener implements ServletContextListener{
public void contextInitialized(ServletContextEvent sce){
System.out.println("====================helloapp application is Initialized.==========");
ServletContext context=sce.getServletContext();
try{
BufferedReader reader = new BufferedReader(
new InputStreamReader(context.getResourceAsStream("/count/count.txt")));
int count = Integer.parseInt(reader.readLine());
reader.close();
Counter counter = new Counter(count);
context.setAttribute("counter",counter);
}catch(IOException e){e.printStackTrace();}
}
public void contextDestroyed(ServletContextEvent sce){
System.out.println("helloapp application is Destroyed.");
ServletContext context=sce.getServletContext();
Counter counter=(Counter)context.getAttribute("counter");
if(counter != null){
try{
String filepath = context.getRealPath("/count");
filepath = filepath+"/count.txt";
PrintWriter pw= new PrintWriter(filepath);
pw.println(counter.getCount());
pw.close();
}catch(IOException e){e.printStackTrace();};
}
}
}
web.xml中注冊監(jiān)聽器:
<listener> <listener-class>joanna.yan.listener.MyServletContextListener</listener-class> </listener>
web.xml中注冊servlet:
<servlet> <servlet-name>QRCodeServlet</servlet-name> <servlet-class>joanna.yan.servlet.QRCodeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>QRCodeServlet</servlet-name> <url-pattern>/QRCode</url-pattern> </servlet-mapping>
public class QRCodeServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("QRCodeServlet被訪問了!");
ServletContext context=getServletContext();
Counter counter=(Counter) context.getAttribute("counter");
if(counter==null){
counter=new Counter(1);
context.setAttribute("counter", counter);
}
counter.add(1);
System.out.println("被掃描的次數(shù):"+counter.getCount());
resp.sendRedirect(""+req.getContextPath()+"/apkdownload.jsp");
}
}
設(shè)置適配移動端屏幕,禁止瀏覽器的縮放功能:
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0">
CSS+JS:
<style type="text/css">
#weixin-tip{display:none;position:fixed;left:0;top:0;background:rgba(0,0,0,0.8);filter:alpha(opacity=80);width:100%;height:100%;z-index:100;}
#weixin-tip p{text-align:center;margin-top:10%;padding:0 5%;position:relative;}
#weixin-tip .close{color:#fff;padding:5px;font:bold 20px/24px simsun;text-shadow:0 1px 0 #ddd;position:absolute;top:0;left:5%;}
</style>
<script type="text/javascript">
var is_weixin = (function(){return navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1;})();
window.onload = function() {
var winHeight = typeof window.innerHeight != 'undefined' ? window.innerHeight : document.documentElement.clientHeight; //兼容IOS,不需要的可以去掉
var btn = document.getElementById('J_weixin');
var tip = document.getElementById('weixin-tip');
var close = document.getElementById('close');
if (is_weixin) {
btn.onclick = function(e) {
tip.style.height = winHeight + 'px'; //兼容IOS彈窗整屏
tip.style.display = 'block';
return false;
};
close.onclick = function() {
tip.style.display = 'none';
};
}
};
</script>
....你的網(wǎng)頁代碼...... <div id="weixin-tip"> <p> <img alt="微信打開" src="img/warn.png"> <span id="close" title="關(guān)閉" class="close">X</span> </p> </div>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
Android開發(fā)中Looper.prepare()和Looper.loop()
Looper用于封裝了android線程中的消息循環(huán),默認(rèn)情況下一個線程是不存在消息循環(huán)(message loop)的,具體調(diào)用方法大家可以通過本文學(xué)習(xí)2016-11-11
Android開發(fā)實現(xiàn)Fragment監(jiān)聽返回鍵事件功能的方法
這篇文章主要介紹了Android開發(fā)實現(xiàn)Fragment監(jiān)聽返回鍵事件功能的方法,結(jié)合實例形式分析了Android使用Fragment監(jiān)聽并屏蔽返回鍵按鈕的實現(xiàn)方法與相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
Flutter StreamBuilder組件實現(xiàn)局部刷新示例講解
日常使用最多的局部刷新為Provider狀態(tài)管理 Selector,今天分享flutter框架自帶的StreamBuilder組件,該組件可做到局部刷新,使用簡單且輕便2022-11-11
Android中使用BitmapShader類來制作各種圖片的圓角
這篇文章主要介紹了Android中使用BitmapShader類來制作各種圖片的圓角的方法,文中隨教程講解帶出的例子可以輕松控制圖片圓形的變換,很好很強(qiáng)大,需要的朋友可以參考下2016-04-04
Android虛擬導(dǎo)航欄遮擋底部的輸入框的解決方法
下面小編就為大家分享一篇Android虛擬導(dǎo)航欄遮擋底部的輸入框的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01

