百度推送 ,终于整理完整了

一定要注意application要继承FrontiaApplication,如果继承的是Application就会报错

忘了最重要的了 ,首先应该说是获取AppKey,哈哈哈 
  
 
  
分几步:
 
  
首先在官网下载pushservice-4.4.0.71.jar和libbdpush_V2_2.导入到工程,在libs目录下建三个文件夹armeabi、mips、x86,将libbdpush_V2_2分别放在各个文件夹中(注意:下载的是libbdpush_V2_2而不是libbdpush_V2_0

1、activity调用:/** 加载百度推送 **/// baiduInit();PushManager.startWork(getApplicationContext(),PushConstants.LOGIN_TYPE_API_KEY,Utils.getMetaValue(MainActivity.this, "api_key"));2、新建一个 Utils类,获取API_Key package com.sfdj.salebaby.push;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.content.pm.ApplicationInfo;import android.content.pm.PackageManager;import android.content.pm.PackageManager.NameNotFoundException;import android.location.LocationManager;import android.os.Bundle;import android.preference.PreferenceManager;public class Utils {    public static final String TAG = "PushDemoActivity";    public static final String RESPONSE_METHOD = "method";    public static final String RESPONSE_CONTENT = "content";    public static final String RESPONSE_ERRCODE = "errcode";    protected static final String ACTION_LOGIN = "com.baidu.pushdemo.action.LOGIN";    public static final String ACTION_MESSAGE = "com.baiud.pushdemo.action.MESSAGE";    public static final String ACTION_RESPONSE = "bccsclient.action.RESPONSE";    public static final String ACTION_SHOW_MESSAGE = "bccsclient.action.SHOW_MESSAGE";    protected static final String EXTRA_ACCESS_TOKEN = "access_token";    public static final String EXTRA_MESSAGE = "message";    public static String logStringCache = "";    // 获取ApiKey    public static String getMetaValue(Context context, String metaKey) {        Bundle metaData = null;        String apiKey = null;        if (context == null || metaKey == null) {            return null;        }        try {            ApplicationInfo ai = context.getPackageManager()                    .getApplicationInfo(context.getPackageName(),                            PackageManager.GET_META_DATA);            if (null != ai) {                metaData = ai.metaData;            }            if (null != metaData) {                apiKey = metaData.getString(metaKey);            }        } catch (NameNotFoundException e) {        }        return apiKey;    }    // 用share preference来实现是否绑定的�?��。在ionBind且成功时设置true,unBind且成功时设置false    public static boolean hasBind(Context context) {        SharedPreferences sp = PreferenceManager                .getDefaultSharedPreferences(context);        String flag = sp.getString("bind_flag", "");        if ("ok".equalsIgnoreCase(flag)) {            return true;        }        return false;    }    public static void setBind(Context context, boolean flag) {        String flagStr = "not";        if (flag) {            flagStr = "ok";        }        SharedPreferences sp = PreferenceManager                .getDefaultSharedPreferences(context);        Editor editor = sp.edit();        editor.putString("bind_flag", flagStr);        editor.commit();    }    public static List getTagsList(String originalText) {        if (originalText == null || originalText.equals("")) {            return null;        }        List tags = new ArrayList();        int indexOfComma = originalText.indexOf(',');        String tag;        while (indexOfComma != -1) {            tag = originalText.substring(0, indexOfComma);            tags.add(tag);            originalText = originalText.substring(indexOfComma + 1);            indexOfComma = originalText.indexOf(',');        }        tags.add(originalText);        return tags;    }    public static String getLogText(Context context) {        SharedPreferences sp = PreferenceManager                .getDefaultSharedPreferences(context);        return sp.getString("log_text", "");    }    public static void setLogText(Context context, String text) {        SharedPreferences sp = PreferenceManager                .getDefaultSharedPreferences(context);        Editor editor = sp.edit();        editor.putString("log_text", text);        editor.commit();    }    /***     * 判断gps是否�?��     *      * **/    public static boolean isGpsEnable(Context mContext) {          LocationManager locationManager =                   ((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE));          return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);      }     }3、加权限: 4、清单注册:
 
  

你可能感兴趣的:(百度推送,百度推送)