以百度推送为例,昨天已给出manifest的例子,这里给出Receiver,继承PushMessageReceiver,复写其中的方法,其中最重要的是onMessage、onNotificationClicked、onNotificationArrived三个方法。
package com.example.administrator.mysensordemo;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.baidu.android.pushservice.PushMessageReceiver;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
/**
* Created by Administrator on 2015/9/23.
*/
public class MyPushMessageReceiver extends PushMessageReceiver {
@Override
public void onBind(Context context, int i, String s, String s1, String s2, String s3) {
}
@Override
public void onUnbind(Context context, int i, String s) {
}
@Override
public void onSetTags(Context context, int i, List list, List list1, String s) {
}
@Override
public void onDelTags(Context context, int i, List list, List list1, String s) {
}
@Override
public void onListTags(Context context, int i, List list, String s) {
}
@Override
public void onMessage(Context context, String message, String customContentString) {
String messageString = "透传消息 message=\"" + message
+ "\" customContentString=" + customContentString;
Log.d(TAG, messageString);
}
@Override
public void onNotificationClicked(Context context, String title,
String description, String customContentString) {
String notifyString = "通知点击 title=\"" + title + "\" description=\""
+ description + "\" customContent=" + customContentString;
Log.d("MyPush", "接收到的消息"+"onNotificationClicked"+notifyString);
try {
JSONObject object = new JSONObject(customContentString);
String className = object.getString("startActivity");
Intent intent = new Intent(context,Class.forName(context.getPackageName()+"."+className));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Override
public void onNotificationArrived(Context context, String title, String description, String customContentString) {
String notifyString = "onNotificationArrived title=\"" + title
+ "\" description=\"" + description + "\" customContent="
+ customContentString;
Log.d(TAG, notifyString);
}
}
数据统计主要有两个作用,用户分析和应用分析,其中用户分析是针对地域、下载渠道、用户年龄等等,而应用分析是针对错误日志、界面停留时间、应用打开次数等等。
以百度数据统计为例,同样需要导入jar包和so文件,同样需要用包名申请key,同样需要加入权限。因为所有的Activity都需要复写onResume和onPause方法,所以写一个BaseActivity继承Activity,复写两个方法,然后所有的Activity都继承BaseActivity。下面给出manifest和BaseActivity的例子。
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
package="com.example.administrator.mylocationdemo" >
"android.permission.ACCESS_COARSE_LOCATION">
"android.permission.ACCESS_FINE_LOCATION">
"android.permission.ACCESS_WIFI_STATE">
"android.permission.ACCESS_NETWORK_STATE">
"android.permission.CHANGE_WIFI_STATE">
"android.permission.READ_PHONE_STATE">
"android.permission.WRITE_EXTERNAL_STORAGE">
"android.permission.INTERNET" />
"android.permission.MOUNT_UNMOUNT_FILESYSTEMS">
"android.permission.INTERNET" />
"android.permission.READ_PHONE_STATE" />
"android.permission.ACCESS_NETWORK_STATE" />
"android.permission.RECEIVE_BOOT_COMPLETED" />
"android.permission.WRITE_SETTINGS" />
"android.permission.VIBRATE" />
"android.permission.WRITE_EXTERNAL_STORAGE" />
"android.permission.ACCESS_DOWNLOAD_MANAGER"/>
"android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
"android.permission.DISABLE_KEYGUARD" />
"android.permission.ACCESS_COARSE_LOCATION" />
"android.permission.ACCESS_WIFI_STATE" />
"android.permission.INTERNET" >
"android.permission.ACCESS_NETWORK_STATE" >
"android.permission.ACCESS_WIFI_STATE" >
"android.permission.READ_PHONE_STATE" >
"android.permission.WRITE_SETTINGS" >
"android.permission.WRITE_EXTERNAL_STORAGE" />
"true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
"com.baidu.lbsapi.API_KEY"
android:value="yrDY98WCG2Dt1X010SBxYv3R" /> //key:开发者申请的key
"BaiduMobAd_STAT_ID"
android:value="8594056ea9" />
"BaiduMobAd_CHANNEL"
android:value="Baidu Market" />
"BaiduMobAd_EXCEPTION_LOG"
android:value="true" />
"BaiduMobAd_SEND_STRATEGY"
android:value="APP_START" />
"BaiduMobAd_ONLY_WIFI"
android:value="false" />
"BaiduMobAd_CELL_LOCATION"
android:value="true" />
"BaiduMobAd_GPS_LOCATION"
android:value="true" />
"BaiduMobAd_WIFI_LOCATION"
android:value="true" />
".MainActivity"
android:label="@string/app_name" >
"android.intent.action.MAIN" />
"android.intent.category.LAUNCHER" />
"com.baidu.location.f" android:enabled="true" android:process=":remote">
package com.example.administrator.mylocationdemo;
import android.app.Activity;
import android.content.Context;
import com.baidu.mobstat.StatService;
/**
* Created by Administrator on 2015/9/23.
*/
public class BaseActivity extends Activity {
@Override
protected void onResume() {
super.onResume();
StatService.onResume(this);
}
@Override
protected void onPause() {
super.onPause();
StatService.onPause(this);
}
}
代码管理工具有两种,SVN和Git,一般公司常用的是SVN。
SVN分服务器和客户端,下面给出两个的下载链接:
服务器:https://www.visualsvn.com/downloads/
客户端:http://tortoisesvn.net/downloads.html